Skip to content

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

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.

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.

3 participants