Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: adding a new harIsAlreadyEncoded option #46

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

Merged
merged 4 commits into from
Sep 9, 2021

Conversation

erunion
Copy link
Member

@erunion erunion commented Sep 8, 2021

With the work done in readmeio/oas-to-har#17 to ensure that query parameters are always properly encoded it was discovered that cookie parameters were being double encoded: once in oas-to-har and again here. Since this is obviously bad, I've refactored our existing custom escapeQueryStrings option to be harIsAlreadyEncoded (defaulting to false) so that we can now control both query string and cookie encoding behavior.

In the process of doing this I've slightly refactored a couple targets to no longer do their own cookie encoding as it's already being done in the HTTPSnippet constructor when it builds up a list of allHeaders. This refactor also allows us to not have to pass an option to HTTPSnippet and also to the target itself to control encoding handling because the root HTTPSnippet class does not currently have any way of passing down options to targets.

📶  Since this option is now more generalized than our previous escapeQueryStrings option was I plan on eventually submitting this work upstream.

@erunion erunion added the enhancement New feature or request label Sep 8, 2021
@@ -1,7 +1,7 @@
const fetch = require('node-fetch');

const url = 'http://mockbin.com/har';
const options = {method: 'POST', headers: {cookie: 'foo=bar; bar=baz; '}};
Copy link
Member Author

Choose a reason for hiding this comment

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

Cool side effect of this work is now cookies for node-fetch won't have an unnecessary space at the end.

if (Object.keys(source.headersObj).length) {
reqOpts.headers = source.headersObj
if (Object.keys(source.allHeaders).length) {
reqOpts.headers = source.allHeaders
Copy link
Member

Choose a reason for hiding this comment

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

What are the implications of consuming allHeaders instead of headersObj?

Copy link
Member Author

Choose a reason for hiding this comment

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

allHeaders contains the cookie header if it's present, headersObj is a k/v store of headers from the HAR excluding cookie because that's dynamically generated from the cookies array in the HAR.

Copy link
Member Author

Choose a reason for hiding this comment

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

And by changing this to allHeaders this target no longer needs to combine its own cookie data into a cookie header because that's already been done in src/index.js.

Copy link
Member

Choose a reason for hiding this comment

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

Love it, thanks for the explanation!

@erunion erunion merged commit 4b0a3f3 into master Sep 9, 2021
@erunion erunion deleted the feat/cookie-encoding-options branch September 9, 2021 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants