-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathresponse_test.go
165 lines (139 loc) · 3.94 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
package gock
import (
"bytes"
"errors"
"io"
"net/http"
"testing"
"time"
"github.com/nbio/st"
)
func TestNewResponse(t *testing.T) {
res := NewResponse()
res.Status(200)
st.Expect(t, res.StatusCode, 200)
res.SetHeader("foo", "bar")
st.Expect(t, res.Header.Get("foo"), "bar")
res.Delay(1000 * time.Millisecond)
st.Expect(t, res.ResponseDelay, 1000*time.Millisecond)
res.EnableNetworking()
st.Expect(t, res.UseNetwork, true)
}
func TestResponseStatus(t *testing.T) {
res := NewResponse()
st.Expect(t, res.StatusCode, 0)
res.Status(200)
st.Expect(t, res.StatusCode, 200)
}
func TestResponseType(t *testing.T) {
res := NewResponse()
res.Type("json")
st.Expect(t, res.Header.Get("Content-Type"), "application/json")
res = NewResponse()
res.Type("xml")
st.Expect(t, res.Header.Get("Content-Type"), "application/xml")
res = NewResponse()
res.Type("foo/bar")
st.Expect(t, res.Header.Get("Content-Type"), "foo/bar")
}
func TestResponseSetHeader(t *testing.T) {
res := NewResponse()
res.SetHeader("foo", "bar")
res.SetHeader("bar", "baz")
st.Expect(t, res.Header.Get("foo"), "bar")
st.Expect(t, res.Header.Get("bar"), "baz")
}
func TestResponseAddHeader(t *testing.T) {
res := NewResponse()
res.AddHeader("foo", "bar")
res.AddHeader("foo", "baz")
st.Expect(t, res.Header.Get("foo"), "bar")
st.Expect(t, res.Header["Foo"][1], "baz")
}
func TestResponseSetHeaders(t *testing.T) {
res := NewResponse()
res.SetHeaders(map[string]string{"foo": "bar", "bar": "baz"})
st.Expect(t, res.Header.Get("foo"), "bar")
st.Expect(t, res.Header.Get("bar"), "baz")
}
func TestResponseBody(t *testing.T) {
res := NewResponse()
res.Body(bytes.NewBuffer([]byte("foo bar")))
st.Expect(t, string(res.BodyBuffer), "foo bar")
}
func TestResponseBodyGenerator(t *testing.T) {
res := NewResponse()
generator := func() io.ReadCloser {
return io.NopCloser(bytes.NewBuffer([]byte("foo bar")))
}
res.BodyGenerator(generator)
bytes, err := io.ReadAll(res.BodyGen())
st.Expect(t, err, nil)
st.Expect(t, string(bytes), "foo bar")
}
func TestResponseBodyString(t *testing.T) {
res := NewResponse()
res.BodyString("foo bar")
st.Expect(t, string(res.BodyBuffer), "foo bar")
}
func TestResponseFile(t *testing.T) {
res := NewResponse()
res.File("version.go")
st.Expect(t, string(res.BodyBuffer)[:12], "package gock")
}
func TestResponseJSON(t *testing.T) {
res := NewResponse()
res.JSON(map[string]string{"foo": "bar"})
st.Expect(t, string(res.BodyBuffer)[:13], `{"foo":"bar"}`)
st.Expect(t, res.Header.Get("Content-Type"), "application/json")
}
func TestResponseXML(t *testing.T) {
res := NewResponse()
type xml struct {
Data string `xml:"data"`
}
res.XML(xml{Data: "foo"})
st.Expect(t, string(res.BodyBuffer), `<xml><data>foo</data></xml>`)
st.Expect(t, res.Header.Get("Content-Type"), "application/xml")
}
func TestResponseMap(t *testing.T) {
res := NewResponse()
st.Expect(t, len(res.Mappers), 0)
res.Map(func(res *http.Response) *http.Response {
return res
})
st.Expect(t, len(res.Mappers), 1)
}
func TestResponseFilter(t *testing.T) {
res := NewResponse()
st.Expect(t, len(res.Filters), 0)
res.Filter(func(res *http.Response) bool {
return true
})
st.Expect(t, len(res.Filters), 1)
}
func TestResponseSetError(t *testing.T) {
res := NewResponse()
st.Expect(t, res.Error, nil)
res.SetError(errors.New("foo error"))
st.Expect(t, res.Error.Error(), "foo error")
}
func TestResponseDelay(t *testing.T) {
res := NewResponse()
st.Expect(t, res.ResponseDelay, 0*time.Microsecond)
res.Delay(100 * time.Millisecond)
st.Expect(t, res.ResponseDelay, 100*time.Millisecond)
}
func TestResponseEnableNetworking(t *testing.T) {
res := NewResponse()
st.Expect(t, res.UseNetwork, false)
res.EnableNetworking()
st.Expect(t, res.UseNetwork, true)
}
func TestResponseDone(t *testing.T) {
res := NewResponse()
res.Mock = &Mocker{request: &Request{Counter: 1}, disabler: new(disabler)}
st.Expect(t, res.Done(), false)
res.Mock.Disable()
st.Expect(t, res.Done(), true)
}