File: matchers_test.go

package info (click to toggle)
golang-gopkg-h2non-gock.v1 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 364 kB
  • sloc: makefile: 2
file content (251 lines) | stat: -rw-r--r-- 7,322 bytes parent folder | download
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
250
251
package gock

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/nbio/st"
)

func TestMatchMethod(t *testing.T) {
	cases := []struct {
		value   string
		method  string
		matches bool
	}{
		{"GET", "GET", true},
		{"POST", "POST", true},
		{"", "POST", true},
		{"POST", "GET", false},
		{"PUT", "GET", false},
	}

	for _, test := range cases {
		req := &http.Request{Method: test.method}
		ereq := &Request{Method: test.value}
		matches, err := MatchMethod(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}

func TestMatchScheme(t *testing.T) {
	cases := []struct {
		value   string
		scheme  string
		matches bool
	}{
		{"http", "http", true},
		{"https", "https", true},
		{"http", "https", false},
		{"", "https", true},
		{"https", "", true},
	}

	for _, test := range cases {
		req := &http.Request{URL: &url.URL{Scheme: test.scheme}}
		ereq := &Request{URLStruct: &url.URL{Scheme: test.value}}
		matches, err := MatchScheme(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}

func TestMatchHost(t *testing.T) {
	cases := []struct {
		value            string
		url              string
		matches          bool
		matchesNonRegexp bool
	}{
		{"foo.com", "foo.com", true, true},
		{"FOO.com", "foo.com", true, true},
		{"foo.net", "foo.com", false, false},
		{"foo.bar.net", "foo-bar.net", true, false},
		{"foo", "foo.com", true, false},
		{"(.*).com", "foo.com", true, false},
		{"127.0.0.1", "127.0.0.1", true, true},
		{"127.0.0.2", "127.0.0.1", false, false},
		{"127.0.0.*", "127.0.0.1", true, false},
		{"127.0.0.[0-9]", "127.0.0.7", true, false},
	}

	for _, test := range cases {
		req := &http.Request{URL: &url.URL{Host: test.url}}
		ereq := &Request{URLStruct: &url.URL{Host: test.value}}
		matches, err := MatchHost(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
		ereq.WithOptions(Options{DisableRegexpHost: true})
		matches, err = MatchHost(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matchesNonRegexp)
	}
}

func TestMatchPath(t *testing.T) {
	cases := []struct {
		value   string
		path    string
		matches bool
	}{
		{"/foo", "/foo", true},
		{"/foo", "/foo/bar", true},
		{"bar", "/foo/bar", true},
		{"foo", "/foo/bar", true},
		{"bar$", "/foo/bar", true},
		{"/foo/*", "/foo/bar", true},
		{"/foo/[a-z]+", "/foo/bar", true},
		{"/foo/baz", "/foo/bar", false},
		{"/foo/baz", "/foo/bar", false},
		{"/foo/bar%3F+%C3%A9", "/foo/bar%3F+%C3%A9", true},
	}

	for _, test := range cases {
		u, _ := url.Parse("http://foo.com" + test.path)
		mu, _ := url.Parse("http://foo.com" + test.value)
		req := &http.Request{URL: u}
		ereq := &Request{URLStruct: mu}
		matches, err := MatchPath(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}

func TestMatchHeaders(t *testing.T) {
	cases := []struct {
		values  http.Header
		headers http.Header
		matches bool
	}{
		{http.Header{"foo": []string{"bar"}}, http.Header{"foo": []string{"bar"}}, true},
		{http.Header{"foo": []string{"bar"}}, http.Header{"foo": []string{"barbar"}}, true},
		{http.Header{"bar": []string{"bar"}}, http.Header{"foo": []string{"bar"}}, false},
		{http.Header{"foofoo": []string{"bar"}}, http.Header{"foo": []string{"bar"}}, false},
		{http.Header{"foo": []string{"bar(.*)"}}, http.Header{"foo": []string{"barbar"}}, true},
		{http.Header{"foo": []string{"b(.*)"}}, http.Header{"foo": []string{"barbar"}}, true},
		{http.Header{"foo": []string{"^bar$"}}, http.Header{"foo": []string{"bar"}}, true},
		{http.Header{"foo": []string{"^bar$"}}, http.Header{"foo": []string{"barbar"}}, false},
		{http.Header{"UPPERCASE": []string{"bar"}}, http.Header{"UPPERCASE": []string{"bar"}}, true},
		{http.Header{"Mixed-CASE": []string{"bar"}}, http.Header{"Mixed-CASE": []string{"bar"}}, true},
		{http.Header{"User-Agent": []string{"Agent (version1.0)"}}, http.Header{"User-Agent": []string{"Agent (version1.0)"}}, true},
		{http.Header{"Content-Type": []string{"(.*)/plain"}}, http.Header{"Content-Type": []string{"text/plain"}}, true},
	}

	for _, test := range cases {
		req := &http.Request{Header: test.headers}
		ereq := &Request{Header: test.values}
		matches, err := MatchHeaders(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}

func TestMatchQueryParams(t *testing.T) {
	cases := []struct {
		value   string
		path    string
		matches bool
	}{
		{"foo=bar", "foo=bar", true},
		{"foo=bar", "foo=foo&foo=bar", true},
		{"foo=b*", "foo=bar", true},
		{"foo=.*", "foo=bar", true},
		{"foo=f[o]{2}", "foo=foo", true},
		{"foo=bar&bar=foo", "foo=bar&foo=foo&bar=foo", true},
		{"foo=", "foo=bar", true},
		{"foo=foo", "foo=bar", false},
		{"bar=bar", "foo=bar bar", false},
	}

	for _, test := range cases {
		u, _ := url.Parse("http://foo.com/?" + test.path)
		mu, _ := url.Parse("http://foo.com/?" + test.value)
		req := &http.Request{URL: u}
		ereq := &Request{URLStruct: mu}
		matches, err := MatchQueryParams(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}

func TestMatchPathParams(t *testing.T) {
	cases := []struct {
		key     string
		value   string
		path    string
		matches bool
	}{
		{"foo", "bar", "/foo/bar", true},
		{"foo", "bar", "/foo/test/bar", false},
		{"foo", "bar", "/test/foo/bar/ack", true},
		{"foo", "bar", "/foo", false},
	}

	for i, test := range cases {
		u, _ := url.Parse("http://foo.com" + test.path)
		mu, _ := url.Parse("http://foo.com" + test.path)
		req := &http.Request{URL: u}
		ereq := &Request{
			URLStruct:  mu,
			PathParams: map[string]string{test.key: test.value},
		}
		matches, err := MatchPathParams(req, ereq)
		st.Expect(t, err, nil, i)
		st.Expect(t, matches, test.matches, i)
	}
}

func TestMatchBody(t *testing.T) {
	cases := []struct {
		value   string
		body    string
		matches bool
	}{
		{"foo bar", "foo bar\n", true},
		{"foo", "foo bar\n", true},
		{"f[o]+", "foo\n", true},
		{`"foo"`, `{"foo":"bar"}\n`, true},
		{`{"foo":"bar"}`, `{"foo":"bar"}\n`, true},
		{`{"foo":"foo"}`, `{"foo":"bar"}\n`, false},

		{`{"foo":"bar","bar":"foo"}`, `{"bar":"foo","foo":"bar"}`, true},
		{`{"bar":"foo","foo":{"two":"three","three":"two"}}`, `{"foo":{"three":"two","two":"three"},"bar":"foo"}`, true},
	}

	for _, test := range cases {
		req := &http.Request{Body: createReadCloser([]byte(test.body))}
		ereq := &Request{BodyBuffer: []byte(test.value)}
		matches, err := MatchBody(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}

func TestMatchBody_MatchType(t *testing.T) {
	body := `{"foo":"bar"}`
	cases := []struct {
		body               string
		requestContentType string
		customBodyType     string
		matches            bool
	}{
		{body, "application/vnd.apiname.v1+json", "foobar", false},
		{body, "application/vnd.apiname.v1+json", "application/vnd.apiname.v1+json", true},
		{body, "application/json", "foobar", false},
		{body, "application/json", "", true},
		{"", "", "", true},
	}

	for _, test := range cases {
		req := &http.Request{
			Header: http.Header{"Content-Type": []string{test.requestContentType}},
			Body:   createReadCloser([]byte(test.body)),
		}
		ereq := NewRequest().BodyString(test.body).MatchType(test.customBodyType)
		matches, err := MatchBody(req, ereq)
		st.Expect(t, err, nil)
		st.Expect(t, matches, test.matches)
	}
}