-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_test.go
160 lines (146 loc) · 3.49 KB
/
mock_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
package httpmock
import (
"io/ioutil"
"testing"
)
func Test_mock_http_response(t *testing.T) {
tests := []struct {
title string
givenMock *Response
givenURI string
expectedStatus int
expectedtResponse string
}{
{
title: "GET for a valid URI",
givenMock: &Response{
URI: "/hello",
StatusCode: 200,
Body: "hello test",
},
givenURI: "/hello",
expectedStatus: 200,
expectedtResponse: "hello test",
},
{
title: "GET for a invalid URI",
givenMock: &Response{
URI: "/hello",
StatusCode: 200,
Body: "hello test",
},
givenURI: "/invalid",
expectedStatus: 404,
expectedtResponse: "Resource not found",
},
}
for _, tc := range tests {
t.Run(tc.title, func(t *testing.T) {
client := Client(tc.givenMock)
resp, err := client.Get("http://apiurl.com/" + tc.givenURI)
if err != nil {
t.Fatalf("Not expecting an error but error occurred executing http request : %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Not expecting an error but error occurred reading the http response : %v", err)
}
if resp.StatusCode != tc.expectedStatus {
t.Fatalf("Expecting %d response code but got %d response code", tc.expectedStatus, resp.StatusCode)
}
if string(body) != tc.expectedtResponse {
t.Fatalf("Expecting %s response but got %s response", tc.expectedtResponse, body)
}
})
}
}
func Test_multiple_http_response_mock(t *testing.T) {
type expected struct {
URI string
status int
response string
}
var tests = []struct {
title string
givenMock MultiResponse
expected []*expected
}{
{
title: "GET for a valid URI's",
givenMock: MultiResponse{
&Response{
URI: "/hello",
StatusCode: 200,
Body: "hello test",
},
&Response{
URI: "/foo",
StatusCode: 200,
Body: "foo test",
},
},
expected: []*expected{
{
URI: "/hello",
status: 200,
response: "hello test",
},
{
URI: "/foo",
status: 200,
response: "foo test",
},
},
},
{
title: "GET for invalid URI's",
givenMock: MultiResponse{
&Response{
URI: "/hello",
StatusCode: 200,
Body: "hello test",
},
&Response{
URI: "/foo",
StatusCode: 200,
Body: "foo test",
},
},
expected: []*expected{
{
URI: "/hello",
status: 200,
response: "hello test",
},
{
URI: "/bar",
status: 404,
response: "Resource not found",
},
},
},
}
for _, tc := range tests {
t.Run(tc.title, func(t *testing.T) {
client := MultiResponseClient(tc.givenMock)
for _, expected := range tc.expected {
resp, err := client.Get("http://apiurl.com/" + expected.URI)
if err != nil {
t.Fatalf("Not expecting an error but error occurred executing http request : %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Not expecting an error but error occurred reading the http response : %v", err)
}
if resp.StatusCode != expected.status {
t.Fatalf("Expecting %d response code but got %d response code", expected.status, resp.StatusCode)
}
if string(body) != expected.response {
t.Fatalf("Expecting %s response but got %s response", expected.response, body)
}
}
})
}
}