Skip to content

Commit

Permalink
fix: Restore behavior of Interceptor.filteringPath (#1543)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mastermatt authored and paulmelnikow committed May 7, 2019
1 parent 6a26f41 commit 560a5d8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 71 deletions.
8 changes: 4 additions & 4 deletions lib/interceptor.js
Expand Up @@ -417,13 +417,13 @@ Interceptor.prototype.matchAddress = function matchAddress(options) {
return comparisonKey === matchKey
}

Interceptor.prototype.filteringPath = function filteringPath() {
if (_.isFunction(arguments[0])) {
this.scope.transformFunction = arguments[0]
}
Interceptor.prototype.filteringPath = function filteringPath(...args) {
this.scope.filteringPath(...args)
return this
}

// filtering by path is valid on the intercept level, but not filtering by request body?

Interceptor.prototype.discard = function discard() {
if ((this.scope.shouldPersist() || this.counter > 0) && this.filePath) {
this.body = fs.createReadStream(this.filePath)
Expand Down
71 changes: 4 additions & 67 deletions tests/test_intercept.js
Expand Up @@ -199,22 +199,6 @@ test('reply with callback and filtered path and body', async t => {
scope.done()
})

test('filteringPath with invalid argument throws expected', t => {
t.throws(() => nock('http://example.test').filteringPath('abc123'), {
message:
'Invalid arguments: filtering path should be a function or a regular expression',
})
t.end()
})

test('filteringRequestBody with invalid argument throws expected', t => {
t.throws(() => nock('http://example.test').filteringRequestBody('abc123'), {
message:
'Invalid arguments: filtering request body should be a function or a regular expression',
})
t.end()
})

test('head', async t => {
const scope = nock('http://example.test')
.head('/')
Expand Down Expand Up @@ -275,10 +259,12 @@ test('encoding', async t => {
scope.done()
})

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

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

test('filter path with regexp', async t => {
const scope = nock('http://example.test')
.filteringPath(/\d/g, '3')
.get('/?a=3&b=3')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
query: { a: '1', b: '2' },
})

t.equal(statusCode, 200)
scope.done()
})

test('filter body with function', async t => {
let filteringRequestBodyCounter = 0

const scope = nock('http://example.test')
.filteringRequestBody(body => {
++filteringRequestBodyCounter
t.equal(body, 'mamma mia')
return 'mamma tua'
})
.post('/', 'mamma tua')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
body: 'mamma mia',
})

t.equal(statusCode, 200)
scope.done()
t.equal(filteringRequestBodyCounter, 1)
})

test('filter body with regexp', async t => {
const scope = nock('http://example.test')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
body: 'mamma mia',
})

t.equal(statusCode, 200)
scope.done()
})

// TODO Convert to async / got.
test('abort request', t => {
const scope = nock('http://example.test')
Expand Down
80 changes: 80 additions & 0 deletions tests/test_scope.js
Expand Up @@ -5,6 +5,7 @@ const { test } = require('tap')
const proxyquire = require('proxyquire').noPreserveCache()
const Interceptor = require('../lib/interceptor')
const nock = require('..')
const got = require('./got_client')

require('./cleanup_after_each')()

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

t.end()
})

test('filter path with function', async t => {
const scope = nock('http://example.test')
.filteringPath(() => '/?a=2&b=1')
.get('/?a=2&b=1')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
query: { a: '1', b: '2' },
})

t.equal(statusCode, 200)
scope.done()
})

test('filter path with regexp', async t => {
const scope = nock('http://example.test')
.filteringPath(/\d/g, '3')
.get('/?a=3&b=3')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
query: { a: '1', b: '2' },
})

t.equal(statusCode, 200)
scope.done()
})

test('filteringPath with invalid argument throws expected', t => {
t.throws(() => nock('http://example.test').filteringPath('abc123'), {
message:
'Invalid arguments: filtering path should be a function or a regular expression',
})
t.end()
})

test('filter body with function', async t => {
let filteringRequestBodyCounter = 0

const scope = nock('http://example.test')
.filteringRequestBody(body => {
++filteringRequestBodyCounter
t.equal(body, 'mamma mia')
return 'mamma tua'
})
.post('/', 'mamma tua')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
body: 'mamma mia',
})

t.equal(statusCode, 200)
scope.done()
t.equal(filteringRequestBodyCounter, 1)
})

test('filter body with regexp', async t => {
const scope = nock('http://example.test')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply(200, 'Hello World!')

const { statusCode } = await got('http://example.test/', {
body: 'mamma mia',
})

t.equal(statusCode, 200)
scope.done()
})

test('filteringRequestBody with invalid argument throws expected', t => {
t.throws(() => nock('http://example.test').filteringRequestBody('abc123'), {
message:
'Invalid arguments: filtering request body should be a function or a regular expression',
})
t.end()
})

0 comments on commit 560a5d8

Please sign in to comment.