From 560a5d863095c6a860914c46b2c5eab8e42fdade Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Tue, 7 May 2019 08:06:00 -0600 Subject: [PATCH] fix: Restore behavior of Interceptor.filteringPath (#1543) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lib/interceptor.js | 8 ++--- tests/test_intercept.js | 71 +++--------------------------------- tests/test_scope.js | 80 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 71 deletions(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index a60682c5b..7efa74f12 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -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) diff --git a/tests/test_intercept.js b/tests/test_intercept.js index 29162b215..f71ea1985 100644 --- a/tests/test_intercept.js +++ b/tests/test_intercept.js @@ -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('/') @@ -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/', { @@ -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') diff --git a/tests/test_scope.js b/tests/test_scope.js index 4179e79f3..2df3ddab2 100644 --- a/tests/test_scope.js +++ b/tests/test_scope.js @@ -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')() @@ -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() +})