Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix Interceptor.filteringPath #1543

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure I'm understanding: this test is of .filteringPath() called on an Interceptor. There's an identical "filter path with function" test of when .filteringPath() is called on a scope, that's being copied to test_scope. Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

.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
Original file line number Diff line number Diff line change
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' },
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are fine for now (and I know you didn't write them, so this is just a thought for the future!

It's hard to tell from these tests what exactly the feature is for. They don't have descriptive titles, and as far as I can tell they are all positive tests – there's nothing that checks what happens when the request doesn't match the filter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought, but didn't want to blow up the scope of this PR.


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()
})