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

test: Bring common.js up to 100% coverage #1583

Merged
merged 6 commits into from
Jun 18, 2019

Conversation

mastermatt
Copy link
Member

For #1404

The changes for each of the three functions that were tested and/or refactored are broken up into separate commits to ease review.

Investigated possible, legit argument values.
Adjusted the function to have clear defaults and remove unreachable code.
Note that the only two existing tests both passed non-string values for the `body` arg when function is only ever explicitly passed a string.
While it’s not currently possible for this util to get invalid args from current callers,
type checks were added per the TODO comment.

Refactored the “meat” of the logic to use native JS.

Added tests for invalid arg flows as well as verifying duplicate field removal.
The new test covers cases of characters that are reserved in RFC 3986 but not encoded by `encodeURIComponent`.

Added JSDoc with info and refs.

Further discussion can be found on the original issue nock#416 and the commit that added the logic 9bd8946
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 on this! 🙌

When it's time to merge, it probably should be marked as a refactor. It goes beyond tests, and I think "test" does not trigger a semantic release.

path: '/path/1',
headers: { cookie: 'fiz=baz' },
}
test('stringifyRequest', async t => {
Copy link
Member

Choose a reason for hiding this comment

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

At @gr2m's suggestion I've been trying to keep the tap subtest trees flat as much as possible. Do you mind updating these?

Also if the functions under test aren't async, I think it's clearer to make the test functions synchronous and use t.end().

Copy link
Member Author

Choose a reason for hiding this comment

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

These have been flattened

throw Error('headers must be an object')
}

if (!_.isString(fieldNameToDelete)) {
Copy link
Member

Choose a reason for hiding this comment

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

Could you perform this type check using plain JS?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can.
I'm still not sure when we want to be using Lodash or not.
For this particular case, because _.isString is technically more robust than typeof var === 'string', I thought it would be a good candidate to keep using Lodash.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with that for now. Ultimately I'd lean toward removing it entirely, though type checking is an area where it's not clear to me whether we need something more robust than typeof.

Copy link
Member Author

Choose a reason for hiding this comment

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

The issue with string is

var stringValue = "This is a String";
var stringObject = new String("This is a String Object");
typeof stringValue === "string"
typeof stringObject === "object"

The "proper" native js check would be

typeof value === 'string' || value instanceof String

// Search through the headers and delete all values whose field name matches the given field name.
Object.keys(headers)
.filter(fieldName => fieldName.toLowerCase() === lowerCaseFieldNameToDelete)
.forEach(fieldName => delete headers[fieldName])
Copy link
Member

Choose a reason for hiding this comment

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

👍

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.

Good to merge when this last comment is addressed!

@@ -339,3 +448,7 @@ test('headersArrayToObject', function(t) {

t.end()
})

test('percentEncode encodes extra reserved characters', async t => {
Copy link
Member

Choose a reason for hiding this comment

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

This one needs updating too.

@mastermatt mastermatt merged commit 9472ad0 into nock:beta Jun 18, 2019
@mastermatt mastermatt deleted the coverage/common branch June 18, 2019 16:08
@nockbot
Copy link
Collaborator

nockbot commented Jun 20, 2019

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

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 📦🚀

gr2m pushed a commit that referenced this pull request Sep 4, 2019
#### Refactor and add tests for `stringifyRequest`
Investigated possible, legit argument values.
Adjusted the function to have clear defaults and remove unreachable code.
Note that the only two existing tests both passed non-string values for the `body` arg when function is only ever explicitly passed a string.

#### Refactor and add tests for `deleteHeadersField`
While it’s not currently possible for this util to get invalid args from current callers,
type checks were added per the TODO comment.
Refactored the “meat” of the logic to use native JS.
Added tests for invalid arg flows as well as verifying duplicate field removal.

#### Add test for `percentEncode`
The new test covers cases of characters that are reserved in RFC 3986 but not encoded by `encodeURIComponent`.
Added JSDoc with info and refs.
Further discussion can be found on the original issue #416 and the commit that added the logic 9bd8946
gr2m pushed a commit that referenced this pull request Sep 4, 2019
#### Refactor and add tests for `stringifyRequest`
Investigated possible, legit argument values.
Adjusted the function to have clear defaults and remove unreachable code.
Note that the only two existing tests both passed non-string values for the `body` arg when function is only ever explicitly passed a string.

#### Refactor and add tests for `deleteHeadersField`
While it’s not currently possible for this util to get invalid args from current callers,
type checks were added per the TODO comment.
Refactored the “meat” of the logic to use native JS.
Added tests for invalid arg flows as well as verifying duplicate field removal.

#### Add test for `percentEncode`
The new test covers cases of characters that are reserved in RFC 3986 but not encoded by `encodeURIComponent`.
Added JSDoc with info and refs.
Further discussion can be found on the original issue #416 and the commit that added the logic 9bd8946
gr2m pushed a commit that referenced this pull request Sep 5, 2019
#### Refactor and add tests for `stringifyRequest`
Investigated possible, legit argument values.
Adjusted the function to have clear defaults and remove unreachable code.
Note that the only two existing tests both passed non-string values for the `body` arg when function is only ever explicitly passed a string.

#### Refactor and add tests for `deleteHeadersField`
While it’s not currently possible for this util to get invalid args from current callers,
type checks were added per the TODO comment.
Refactored the “meat” of the logic to use native JS.
Added tests for invalid arg flows as well as verifying duplicate field removal.

#### Add test for `percentEncode`
The new test covers cases of characters that are reserved in RFC 3986 but not encoded by `encodeURIComponent`.
Added JSDoc with info and refs.
Further discussion can be found on the original issue #416 and the commit that added the logic 9bd8946
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.

3 participants