-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclient_builder_header_test.go
More file actions
121 lines (114 loc) · 3.32 KB
/
client_builder_header_test.go
File metadata and controls
121 lines (114 loc) · 3.32 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package fastshot
import (
"testing"
"github.com/opus-domini/fast-shot/constant/header"
"github.com/opus-domini/fast-shot/constant/mime"
)
func TestClientHeaderBuilder(t *testing.T) {
const testAgent = "TestAgent"
tests := []struct {
name string
setup func(*ClientBuilder)
assertFunc func(*testing.T, *ClientBuilder)
}{
{
name: "Add single header",
setup: func(cb *ClientBuilder) {
cb.Header().Add(header.ContentType, mime.JSON.String())
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.ContentType); got != mime.JSON.String() {
t.Errorf("got %q, want %q", got, mime.JSON.String())
}
},
},
{
name: "Add multiple headers",
setup: func(cb *ClientBuilder) {
cb.Header().AddAll(map[header.Type]string{
header.ContentType: mime.JSON.String(),
header.UserAgent: testAgent,
})
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.ContentType); got != mime.JSON.String() {
t.Errorf("ContentType got %q, want %q", got, mime.JSON.String())
}
if got := cb.client.Header().Get(header.UserAgent); got != testAgent {
t.Errorf("UserAgent got %q, want %q", got, testAgent)
}
},
},
{
name: "Set single header",
setup: func(cb *ClientBuilder) {
cb.Header().Set(header.ContentType, mime.JSON.String())
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.ContentType); got != mime.JSON.String() {
t.Errorf("got %q, want %q", got, mime.JSON.String())
}
},
},
{
name: "Set multiple headers",
setup: func(cb *ClientBuilder) {
cb.Header().SetAll(map[header.Type]string{
header.ContentType: mime.JSON.String(),
header.UserAgent: testAgent,
})
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.ContentType); got != mime.JSON.String() {
t.Errorf("ContentType got %q, want %q", got, mime.JSON.String())
}
if got := cb.client.Header().Get(header.UserAgent); got != testAgent {
t.Errorf("UserAgent got %q, want %q", got, testAgent)
}
},
},
{
name: "Add Accept header",
setup: func(cb *ClientBuilder) {
cb.Header().AddAccept(mime.JSON)
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.Accept); got != mime.JSON.String() {
t.Errorf("got %q, want %q", got, mime.JSON.String())
}
},
},
{
name: "Add Content-Type header",
setup: func(cb *ClientBuilder) {
cb.Header().AddContentType(mime.JSON)
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.ContentType); got != mime.JSON.String() {
t.Errorf("got %q, want %q", got, mime.JSON.String())
}
},
},
{
name: "Add User-Agent header",
setup: func(cb *ClientBuilder) {
cb.Header().AddUserAgent(testAgent)
},
assertFunc: func(t *testing.T, cb *ClientBuilder) {
if got := cb.client.Header().Get(header.UserAgent); got != testAgent {
t.Errorf("got %q, want %q", got, testAgent)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Arrange
cb := NewClient("https://example.com")
// Act
tt.setup(cb)
// Assert
tt.assertFunc(t, cb)
})
}
}