-
Notifications
You must be signed in to change notification settings - Fork 53
/
response_test.go
249 lines (223 loc) · 5.74 KB
/
response_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package gentleman
import (
"errors"
"io/ioutil"
"os"
"testing"
"github.com/nbio/st"
"gopkg.in/h2non/gentleman.v2/utils"
)
func TestResponseBuild(t *testing.T) {
ctx := NewContext()
ctx.Response.StatusCode = 200
ctx.Response.Header.Set("foo", "bar")
res, err := buildResponse(ctx)
if err != nil {
t.Errorf("Error: %s", err)
}
if res.Header.Get("foo") != "bar" {
t.Error("Invalid foo header")
}
if res.StatusCode != 200 {
t.Errorf("Invalid status code: %d", res.StatusCode)
}
if !res.Ok {
t.Error("Ok field must be true")
}
if res.ClientError || res.ServerError {
t.Error("Invalid client/server status error")
}
if res.Context != ctx {
t.Error("Invalid context")
}
if res.Header.Get("foo") != "bar" {
t.Error("Invalid foo header")
}
}
func TestResponseBuildStatusCodes(t *testing.T) {
cases := []struct {
code int
ok bool
client bool
server bool
}{
{0, false, false, false},
{200, true, false, false},
{204, true, false, false},
{300, true, false, false},
{400, false, true, false},
{404, false, true, false},
{499, false, true, false},
{500, false, false, true},
{599, false, false, true},
}
for _, test := range cases {
ctx := NewContext()
ctx.Response.StatusCode = test.code
res, _ := buildResponse(ctx)
if res.StatusCode != test.code {
t.Errorf("Invalid status code: %d", res.StatusCode)
}
if res.Ok != test.ok {
t.Errorf("Invalid Ok field: %v", res.Ok)
}
if res.ClientError != test.client {
t.Error("Invalid ClientError field")
}
if res.ServerError != test.server {
t.Error("Invalid ServerError field")
}
}
}
func TestResponseReadError(t *testing.T) {
ctx := NewContext()
ctx.Error = errors.New("foo error")
res, _ := buildResponse(ctx)
num, err := res.Read([]byte{})
st.Reject(t, err, nil)
st.Expect(t, err.Error(), "foo error")
st.Expect(t, num, -1)
}
func TestResponseCloseError(t *testing.T) {
ctx := NewContext()
ctx.Error = errors.New("foo error")
res, _ := buildResponse(ctx)
err := res.Close()
st.Reject(t, err, nil)
st.Expect(t, err.Error(), "foo error")
}
func TestResponseSaveToFile(t *testing.T) {
ctx := NewContext()
utils.WriteBodyString(ctx.Response, "hello world")
res, _ := buildResponse(ctx)
err := res.SaveToFile("body.tmp")
st.Expect(t, err, nil)
defer os.Remove("body.tmp")
body, err := ioutil.ReadFile("body.tmp")
st.Expect(t, err, nil)
st.Expect(t, string(body), "hello world")
}
func TestResponseSaveToFileError(t *testing.T) {
ctx := NewContext()
ctx.Error = errors.New("foo error")
res, _ := buildResponse(ctx)
err := res.SaveToFile("body.tmp")
st.Reject(t, err, nil)
}
func TestResponseJSON(t *testing.T) {
type jsonData struct {
Foo string `json:"foo"`
}
json := &jsonData{}
ctx := NewContext()
utils.WriteBodyString(ctx.Response, `{"foo":"bar"}`)
res, _ := buildResponse(ctx)
err := res.JSON(json)
st.Expect(t, err, nil)
st.Expect(t, json.Foo, "bar")
}
func TestResponseJSONError(t *testing.T) {
type jsonData struct {
Foo string `json:"foo"`
}
json := &jsonData{}
ctx := NewContext()
ctx.Error = errors.New("foo error")
res, _ := buildResponse(ctx)
err := res.JSON(json)
st.Reject(t, err, nil)
st.Expect(t, json.Foo, "")
}
func TestResponseXML(t *testing.T) {
type xml struct {
Foo string `xml:"foo"`
}
xmlData := &xml{}
ctx := NewContext()
utils.WriteBodyString(ctx.Response, `<xml><foo>bar</foo></xml>`)
res, _ := buildResponse(ctx)
err := res.XML(xmlData, nil)
st.Expect(t, err, nil)
st.Expect(t, xmlData.Foo, "bar")
}
func TestResponseXMLError(t *testing.T) {
type xml struct {
Foo string `xml:"foo"`
}
xmlData := &xml{}
ctx := NewContext()
ctx.Error = errors.New("foo error")
utils.WriteBodyString(ctx.Response, `<xml><foo>bar</foo></xml>`)
res, _ := buildResponse(ctx)
err := res.XML(xmlData, nil)
st.Reject(t, err, nil)
st.Expect(t, xmlData.Foo, "")
}
func TestResponseString(t *testing.T) {
ctx := NewContext()
utils.WriteBodyString(ctx.Response, "foo bar")
res, err := buildResponse(ctx)
body := res.String()
st.Expect(t, err, nil)
st.Expect(t, body, "foo bar")
}
func TestResponseStringError(t *testing.T) {
ctx := NewContext()
ctx.Error = errors.New("foo error")
utils.WriteBodyString(ctx.Response, "foo bar")
res, err := buildResponse(ctx)
body := res.String()
st.Reject(t, err, nil)
st.Expect(t, body, "")
}
func TestResponseBytes(t *testing.T) {
ctx := NewContext()
utils.WriteBodyString(ctx.Response, "foo bar")
res, err := buildResponse(ctx)
body := res.Bytes()
st.Expect(t, err, nil)
st.Expect(t, string(body), "foo bar")
}
func TestResponseBytesError(t *testing.T) {
ctx := NewContext()
ctx.Error = errors.New("foo error")
utils.WriteBodyString(ctx.Response, "foo bar")
res, err := buildResponse(ctx)
body := res.Bytes()
st.Reject(t, err, nil)
st.Expect(t, string(body), "")
}
func TestResponseReaderBuffer(t *testing.T) {
ctx := NewContext()
utils.WriteBodyString(ctx.Response, "foo bar")
res, err := buildResponse(ctx)
res.RawResponse.ContentLength = 7
body := res.Bytes()
st.Expect(t, err, nil)
st.Expect(t, string(body), "foo bar")
st.Expect(t, res.buffer.String(), "foo bar")
res.ClearInternalBuffer()
st.Expect(t, string(res.Bytes()), "")
}
func TestResponseReaderEmtpyBuffer(t *testing.T) {
ctx := NewContext()
res, err := buildResponse(ctx)
res.RawResponse.ContentLength = 0
body := res.Bytes()
st.Expect(t, err, nil)
st.Expect(t, string(body), "")
st.Expect(t, res.buffer.String(), "")
res.ClearInternalBuffer()
st.Expect(t, string(res.Bytes()), "")
}
func TestResponseReaderBufferError(t *testing.T) {
ctx := NewContext()
ctx.Error = errors.New("foo error")
res, err := buildResponse(ctx)
body := res.Bytes()
st.Reject(t, err, nil)
st.Expect(t, string(body), "")
st.Expect(t, res.buffer.Len(), 0)
res.ClearInternalBuffer()
st.Expect(t, res.buffer.Len(), 0)
}