-
Notifications
You must be signed in to change notification settings - Fork 4
/
requests_test.go
164 lines (150 loc) · 2.84 KB
/
requests_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package requests
import (
"io"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
func TestBodyJSON(t *testing.T) {
jsonbody := func(s string) io.ReadCloser {
type rc struct {
io.Reader
io.Closer
}
return rc{
Reader: strings.NewReader(s),
}
}
type T struct {
A string `json:"a"`
}
tests := []struct {
Body
want []T
}{{
Body: Body{
ReadCloser: jsonbody(`{"a":"hello"}`),
},
want: []T{{A: "hello"}},
}, {
Body: Body{
ReadCloser: jsonbody(`{"a":"first"}{"a":"second"}`),
},
want: []T{{A: "first"}, {A: "second"}},
}}
for _, tt := range tests {
got := make([]T, len(tt.want))
for i := range got {
if err := tt.Body.JSON(&got[i]); err != nil {
t.Fatal(err)
}
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got: %v, want: %v", got, tt.want)
}
}
}
func TestResponseHeader(t *testing.T) {
header := func(key, val string, vals ...string) Header {
v := []string{val}
return Header{
Key: key,
Values: append(v, vals...),
}
}
resp := &Response{
Headers: []Header{
header("foo", "bar"),
header("quxx", "frob", "frob"),
},
}
tests := []struct {
*Response
key string
want string
}{{
resp,
"foo",
"bar",
}, {
resp,
"quxx",
"frob,frob",
}, {
resp,
"flimm",
"",
}}
for i, tc := range tests {
got := tc.Header(tc.key)
if got != tc.want {
t.Errorf("%d: Header(%q): got %q, want %v", i, tc.key, got, tc.want)
}
}
}
func TestToHeaders(t *testing.T) {
tests := []struct {
Headers []Header
want map[string][]string
}{{
Headers: []Header{
{Key: "foo", Values: []string{"bar"}},
{Key: "cram", Values: []string{"witt", "jannet"}},
},
want: map[string][]string{
"foo": []string{"bar"},
"cram": []string{"witt", "jannet"},
},
}, {
Headers: []Header{},
want: nil,
}}
for i, tc := range tests {
got := toHeaders(tc.Headers)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("%d: %v.toHeaders(): got: %v, want: %v", i, tc.Headers, got, tc.want)
}
}
}
func TestNewHTTPRequest(t *testing.T) {
tests := []struct {
Request
want http.Request
}{{
Request{
Method: "GET",
URL: "https://example.com",
Headers: []Header{
{Key: "Connection", Values: []string{"close"}},
{Key: "Upgrade", Values: []string{"h2c"}},
},
},
http.Request{
Method: "GET",
URL: &url.URL{
Scheme: "https",
Host: "example.com",
},
Host: "example.com",
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Connection": []string{"close"},
"Upgrade": []string{"h2c"},
},
},
}}
for i, tc := range tests {
got, err := newHttpRequest(&tc.Request)
if err != nil {
t.Errorf("%d: %v: %v", i, tc.Request, err)
continue
}
if !reflect.DeepEqual(got, &tc.want) {
t.Errorf("%d: %v: got:\n%+v, want:\n%+v", i, tc.Request, got, &tc.want)
}
}
}