Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

fix: issue where application/x-www-form-urlencoded payloads weren't being handled properly #807

Merged
merged 5 commits into from
Jun 30, 2020

Conversation

domharrington
Copy link
Member

@domharrington domharrington commented Jun 26, 2020

🧰 What's being changed?

This resolves a couple bugs with how we're generating HAR definitions:

  • For application/x-www-form-urlencoded operations, we were storing their payloads inside of postData.text instead of postData.params.
  • We were always sending postData regardless if there was actually any data for it. For simple GET requests that just have queryString populated, the postData object is no longer part of the created HAR.
  • The HAR definitions that oas-to-har was creating didn't actually validate outside of httpsnippet. This was because that library adds some defaults (like httpVersion) if they're missing. Since oas-to-har should support the HAR spec and not HTTP Snippets handling of it, I've made the decision to:
    • Add those missing defaults to generated definitions.
    • Update the unit test suite to assert that we're always generating valid HAR definitions when running our other assertions.

🧪 Testing

You can see this broken in action here: http://bin.readme.com/s/5efbb6ac35bcd40024d60488

Screen Shot 2020-06-30 at 3 04 39 PM

Load up the local server and the formData example to see how it's handled how: http://localhost:9966/?selected=swagger-files%2Fformdata.json

Screen Shot 2020-06-30 at 3 04 48 PM

🗳 Checklist

💡 If answering yes to any of the following, include additional info, before/after links, screenshots, etc. where appropriate!

  • 🆕 I'm adding something new!
  • 🐛 I'm fixing a bug!
  • 📸 I've made some changes to the UI!

I added two test cases: one for json and one for formData. The json
one passes fine! This code in oas-to-har is working as expected:

har.postData.text = querystring.stringify(formData.formData);

I narrowed down the issue to here:
https://github.com/Kong/httpsnippet/blob/633b825364ab050f02556cfa837148a1e136fd69/src/index.js#L126-L128
httpsnippet is setting it from the value we set back to ""

I'm not sure if this is something that's changed recently in the way
we're generating hars? This part of httpsnippet hasn't been updated
in 5 years. Gunna defer to Jon for this.

I added two test cases: one for json and one for formData. The json
one passes fine! This code in oas-to-har is working as expected:
https://github.com/readmeio/api-explorer/blob/59a5d060997321936ae356f2ba3f36e34c81c520/packages/oas-to-har/src/index.js#L198

I narrowed down the issue to here:
https://github.com/Kong/httpsnippet/blob/633b825364ab050f02556cfa837148a1e136fd69/src/index.js#L126-L128
httpsnippet is setting it from the value we set back to ""

I'm not sure if this is something that's changed recently in the way
we're generating hars? This part of httpsnippet hasn't been updated
in 5 years. Gunna defer to Jon for this.
@erunion
Copy link
Member

erunion commented Jun 26, 2020

What's the issue? I made some changes to HAR generation in the SDK work to pass Content-Type headers through so JSON objects are dumped in a snippets as objects, not stringified.

@domharrington
Copy link
Member Author

Sorry I should've been clearer. Form data values in the form are not coming through into the code sample!

image

@erunion erunion added area:explorer type:bug Something isn't working labels Jun 29, 2020
@erunion erunion changed the title test: added failing test case for formData issue fix: issue where application/x-www-form-urlencoded payloads weren't being handled properly Jun 30, 2020
@erunion erunion added the type:refactor Issues about tackling technical debt label Jun 30, 2020
@erunion
Copy link
Member

erunion commented Jun 30, 2020

Tests will pass when readmeio/fetch-har#67 is pulled into a new fetch-har release.

@erunion erunion marked this pull request as ready for review June 30, 2020 22:05
@erunion erunion requested a review from kanadgupta June 30, 2020 22:06
@@ -1,4 +1,4 @@
const querystring = require('querystring');
const validate = require('har-validator');
Copy link
Member

Choose a reason for hiding this comment

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

FYI, most of the changes to this file were to rewrite some tests into using it.each to make them easier to maintain.

Copy link
Member Author

@domharrington domharrington left a comment

Choose a reason for hiding this comment

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

Code lgtm! Any idea what caused the regression?

test('should output a har format', () => {
expect(oasToHar(oas)).toStrictEqual({
expect.extend({
toBeAValidHAR(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.

🤯

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 tempted to rip this out into its own package.

it('should use example if no value', () => {
expect(
oasToHar(oas, {
it.each([
Copy link
Member Author

Choose a reason for hiding this comment

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

👌

@@ -1157,7 +1048,7 @@ describe('requestBody', () => {
},
},
}).log.entries[0].request.postData.text
).toBe(querystring.stringify({ a: 'value' }));
).toBe(JSON.stringify({ a: 'value' }));
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this correct? How come it's change from querystring to JSON?

Copy link
Member

@erunion erunion Jun 30, 2020

Choose a reason for hiding this comment

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

We were only ever using the querystring module for compiling postData.text for formData, but since aren't using that for that anymore we no longer need to pull that module in. Since the other cases within the library that are compiling postData.text use JSON.stringify() I moved this instance over as well.

That said, this test is still not quite right because it's looking for postData.text for a formData case, but since the test is skipped right now I'm not going to worry about it right now.

@erunion
Copy link
Member

erunion commented Jun 30, 2020

@domharrington Yeah it seems like it was a result of me adding setting postData.mimeType to application/x-www-form-urlencoded with the node-simple work in https://github.com/readmeio/api-explorer/pull/792/files#diff-528f99451eda35186e98d5f772a60fd1R182, and we didn't have sufficient tests for formData anywhere else.

@erunion erunion merged commit c08ca55 into master Jun 30, 2020
@erunion erunion deleted the bug/formdata-code-samples branch June 30, 2020 23:39
erunion added a commit that referenced this pull request Jul 1, 2020
…eing handled properly (#807)

* test: added failing test case for formData issue

I added two test cases: one for json and one for formData. The json
one passes fine! This code in oas-to-har is working as expected:
https://github.com/readmeio/api-explorer/blob/59a5d060997321936ae356f2ba3f36e34c81c520/packages/oas-to-har/src/index.js#L198

I narrowed down the issue to here:
https://github.com/Kong/httpsnippet/blob/633b825364ab050f02556cfa837148a1e136fd69/src/index.js#L126-L128
httpsnippet is setting it from the value we set back to ""

I'm not sure if this is something that's changed recently in the way
we're generating hars? This part of httpsnippet hasn't been updated
in 5 years. Gunna defer to Jon for this.

* fix: correctly preparing `x-www-form-urlencoded` forms as params

* fix: oas-to-har will now output valid HAR definitions

* chore(deps): upgrading fetch-har to 3.0.0

* docs: adding an example for our handling of formData

Co-authored-by: Jon Ursenbach <jon@ursenba.ch>
erunion added a commit that referenced this pull request Jul 1, 2020
* fix: issue where application/x-www-form-urlencoded payloads weren't being handled properly (#807)

* test: added failing test case for formData issue

I added two test cases: one for json and one for formData. The json
one passes fine! This code in oas-to-har is working as expected:
https://github.com/readmeio/api-explorer/blob/59a5d060997321936ae356f2ba3f36e34c81c520/packages/oas-to-har/src/index.js#L198

I narrowed down the issue to here:
https://github.com/Kong/httpsnippet/blob/633b825364ab050f02556cfa837148a1e136fd69/src/index.js#L126-L128
httpsnippet is setting it from the value we set back to ""

I'm not sure if this is something that's changed recently in the way
we're generating hars? This part of httpsnippet hasn't been updated
in 5 years. Gunna defer to Jon for this.

* fix: correctly preparing `x-www-form-urlencoded` forms as params

* fix: oas-to-har will now output valid HAR definitions

* chore(deps): upgrading fetch-har to 3.0.0

* docs: adding an example for our handling of formData

Co-authored-by: Jon Ursenbach <jon@ursenba.ch>

* build: updating dist files

* v6.12.1

Co-authored-by: domharrington <domharrington@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type:bug Something isn't working type:refactor Issues about tackling technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants