This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
forked from jarcoal/httpmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
204 lines (167 loc) · 3.98 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
package httpmock
import (
"encoding/json"
"encoding/xml"
"errors"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
func TestResponderFromResponse(t *testing.T) {
responder := ResponderFromResponse(NewStringResponse(200, "hello world"))
req, err := http.NewRequest(http.MethodGet, testUrl, nil)
if err != nil {
t.Fatal("Error creating request")
}
response1, err := responder(req)
if err != nil {
t.Error("Error should be nil")
}
testUrlWithQuery := testUrl + "?a=1"
req, err = http.NewRequest(http.MethodGet, testUrlWithQuery, nil)
if err != nil {
t.Fatal("Error creating request")
}
response2, err := responder(req)
if err != nil {
t.Error("Error should be nil")
}
// Body should be the same for both responses
assertBody(t, response1, "hello world")
assertBody(t, response2, "hello world")
// Request should be non-nil and different for each response
if response1.Request != nil && response2.Request != nil {
if response1.Request.URL.String() != testUrl {
t.Errorf("Expected request url %s, got: %s", testUrl, response1.Request.URL.String())
}
if response2.Request.URL.String() != testUrlWithQuery {
t.Errorf("Expected request url %s, got: %s", testUrlWithQuery, response2.Request.URL.String())
}
} else {
t.Error("response.Request should not be nil")
}
}
func TestNewStringResponse(t *testing.T) {
body := "hello world"
status := 200
response := NewStringResponse(status, body)
data, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != body {
t.FailNow()
}
if response.StatusCode != status {
t.FailNow()
}
}
func TestNewBytesResponse(t *testing.T) {
body := []byte("hello world")
status := 200
response := NewBytesResponse(status, body)
data, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != string(body) {
t.FailNow()
}
if response.StatusCode != status {
t.FailNow()
}
}
func TestNewJsonResponse(t *testing.T) {
type schema struct {
Hello string `json:"hello"`
}
body := &schema{"world"}
status := 200
response, err := NewJsonResponse(status, body)
if err != nil {
t.Fatal(err)
}
if response.StatusCode != status {
t.FailNow()
}
if response.Header.Get("Content-Type") != "application/json" {
t.FailNow()
}
checkBody := &schema{}
if err := json.NewDecoder(response.Body).Decode(checkBody); err != nil {
t.Fatal(err)
}
if checkBody.Hello != body.Hello {
t.FailNow()
}
}
func TestNewXmlResponse(t *testing.T) {
type schema struct {
Hello string `xml:"hello"`
}
body := &schema{"world"}
status := 200
response, err := NewXmlResponse(status, body)
if err != nil {
t.Fatal(err)
}
if response.StatusCode != status {
t.FailNow()
}
if response.Header.Get("Content-Type") != "application/xml" {
t.FailNow()
}
checkBody := &schema{}
if err := xml.NewDecoder(response.Body).Decode(checkBody); err != nil {
t.Fatal(err)
}
if checkBody.Hello != body.Hello {
t.FailNow()
}
}
func TestNewErrorResponder(t *testing.T) {
responder := NewErrorResponder(errors.New("oh no"))
req, err := http.NewRequest(http.MethodGet, testUrl, nil)
if err != nil {
t.Fatal("Error creating request")
}
response, err := responder(req)
if response != nil {
t.Error("Response should be nil")
}
expected := errors.New("oh no")
if !reflect.DeepEqual(err, expected) {
t.Errorf("Expected error %#v, got: %#v", expected, err)
}
}
func TestRewindResponse(t *testing.T) {
body := []byte("hello world")
status := 200
responses := []*http.Response{
NewBytesResponse(status, body),
NewStringResponse(status, string(body)),
}
for _, response := range responses {
data, err := ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != string(body) {
t.FailNow()
}
if response.StatusCode != status {
t.FailNow()
}
data, err = ioutil.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if string(data) != string(body) {
t.FailNow()
}
if response.StatusCode != status {
t.FailNow()
}
}
}