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
Merged
mastermatt
merged 5 commits into
nock:beta
from
nikaspran:1861/enableNetConnect-function
Feb 11, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1eb19ce
feat(1861): allow passing a function to enableNetConnect
nikaspran 2c8b6f8
lint: run prettier
nikaspran 51810a5
Use typeof instead of instanceof (PR #1850)
nikaspran e9bdb0f
Add test to cover passed parameters
nikaspran 9efd292
Merge branch 'beta' into 1861/enableNetConnect-function
mastermatt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,4 +89,39 @@ describe('`enableNetConnect()`', () => { | |
/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. 👍 |
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.