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

Add tests to increase coverage of the Request Overrider. #1577

Merged
merged 9 commits into from Jun 2, 2019

Conversation

mastermatt
Copy link
Member

This, along with another PR I have for headers, will take lib/request_overrider.js to 100% branch coverage.

For #1404

This check was originally added when fixing a bug related to aborted
requests after calling `delayConnection`.
28f6b29 "abort behaves. somehow addresses nock#439"
Returning early from this function was moved to the top with 28f6b29,
the original check was left behind.
Because of the way we override `http.request`, this callback is only
defined when `http.ClientRequest` is called directly with a callback.
In the normal error case, that the value of response body is a string, but
not hex encoded, the attempt to create a Buffer will return a Uint8Array
with nothing in it.

Looking at the [Node source for Buffer.from](https://github.com/nodejs/node/blob/2817fde844de39b0e8c17c352433ee6124b5ef09/lib/buffer.js#L196), the reason errors are
thrown are:
- The first arg isn't a value type, but we've already ensured it's a string.
- The encoding arg is not valid, but we hard code 'hex'.
Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Left you a few comments.

http.get(reqOpts, res => {
res.on('error', err => {
t.is(err, 'oh no!')
t.done()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.reply(201, (path, requestBody, callback) => {
callback(null, 'one')
callback(null, 'two')
callback(null, 'three')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a t.ok() here with a t.plan() to make sure we get all the way through this function before the test finishes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

const scope = nock('http://example.test')
.get('/')
.reply((path, requestBody, callback) => {
callback(Error('boom'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

tests/test_reply_function_async.js Outdated Show resolved Hide resolved
t.match(
err,
Error(
'Response delay of the body is currently not supported with content-encoded responses.'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// TODO-coverage: Add a test of this error case.
debug(
'exception during Buffer construction from hex data:',
responseBody,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Member Author

@mastermatt mastermatt Jun 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I broke out each change into separate commits with descriptions in the messages for the non-trivial ones.
de1d60a


In the normal error case, that the value of response body is a string, but
not hex encoded, the attempt to create a Buffer will return a Uint8Array
with nothing in it.

Looking at the Node source for Buffer.from, the reason errors are
thrown are:

  • The first arg isn't a valid type, but we've already ensured it's a string.
  • The encoding arg is not valid, but we hardcoded 'hex'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

mastermatt and others added 2 commits June 2, 2019 10:26
Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@mastermatt
Copy link
Member Author

@paulmelnikow I think this my first pr since becoming a maintainer, what's the protocol for determining when a PR can be merged?

@paulmelnikow
Copy link
Member

Generally we're looking for one 👍 review from another maintainer unless you feel extra discussion is warranted.

It took me a while to get the hang of conventional commit messages; feel free to ping me or @gr2m if you need help determining what to use.

For this I'd probably write refactor(request_overrider): Remove unreachable code and increase coverage

@mastermatt mastermatt merged commit 9883783 into nock:beta Jun 2, 2019
@mastermatt mastermatt deleted the fix-todo/request-overrider branch June 2, 2019 18:03
@nockbot
Copy link
Collaborator

nockbot commented Jun 8, 2019

🎉 This PR is included in version 11.0.0-beta.18 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nockbot
Copy link
Collaborator

nockbot commented Aug 12, 2019

🎉 This PR is included in version 11.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

3 participants