-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_option_test.go
99 lines (92 loc) · 2.18 KB
/
request_option_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package httpclient
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
var errNone = errors.New("no error")
func TestSetHeaders(t *testing.T) {
// default headers used to init the Client
headers := make(http.Header)
headers.Add("X-Test", "TestClient")
// test case struct
type testCase struct {
name string
extra map[string][]string
expected map[string][]string
}
// table tests
for _, tc := range []testCase{
{
name: "default case",
expected: map[string][]string{
"X-Test": {"TestClient"},
},
},
{
name: "overriding X-Test header value",
extra: map[string][]string{
"X-Test": {"Override"},
},
expected: map[string][]string{
"X-Test": {"Override"},
},
},
{
name: "adding X-Extra with single value",
extra: map[string][]string{
"X-Extra": {"true"},
},
expected: map[string][]string{
"X-Extra": {"true"},
"X-Test": {"TestClient"},
},
},
{
name: "adding X-Extra with multiple values",
extra: map[string][]string{
"X-Extra": {"true", "multi"},
},
expected: map[string][]string{
"X-Extra": {"true", "multi"},
"X-Test": {"TestClient"},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
c, err := New(Headers(headers))
if err != nil {
t.Errorf("trouble when creating the client: %v", err)
}
defer c.Close()
// startup a server with handler to check that received request headers
// are correct
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ensure headers are _received_ as expected
for k, vs := range tc.expected {
exp := strings.Join(vs, ", ")
act := strings.Join(r.Header.Values(k), ", ")
if act != exp {
t.Errorf("expected \"%s: %s\" Header value, got \"%s: %s\"", k, exp, k, act)
}
}
}))
defer s.Close()
// create our extra headers
extra := make(http.Header)
for k, vs := range tc.extra {
for _, v := range vs {
extra.Add(k, v)
}
}
// make the request
err = c.Get(context.Background(), NoopResponseHandler, s.URL, SetHeaders(extra))
if err != nil {
t.Errorf("unexpected error with SetHeaders: %v", err)
}
})
}
}