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

reqheaders does not work when using an undefined requestBody #2545

Open
1 of 2 tasks
josephearl opened this issue Oct 31, 2023 · 3 comments
Open
1 of 2 tasks

reqheaders does not work when using an undefined requestBody #2545

josephearl opened this issue Oct 31, 2023 · 3 comments
Labels

Comments

@josephearl
Copy link

josephearl commented Oct 31, 2023

Please avoid duplicates

Reproducible test case

https://github.com/josephearl/nock-reqheaders-repro

Nock Version

13.3.7

Node Version

v18.17.1

TypeScript Version

N/A

What happened?

When creating a request matcher and using reqheaders, these do not work when setting an undefined requestBody:

mock.get('/some/path', undefined, {
  reqheaders: { 'HEADER1': 'value1', 'HEADER2': 'value1' }
}).reply(200);

Strangely if you use null for the requestBody it does work - but null is not allowed by the TypeScript types which specificy requestBody must be undefined | RequestBody and the RequestBody type does not include null in its union.

Expected is that when using undefined for a requestBody the reqheaders matching still works.

See https://github.com/josephearl/nock-reqheaders-repro/tree/main for a reproducer.

NB: the all open bugs link (https://github.com/nock/nock.js/issues?q=is%3Aissue+is%3Aopen+label%3Abug) in your issue template leads to a 404 - because it is pointing to an old repo name nock.js

Would you be interested in contributing a fix?

  • yes
@josephearl josephearl added the bug label Oct 31, 2023
@gr2m
Copy link
Member

gr2m commented Nov 3, 2023

NB: the all open bugs link (https://github.com/nock/nock.js/issues?q=is%3Aissue+is%3Aopen+label%3Abug) in your issue template leads to a 404 - because it is pointing to an old repo name nock.js

thanks, fixed 👍

@mikicho
Copy link
Contributor

mikicho commented Feb 6, 2024

@gr2m

This is because we ignore the interceptor's headers:

nock/lib/interceptor.js

Lines 68 to 74 in f4d1b15

this.reqheaders = common.headersFieldNamesToLowerCase(
scope.scopeOptions.reqheaders || {},
true,
)
this.badheaders = common.headersFieldsArrayToLowerCase(
scope.scopeOptions.badheaders || [],
)

What's the expected behavior? Should we merge the headers? Do the interceptor's headers take precedence? vice-versa?

P.S: @josephearl I think it worked on null because it failed to match the body.

@gr2m
Copy link
Member

gr2m commented Feb 6, 2024

I don't know out of hand, I'd have to digg into it as well. I'd look at tests first to see if the behavior might be covered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants