/
test_query.js
340 lines (270 loc) · 9.82 KB
/
test_query.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
'use strict'
const { expect } = require('chai')
const sinon = require('sinon')
const url = require('url')
const nock = require('../..')
const got = require('./got_client')
const assertRejects = require('assert-rejects')
describe('query params in path', () => {
it('matches that query string', async () => {
const scope = nock('http://example.test').get('/foo?bar=baz').reply()
const { statusCode } = await got('http://example.test/foo?bar=baz')
expect(statusCode).to.equal(200)
scope.done()
})
})
describe('`query()`', () => {
describe('when called with a string', () => {
it('matches a url encoded query string of the same name=value', async () => {
const scope = nock('http://example.test')
.get('/')
.query('foo%5Bbar%5D%3Dhello%20world%21')
.reply()
const { statusCode } = await got(
'http://example.test/?foo%5Bbar%5D%3Dhello%20world%21',
)
expect(statusCode).to.equal(200)
scope.done()
})
})
describe('when called with an object', () => {
it('matches a query string of the same name=value', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ foo: 'bar' })
.reply()
const { statusCode } = await got('http://example.test/?foo=bar')
expect(statusCode).to.equal(200)
scope.done()
})
it('matches multiple query strings of the same name=value', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ foo: 'bar', baz: 'foz' })
.reply()
const { statusCode } = await got('http://example.test/?foo=bar&baz=foz')
expect(statusCode).to.equal(200)
scope.done()
})
it('matches multiple query strings of the same name=value regardless of order', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ foo: 'bar', baz: 'foz' })
.reply()
const { statusCode } = await got('http://example.test/?baz=foz&foo=bar')
expect(statusCode).to.equal(200)
scope.done()
})
it('matches query values regardless of their type of declaration', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ num: 1, bool: true, empty: null, str: 'fou' })
.reply()
const { statusCode } = await got(
'http://example.test/?num=1&bool=true&empty=&str=fou',
)
expect(statusCode).to.equal(200)
scope.done()
})
it("doesn't match query values of requests without query string", async () => {
const scope1 = nock('http://example.test')
.get('/')
.query({ num: 1, bool: true, empty: null, str: 'fou' })
.reply(200, 'scope1')
const scope2 = nock('http://example.test').get('/').reply(200, 'scope2')
const { statusCode, body } = await got('http://example.test/')
expect(statusCode).to.equal(200)
expect(body).to.equal('scope2')
scope2.done()
expect(scope1.isDone()).to.be.false()
})
it('matches a query string using regexp', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ foo: /.*/ })
.reply()
const { statusCode } = await got('http://example.test/?foo=bar')
expect(statusCode).to.equal(200)
scope.done()
})
it('throws if query params have already been defined', () => {
const interceptor = nock('http://example.test').get('/?foo=bar')
expect(() => {
interceptor.query({ foo: 'baz' })
}).to.throw(Error, 'Query parameters have already been defined')
})
it('throws if it was already called', () => {
const interceptor = nock('http://example.test')
.get('/')
.query({ foo: 'bar' })
expect(() => {
interceptor.query({ baz: 'qux' })
}).to.throw(Error, 'Query parameters have already been defined')
})
it('matches a query string that contains special RFC3986 characters', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ 'foo&bar': 'hello&world' })
.reply()
const { statusCode } = await got('http://example.test/', {
searchParams: { 'foo&bar': 'hello&world' },
})
expect(statusCode).to.equal(200)
scope.done()
})
it('expects unencoded query params', async () => {
const scope = nock('http://example.test')
.get('/')
.query({ foo: 'hello%20world' })
.reply()
await assertRejects(
got('http://example.test/?foo=hello%20world'),
/Nock: No match for request/,
)
const { statusCode } = await got(
'http://example.test/?foo=hello%2520world',
)
expect(statusCode).to.equal(200)
scope.done()
})
it('matches a query string with pre-encoded values', async () => {
const scope = nock('http://example.test', { encodedQueryParams: true })
.get('/')
.query({ foo: 'hello%20world' })
.reply()
const { statusCode } = await got('http://example.test/?foo=hello%20world')
expect(statusCode).to.equal(200)
scope.done()
})
it('when called with "{}" will allow a match against ending in ?', async () => {
const scope = nock('http://example.test')
.get('/noquerystring')
.query({})
.reply()
const { statusCode } = await got('http://example.test/noquerystring?')
expect(statusCode).to.equal(200)
scope.done()
})
it('will not match when a query string does not match name=value', async () => {
nock('http://example.test').get('/').query({ foo: 'bar' }).reply()
await assertRejects(
got('http://example.test/?foo=baz'),
/Nock: No match for request/,
)
})
it('will not match when a query string is present that was not registered', async () => {
nock('http://example.test').get('/').query({ foo: 'bar' }).reply()
await assertRejects(
got('http://example.test/?foo=bar&baz=foz'),
/Nock: No match for request/,
)
})
it('will not match when a query string is malformed', async () => {
// This is a valid query string so it's not really malformed, just not
// matching. Should this test be removed?
nock('http://example.test').get('/').query({ foo: 'bar' }).reply()
await assertRejects(
got('http://example.test/?foobar'),
/Nock: No match for request/,
)
})
it('will not match when a query string has fewer correct values than expected', async () => {
nock('http://example.test')
.get('/')
.query({
num: 1,
bool: true,
empty: null,
str: 'fou',
})
.reply()
await assertRejects(
got('http://example.test/?num=1str=fou'),
/Nock: No match for request/,
)
})
it('query matching should not consider request arrays equal to comma-separated expectations', async () => {
nock('http://example.test').get('/').query({ foo: 'bar,baz' }).reply()
await assertRejects(
got('http://example.test?foo[]=bar&foo[]=baz'),
/Nock: No match for request/,
)
})
it('query matching should not consider comma-separated requests equal to array expectations', async () => {
nock('http://example.test')
.get('/')
.query({ foo: ['bar', 'baz'] })
.reply()
await assertRejects(
got('http://example.test?foo=bar%2Cbaz'),
/Nock: No match for request/,
)
})
})
describe('when called with URLSearchParams', () => {
it('matches', async () => {
const params = new url.URLSearchParams({ foo: 'bar' })
const scope = nock('http://example.test').get('/').query(params).reply()
const { statusCode } = await got('http://example.test?foo=bar')
expect(statusCode).to.equal(200)
scope.done()
})
})
describe('when called with invalid arguments', () => {
it('throws the expected error', () => {
const interceptor = nock('http://example.test').get('/')
expect(() => {
interceptor.query(1)
}).to.throw(Error, 'Argument Error: 1')
})
})
describe('when called with `true`', () => {
it('will allow all query strings to pass', async () => {
const scope = nock('http://example.test').get('/').query(true).reply()
const { statusCode } = await got('http://example.test/?foo=hello%20world')
expect(statusCode).to.equal(200)
scope.done()
})
it('will match when the path has no query', async () => {
const scope = nock('http://example.test').get('/').query(true).reply()
const { statusCode } = await got('http://example.test/')
expect(statusCode).to.equal(200)
scope.done()
})
})
describe('when called with a function', () => {
it('function called with actual queryObject', async () => {
const queryFn = sinon.stub().returns(true)
const scope = nock('http://example.test').get('/').query(queryFn).reply()
const { statusCode } = await got('http://example.test/?foo=bar&a=1&b=2')
expect(statusCode).to.equal(200)
expect(queryFn).to.have.been.calledOnceWithExactly({
foo: 'bar',
a: '1',
b: '2',
})
scope.done()
})
it('function return true the query treat as matched', async () => {
const scope = nock('http://example.test')
.get('/')
.query(() => true)
.reply()
const { statusCode } = await got(
'http://example.test/?ignore=the&actual=query',
)
expect(statusCode).to.equal(200)
scope.done()
})
it('function return false the query treat as un-matched', async () => {
nock('http://example.test')
.get('/')
.query(() => false)
.reply()
await assertRejects(
got('http://example.test/?i=should&pass=?'),
/Nock: No match for request/,
)
})
})
})