-
-
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
fix: Fix .matchHeader()
with allowUnmocked
#1480
Changes from 6 commits
d2f41e5
6471ec7
70da46a
a1eda82
c068380
307b594
147be10
499a9d7
5e0dc68
181e9e9
7a98cef
1a68c69
2876c96
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 |
---|---|---|
|
@@ -82,7 +82,7 @@ test('match headers on number with regexp', async t => { | |
}) | ||
|
||
test('match header on scope with function: gets the expected argument', async t => { | ||
t.plan(3) | ||
t.plan(4) // The check in matchHeader should run twice | ||
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. Hmm, is there a reason it should? It seems confusing for the check to run more than once per request. 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 ran the debug and what is happening is it is called once from 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. Okay. I can dig into it a bit more and see if I can get all the tests passing with the function only being called once. Ideally I'd like to fix that before merging because I don't think there's a good reason to evaluate it twice and it seems confusing for the user. 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. So 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. Gotcha. Thank you so much for looking into it this far! I can pick it up from here. Since the headers aren't part of the body, it seems like the check belongs in 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. You can't only check in the first because there might be I rejecting body filter. So you could only check in the second method when you have all the data. As an alternative, you could store the results of matchIndependant... on the object and then use the result in the match call rather than rerunning all the checks. As a contributor I do find the separate matches confusing so executing the checks once would read better, but, it was so odd I assumed it was done for good reason. 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. If possible you could also consider merging this change in as is and addressing the extra call separately. We would trade the duplicate call for working header matching. |
||
|
||
const scope = nock('http://example.test') | ||
.get('/') | ||
|
@@ -118,6 +118,21 @@ test('match header on scope with function: matches when match accepted', async t | |
scope.done() | ||
}) | ||
|
||
test('match header on scope with function and allow unmocked: matches when match accepted', async t => { | ||
const scope = nock('http://example.test', { allowUnmocked: true }) | ||
.get('/') | ||
.matchHeader('x-my-headers', val => true) | ||
.reply(200, 'Hello World!') | ||
|
||
const { statusCode, body } = await got('http://example.test/', { | ||
headers: { 'X-My-Headers': 456 }, | ||
}) | ||
|
||
t.equal(statusCode, 200) | ||
t.equal(body, 'Hello World!') | ||
scope.done() | ||
}) | ||
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. 👍 |
||
|
||
test('match header on scope with function: does not match when match declined', async t => { | ||
nock('http://example.test') | ||
.get('/') | ||
|
@@ -152,7 +167,7 @@ test('match header on scope with function: does not consume mock request when ma | |
}) | ||
|
||
test('match header on intercept with function: gets the expected argument', async t => { | ||
t.plan(3) | ||
t.plan(4) // The check in matchHeader should run twice | ||
|
||
const scope = nock('http://example.test') | ||
.matchHeader('x-my-headers', val => { | ||
|
@@ -192,6 +207,23 @@ test('match header on interceptor with function: matches when match accepted', a | |
scope.done() | ||
}) | ||
|
||
test('match header on interceptor with function: matches when match accepted', async t => { | ||
const scope = nock('http://example.test', { allowUnmocked: true }) | ||
.matchHeader('x-my-headers', val => true) | ||
// `.matchHeader()` is called on the interceptor. It precedes the call to | ||
// `.get()`. | ||
.get('/') | ||
.reply(200, 'Hello World!') | ||
|
||
const { statusCode, body } = await got('http://example.test/', { | ||
headers: { 'X-My-Headers': 456 }, | ||
}) | ||
|
||
t.equal(statusCode, 200) | ||
t.equal(body, 'Hello World!') | ||
scope.done() | ||
}) | ||
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. 👍 |
||
|
||
test('match header on interceptor with function: does not match when match declined', async t => { | ||
nock('http://example.test') | ||
.matchHeader('x-my-headers', val => false) | ||
|
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.
This looks great!