This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
response_test.go
159 lines (147 loc) · 3.86 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
package httptest
import (
"encoding/json"
"net/http"
"testing"
)
// ---------------------------------------------------------------------------
type caseMatchHeader struct {
expected http.Header
real http.Header
message string
ok bool
}
func matchHeaders(expected, real http.Header) (message string, ok bool) {
for k, v := range expected {
if !matchHeader(real, k, v) {
return "unmatched response header: " + k, false
}
}
return "", true
}
func TestMatchHeader(t *testing.T) {
cases := []caseMatchHeader{
{
expected: http.Header{"a": {"a1"}, "b": {"b1", "b2"}},
real: http.Header{"c": {"c1"}, "a": {"a1"}, "b": {"b1", "b2"}, "d": {"d1"}},
ok: true,
},
{
expected: http.Header{"a": {"a1"}, "b": {"b1", "b2"}},
real: http.Header{"c": {"c1"}, "a": {"a1"}, "b": {"b1"}, "d": {"d1"}},
message: "unmatched response header: b",
},
{
expected: http.Header{"a": {"a1"}, "b": {"b1", "b2"}},
real: http.Header{"c": {"c1"}, "a": {"a1"}, "d": {"d1"}},
message: "unmatched response header: b",
},
{
expected: http.Header{},
real: http.Header{"c": {"c1"}, "a": {"a1"}, "d": {"d1"}},
ok: true,
},
}
for _, c := range cases {
message, ok := matchHeaders(c.expected, c.real)
if message != c.message || ok != c.ok {
t.Fatal("matchHeader failed:", c, message, ok)
}
}
}
// ---------------------------------------------------------------------------
type caseMatchBody struct {
expBody string
expBodyType string
respBody string
respBodyType string
message string
ok bool
}
func TestMatchBody(t *testing.T) {
cases := []caseMatchBody{
{
expBody: ``,
expBodyType: "application/json",
respBody: ``,
respBodyType: "application/json",
ok: true,
},
{
expBody: `abc`,
expBodyType: "",
respBody: `abc`,
respBodyType: "application/json",
ok: true,
},
{
expBody: `abc`,
expBodyType: "application/text",
respBody: `abc`,
respBodyType: "application/text",
ok: true,
},
{
expBody: `abc`,
expBodyType: "application/text",
respBody: `abc`,
respBodyType: "application/json",
message: "unmatched Content-Type",
},
{
expBody: `{}`,
expBodyType: "application/json",
respBody: `{ }`,
respBodyType: "application/json",
ok: true,
},
{
expBody: `{}`,
expBodyType: "application/json",
respBody: `{"aaa": "123"}`,
respBodyType: "application/json",
ok: true,
},
{
expBody: `{"a": "a1", "b": ["b1", "b2"]}`,
expBodyType: "application/json",
respBody: `{"c": 1, "a": "a1", "b": ["b1", "b2"], "d": 2.0}`,
respBodyType: "application/json",
ok: true,
},
{
expBody: `{"a": "a1", "b": ["b1", "b2"]}`,
expBodyType: "application/json",
respBody: `{"c": 1, "a": "a1", "d": 2.0}`,
respBodyType: "application/json",
message: "unmatched json response body - field b",
},
{
expBody: `{"a": "a1", "b": ["b1", "b2"]}`,
expBodyType: "application/json",
respBody: `{"c": 1, "a": "a1", "b": [1], "d": 2.0}`,
respBodyType: "application/json",
message: "unmatched json response body - field b",
},
}
for _, c := range cases {
resp := &Response{
RawBody: []byte(c.respBody),
Header: make(http.Header),
}
if c.respBodyType != "" {
resp.BodyType = c.respBodyType
resp.Header["Content-Type"] = []string{c.respBodyType}
}
if len(resp.RawBody) > 0 {
if resp.BodyType == "application/json" {
resp.Err = json.Unmarshal(resp.RawBody, &resp.BodyObj)
}
}
message, ok := matchBody(c.expBodyType, c.expBody, resp)
if message != c.message || ok != c.ok {
t.Fatal("matchBody failed:", c, message, ok)
}
}
}
// ---------------------------------------------------------------------------