-
-
Notifications
You must be signed in to change notification settings - Fork 736
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
Conversation
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.
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.
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.
tests/test_common.js
Outdated
path: '/path/1', | ||
headers: { cookie: 'fiz=baz' }, | ||
} | ||
test('stringifyRequest', async t => { |
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.
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()
.
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.
These have been flattened
throw Error('headers must be an object') | ||
} | ||
|
||
if (!_.isString(fieldNameToDelete)) { |
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.
Could you perform this type check using plain JS?
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 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.
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'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
.
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.
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]) |
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.
👍
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.
Good to merge when this last comment is addressed!
tests/test_common.js
Outdated
@@ -339,3 +448,7 @@ test('headersArrayToObject', function(t) { | |||
|
|||
t.end() | |||
}) | |||
|
|||
test('percentEncode encodes extra reserved characters', async t => { |
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.
This one needs updating too.
🎉 This PR is included in version 11.0.0-beta.20 🎉 The release is available on: Your semantic-release bot 📦🚀 |
🎉 This PR is included in version 11.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
#### 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
#### 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
#### 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
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.