Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions src/helpers/query-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ReducedHelperObject } from './reducer';

/**
* Given a query params object return a flattened query string.
*/
export const getQueryString = (queryObj: ReducedHelperObject): string => {
const params = new URLSearchParams();

Object.entries(queryObj).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach(v => params.append(key, v));
} else {
params.append(key, value);
}
});

return params.toString();
};
29 changes: 29 additions & 0 deletions src/httpsnippet.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mimetypes } from './fixtures/mimetypes';
import full from './fixtures/requests/full.json';
import headers from './fixtures/requests/headers.json';
import query from './fixtures/requests/query.json';
import short from './fixtures/requests/short.json';
Expand Down Expand Up @@ -217,4 +218,32 @@ describe('hTTPSnippet', () => {
});
});
});

describe('queryObj', () => {
it('should build queryObj with arrays for duplicate keys', () => {
const snippet = new HTTPSnippet(full as Request);
const request = snippet.requests[0];

expect(request.queryObj).toMatchObject({
key: 'value',
foo: ['bar', 'baz'],
baz: 'abc',
});
});

it('should not include array-indexed query keys like foo[0]', () => {
const snippet = new HTTPSnippet(full as Request);
const curl = snippet.convert('shell', 'curl');

expect(curl).not.toMatch(/foo\[\d+\]=/);
});

it('should handle empty queryString array gracefully', () => {
const snippet = new HTTPSnippet(short as Request);

const curl = snippet.convert('shell', 'curl');

expect(curl).toContain('--url http://mockbin.com/har');
});
});
});
3 changes: 2 additions & 1 deletion src/httpsnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { format as urlFormat, parse as urlParse, UrlWithParsedQuery } from 'url'

import { formDataIterator, isBlob } from './helpers/form-data';
import { getHeaderName } from './helpers/headers';
import { getQueryString } from './helpers/query-params';
import { ReducedHelperObject, reducer } from './helpers/reducer';
import { ClientId, TargetId, targets } from './targets/targets';

Expand Down Expand Up @@ -291,7 +292,7 @@ export class HTTPSnippet {
}; //?

// reset uriObj values for a clean url
const search = queryStringify(request.queryObj);
const search = getQueryString(request.queryObj);

const uriObj = {
...urlWithParsedQuery,
Expand Down