Skip to content

Commit 560a5d8

Browse files
mastermattpaulmelnikow
authored andcommitted
fix: Restore behavior of Interceptor.filteringPath (#1543)
Calling `filteringPath` on the intercept instance was broken as the transform fn set on the scope had the wrong name. Proxying to the Scope’s method allows for the regex version to work too. The bulk of the changed lines come from moving the tests to their appropriate file since the real logic acts on the Scope. Found when looking at Uncovered lines in coveralls.
1 parent 6a26f41 commit 560a5d8

File tree

3 files changed

+88
-71
lines changed

3 files changed

+88
-71
lines changed

lib/interceptor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,13 @@ Interceptor.prototype.matchAddress = function matchAddress(options) {
417417
return comparisonKey === matchKey
418418
}
419419

420-
Interceptor.prototype.filteringPath = function filteringPath() {
421-
if (_.isFunction(arguments[0])) {
422-
this.scope.transformFunction = arguments[0]
423-
}
420+
Interceptor.prototype.filteringPath = function filteringPath(...args) {
421+
this.scope.filteringPath(...args)
424422
return this
425423
}
426424

425+
// filtering by path is valid on the intercept level, but not filtering by request body?
426+
427427
Interceptor.prototype.discard = function discard() {
428428
if ((this.scope.shouldPersist() || this.counter > 0) && this.filePath) {
429429
this.body = fs.createReadStream(this.filePath)

tests/test_intercept.js

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,6 @@ test('reply with callback and filtered path and body', async t => {
199199
scope.done()
200200
})
201201

202-
test('filteringPath with invalid argument throws expected', t => {
203-
t.throws(() => nock('http://example.test').filteringPath('abc123'), {
204-
message:
205-
'Invalid arguments: filtering path should be a function or a regular expression',
206-
})
207-
t.end()
208-
})
209-
210-
test('filteringRequestBody with invalid argument throws expected', t => {
211-
t.throws(() => nock('http://example.test').filteringRequestBody('abc123'), {
212-
message:
213-
'Invalid arguments: filtering request body should be a function or a regular expression',
214-
})
215-
t.end()
216-
})
217-
218202
test('head', async t => {
219203
const scope = nock('http://example.test')
220204
.head('/')
@@ -275,10 +259,12 @@ test('encoding', async t => {
275259
scope.done()
276260
})
277261

278-
test('filter path with function', async t => {
262+
test('on interceptor, filter path with function', async t => {
263+
// Interceptor.filteringPath simply proxies to Scope.filteringPath, this test covers the proxy,
264+
// testing the logic of filteringPath itself is done in test_scope.js.
279265
const scope = nock('http://example.test')
280-
.filteringPath(path => '/?a=2&b=1')
281266
.get('/?a=2&b=1')
267+
.filteringPath(() => '/?a=2&b=1')
282268
.reply(200, 'Hello World!')
283269

284270
const { statusCode } = await got('http://example.test/', {
@@ -289,55 +275,6 @@ test('filter path with function', async t => {
289275
scope.done()
290276
})
291277

292-
test('filter path with regexp', async t => {
293-
const scope = nock('http://example.test')
294-
.filteringPath(/\d/g, '3')
295-
.get('/?a=3&b=3')
296-
.reply(200, 'Hello World!')
297-
298-
const { statusCode } = await got('http://example.test/', {
299-
query: { a: '1', b: '2' },
300-
})
301-
302-
t.equal(statusCode, 200)
303-
scope.done()
304-
})
305-
306-
test('filter body with function', async t => {
307-
let filteringRequestBodyCounter = 0
308-
309-
const scope = nock('http://example.test')
310-
.filteringRequestBody(body => {
311-
++filteringRequestBodyCounter
312-
t.equal(body, 'mamma mia')
313-
return 'mamma tua'
314-
})
315-
.post('/', 'mamma tua')
316-
.reply(200, 'Hello World!')
317-
318-
const { statusCode } = await got('http://example.test/', {
319-
body: 'mamma mia',
320-
})
321-
322-
t.equal(statusCode, 200)
323-
scope.done()
324-
t.equal(filteringRequestBodyCounter, 1)
325-
})
326-
327-
test('filter body with regexp', async t => {
328-
const scope = nock('http://example.test')
329-
.filteringRequestBody(/mia/, 'nostra')
330-
.post('/', 'mamma nostra')
331-
.reply(200, 'Hello World!')
332-
333-
const { statusCode } = await got('http://example.test/', {
334-
body: 'mamma mia',
335-
})
336-
337-
t.equal(statusCode, 200)
338-
scope.done()
339-
})
340-
341278
// TODO Convert to async / got.
342279
test('abort request', t => {
343280
const scope = nock('http://example.test')

tests/test_scope.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { test } = require('tap')
55
const proxyquire = require('proxyquire').noPreserveCache()
66
const Interceptor = require('../lib/interceptor')
77
const nock = require('..')
8+
const got = require('./got_client')
89

910
require('./cleanup_after_each')()
1011

@@ -86,3 +87,82 @@ test('loadDefs throws expected when fs is not available', t => {
8687

8788
t.end()
8889
})
90+
91+
test('filter path with function', async t => {
92+
const scope = nock('http://example.test')
93+
.filteringPath(() => '/?a=2&b=1')
94+
.get('/?a=2&b=1')
95+
.reply(200, 'Hello World!')
96+
97+
const { statusCode } = await got('http://example.test/', {
98+
query: { a: '1', b: '2' },
99+
})
100+
101+
t.equal(statusCode, 200)
102+
scope.done()
103+
})
104+
105+
test('filter path with regexp', async t => {
106+
const scope = nock('http://example.test')
107+
.filteringPath(/\d/g, '3')
108+
.get('/?a=3&b=3')
109+
.reply(200, 'Hello World!')
110+
111+
const { statusCode } = await got('http://example.test/', {
112+
query: { a: '1', b: '2' },
113+
})
114+
115+
t.equal(statusCode, 200)
116+
scope.done()
117+
})
118+
119+
test('filteringPath with invalid argument throws expected', t => {
120+
t.throws(() => nock('http://example.test').filteringPath('abc123'), {
121+
message:
122+
'Invalid arguments: filtering path should be a function or a regular expression',
123+
})
124+
t.end()
125+
})
126+
127+
test('filter body with function', async t => {
128+
let filteringRequestBodyCounter = 0
129+
130+
const scope = nock('http://example.test')
131+
.filteringRequestBody(body => {
132+
++filteringRequestBodyCounter
133+
t.equal(body, 'mamma mia')
134+
return 'mamma tua'
135+
})
136+
.post('/', 'mamma tua')
137+
.reply(200, 'Hello World!')
138+
139+
const { statusCode } = await got('http://example.test/', {
140+
body: 'mamma mia',
141+
})
142+
143+
t.equal(statusCode, 200)
144+
scope.done()
145+
t.equal(filteringRequestBodyCounter, 1)
146+
})
147+
148+
test('filter body with regexp', async t => {
149+
const scope = nock('http://example.test')
150+
.filteringRequestBody(/mia/, 'nostra')
151+
.post('/', 'mamma nostra')
152+
.reply(200, 'Hello World!')
153+
154+
const { statusCode } = await got('http://example.test/', {
155+
body: 'mamma mia',
156+
})
157+
158+
t.equal(statusCode, 200)
159+
scope.done()
160+
})
161+
162+
test('filteringRequestBody with invalid argument throws expected', t => {
163+
t.throws(() => nock('http://example.test').filteringRequestBody('abc123'), {
164+
message:
165+
'Invalid arguments: filtering request body should be a function or a regular expression',
166+
})
167+
t.end()
168+
})

0 commit comments

Comments
 (0)