-
-
Notifications
You must be signed in to change notification settings - Fork 733
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' }, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
}) |
There was a problem hiding this comment.
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 totest_scope
. Is that right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct.