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

feat: dropping support for non-native FormData implementations #199

Merged
merged 6 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

"unicorn/prefer-node-protocol": "error",

"vitest/require-hook": "off"
"vitest/require-hook": ["error", {
"allowedFunctionCalls": ["runCustomFixtures"]
}]
},
"overrides": [
{
Expand Down
1 change: 0 additions & 1 deletion .sink.d.ts

This file was deleted.

10 changes: 8 additions & 2 deletions .vscode/settings.json
Copy link
Member

Choose a reason for hiding this comment

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

Is this file here because they use it upstream? This feels like something that should just be in the prettier config

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah these snippets have all sorts of frustrating line endings, that are handled in .editorconfig, that i fought the upstream maintainers on but they didn't relent.

Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"files.insertFinalNewline": false, // controlled by the .editorconfig at root since we can't map vscode settings directly to files (see: https://github.com/microsoft/vscode/issues/35350)
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.codeActionsOnSave": {
"source.fixAll": true
},
"editor.defaultFormatter": "esbenp.prettier-vscode",

// controlled by the .editorconfig at root since we can't map vscode settings directly to files
// https://github.com/microsoft/vscode/issues/35350
"files.insertFinalNewline": false
}
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Type: `object`

Available options:

* `harIsAlreadyEncoded` (`boolean`): In the event of you supplying a `source` HAR that already contains escaped data (query and cookie parameters)strings, this allows you to disable automatic encoding of those parameters to prevent them from being double-escaped.
- `harIsAlreadyEncoded` (`boolean`): In the event of you supplying a `source` HAR that already contains escaped data (query and cookie parameters)strings, this allows you to disable automatic encoding of those parameters to prevent them from being double-escaped.

### convert(target [, options])

Expand All @@ -63,6 +63,8 @@ const snippet = new HTTPSnippet({
url: 'httsp://httpbin.org/anything',
});

await snippet.init();

// generate Node.js: Native output
console.log(snippet.convert('node'));

Expand Down Expand Up @@ -102,6 +104,8 @@ const snippet = new HTTPSnippet({
url: 'https://httpbin.org/anything',
});

await snippet.init();

// generate Shell: cURL output
console.log(
snippet.convert('shell', 'curl', {
Expand Down Expand Up @@ -155,32 +159,31 @@ For detailed information on each target, please review the [wiki](https://github

There are some major differences between this library and the [httpsnippet](https://github.com/Kong/httpsnippet) upstream:

* Includes a full integration test suite for a handful of clients and targets.
* Does not ship with a CLI component.
* Does not do any HAR schema validation. It's just assumed that the HAR you're supplying to the library is already valid.
* The main `HTTPSnippet` export contains an `options` argument for an `harIsAlreadyEncoded` option for disabling [escaping](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) of cookies and query strings in URLs.
* We added this because all HARs that we interact with already have this data escaped and this option prevents them from being double encoded, thus corrupting the data.
* Does not support the `insecureSkipVerify` option on `go:native`, `node:native`, `ruby:native`, and `shell:curl` as we don't want snippets generated for our users to bypass SSL certificate verification.
* Node
* `fetch`
* Body payloads are treated as an object literal and wrapped within `JSON.stringify()`. We do this to keep those targets looking nicer with those kinds of payloads. This also applies to the JS `fetch` target as well.
* `request`
* Does not provide query string parameters in a `params` argument due to complexities with query encoding.
* PHP
* `guzzle`
* Snippets have `require_once('vendor/autoload.php');` prefixed at the top.
* Python
* `python3`
* Does not ship this client due to its incompatibility with being able to support file uploads.
* `requests`
* Does not provide query string parameters in a `params` argument due to complexities with query encoding.
- Includes a full integration test suite for a handful of clients and targets.
- Does not ship with a CLI component.
- Does not do any HAR schema validation. It's just assumed that the HAR you're supplying to the library is already valid.
- The main `HTTPSnippet` export contains an `options` argument for an `harIsAlreadyEncoded` option for disabling [escaping](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) of cookies and query strings in URLs.
- We added this because all HARs that we interact with already have this data escaped and this option prevents them from being double encoded, thus corrupting the data.
- Does not support the `insecureSkipVerify` option on `go:native`, `node:native`, `ruby:native`, and `shell:curl` as we don't want snippets generated for our users to bypass SSL certificate verification.
- Node
- `fetch`
- Body payloads are treated as an object literal and wrapped within `JSON.stringify()`. We do this to keep those targets looking nicer with those kinds of payloads. This also applies to the JS `fetch` target as well.
- `request`
- Does not provide query string parameters in a `params` argument due to complexities with query encoding.
- PHP
- `guzzle`
- Snippets have `require_once('vendor/autoload.php');` prefixed at the top.
- Python
- `python3`
- Does not ship this client due to its incompatibility with being able to support file uploads.
- `requests`
- Does not provide query string parameters in a `params` argument due to complexities with query encoding.

## License

[MIT](LICENSE) © [Kong](https://konghq.com)

[license-url]: https://github.com/Kong/httpsnippet/blob/master/LICENSE

[npm-url]: https://www.npmjs.com/package/@readme/httpsnippet
[npm-license]: https://img.shields.io/npm/l/@readme/httpsnippet.svg?style=flat-square
[npm-version]: https://img.shields.io/npm/v/@readme/httpsnippet.svg?style=flat-square
95 changes: 37 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"prebuild": "npm run clean",
"prepack": "npm run build",
"pretest": "npm run lint",
"prettier": "prettier --list-different --write \"./**/**.{js,ts}\"",
"prettier": "prettier --list-different --write \"./**/**.{md,cjs,js,ts}\"",
erunion marked this conversation as resolved.
Show resolved Hide resolved
"test": "vitest --coverage"
},
"devDependencies": {
Expand All @@ -95,8 +95,7 @@
"vitest": "^0.34.4"
},
"dependencies": {
"form-data": "^4.0.0",
"map-stream": "^0.0.7",
"formdata-to-string": "^1.0.1",
"qs": "^6.11.2",
"stringify-object": "^3.3.0"
},
Expand Down
15 changes: 8 additions & 7 deletions src/fixtures/runCustomFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ export interface CustomFixture {

export const runCustomFixtures = ({ targetId, clientId, tests }: CustomFixture) => {
describe(`custom fixtures for ${targetId}:${clientId}`, () => {
tests.forEach(({ it: title, expected: fixtureFile, options, input: request }) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Had to restructure all of these tests into formal describe.each and it.each blocks because with the async change Vitest wasn't picking them up as they were getting invoked after Vitest was expecting them to because it was running an async test inside of a non-async foreach loop.

it.each(tests.map(t => [t.it, t]))('%s', async (_, { expected: fixtureFile, options, input: request }) => {
const opts: HTTPSnippetOptions = {};
if (options.harIsAlreadyEncoded) {
opts.harIsAlreadyEncoded = options.harIsAlreadyEncoded;
}

const result = new HTTPSnippet(request, opts).convert(targetId, clientId, options);
const snippet = new HTTPSnippet(request, opts);
await snippet.init();

const result = snippet.convert(targetId, clientId, options);
const filePath = path.join(__dirname, '..', 'targets', targetId, clientId, 'fixtures', fixtureFile);
if (process.env.OVERWRITE_EVERYTHING) {
writeFileSync(filePath, String(result));
}

it(title, async () => {
const buffer = await readFile(filePath);
const fixture = String(buffer);
const buffer = await readFile(filePath);
const fixture = String(buffer);

expect(result).toStrictEqual(fixture);
});
expect(result).toStrictEqual(fixture);
});
});
};
Loading