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
feat: allow passing a function to enableNetConnect()
#1889
Changes from 4 commits
1eb19ce
2c8b6f8
51810a5
e9bdb0f
9efd292
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 |
---|---|---|
|
@@ -19,8 +19,7 @@ describe('`disableNetConnect()`', () => { | |
|
||
await assertRejects( | ||
got('https://other.example.test/'), | ||
Error, | ||
'Nock: Disallowed net connect for "other.example.test:443/"' | ||
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. This is the bug I mentioned in the PR description - this last parameter is used for displaying a message when the assertion fails, not to check the error itself. |
||
/Nock: Disallowed net connect for "other.example.test:443\/"/ | ||
) | ||
}) | ||
|
||
|
@@ -61,8 +60,7 @@ describe('`enableNetConnect()`', () => { | |
|
||
await assertRejects( | ||
got('https://example.test/'), | ||
Error, | ||
'Nock: Disallowed net connect for "example.test:80/"' | ||
/Nock: Disallowed net connect for "example.test:443\/"/ | ||
) | ||
}) | ||
|
||
|
@@ -88,8 +86,42 @@ describe('`enableNetConnect()`', () => { | |
|
||
await assertRejects( | ||
got('https://example.test/'), | ||
Error, | ||
'Nock: Disallowed net connect for "example.test:80/"' | ||
/Nock: Disallowed net connect for "example.test:443\/"/ | ||
) | ||
}) | ||
|
||
it('enables real HTTP request only for specified domain, via function', async () => { | ||
const onResponse = sinon.spy() | ||
const server = http.createServer((request, response) => { | ||
onResponse() | ||
response.writeHead(200) | ||
response.end() | ||
}) | ||
await new Promise(resolve => server.listen(resolve)) | ||
|
||
nock.enableNetConnect(host => host.includes('ocalhos')) | ||
|
||
await got(`http://localhost:${server.address().port}/`) | ||
expect(onResponse).to.have.been.calledOnce() | ||
|
||
server.close() | ||
}) | ||
|
||
it('disallows request for other domains, via function', async () => { | ||
nock.enableNetConnect(host => host.includes('ocalhos')) | ||
|
||
await assertRejects( | ||
got('https://example.test/'), | ||
/Nock: Disallowed net connect for "example.test:443\/"/ | ||
nikaspran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
}) | ||
|
||
it('passes the domain to be tested, via function', async () => { | ||
const matcher = sinon.stub().returns(false) | ||
nock.enableNetConnect(matcher) | ||
|
||
await got('https://example.test/').catch(() => undefined) // ignore rejection, expected | ||
|
||
expect(matcher).to.have.been.calledOnceWithExactly('example.test:443') | ||
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. 👍 |
||
}) | ||
}) |
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.
I could also make the internal variable a function, but this seemed quite elegant - let me know if you want me to change this.