Skip to content

Commit

Permalink
fix: use querystring to stringify data
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Feb 20, 2024
1 parent f6ac20b commit 161b5d1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { basename } from 'node:path';
import { createReadStream } from 'node:fs';
import { format as urlFormat } from 'node:url';
import { performance } from 'node:perf_hooks';
import querystring from 'node:querystring';
import {
FormData as FormDataNative,
request as undiciRequest,
Expand Down Expand Up @@ -503,18 +504,15 @@ export class HttpClient extends EventEmitter {
|| isReadable(args.data);
if (isGETOrHEAD) {
if (!isStringOrBufferOrReadable) {
let query;
if (args.nestedQuerystring) {
const querystring = qs.stringify(args.data);
// reset the requestUrl
const href = requestUrl.href;
requestUrl = new URL(href + (href.includes('?') ? '&' : '?') + querystring);
query = qs.stringify(args.data);
} else {
for (const field in args.data) {
const fieldValue = args.data[field];
if (fieldValue === undefined) continue;
requestUrl.searchParams.append(field, fieldValue);
}
query = querystring.stringify(args.data);
}
// reset the requestUrl
const href = requestUrl.href;
requestUrl = new URL(href + (href.includes('?') ? '&' : '?') + query);
}
} else {
if (isStringOrBufferOrReadable) {
Expand Down
3 changes: 2 additions & 1 deletion test/options.data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('options.data.test.ts', () => {
d: 1111,
e() { return ''; },
f: true,
g: [ 'a', 'b' ],
},
dataType: 'json',
});
Expand All @@ -37,7 +38,7 @@ describe('options.data.test.ts', () => {
assert.equal(response.data.method, 'GET');
assert(response.url.startsWith(_url));
assert(!response.redirected);
assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true');
assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b');

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 16)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 16)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12

Check failure on line 41 in test/options.data.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

test/options.data.test.ts > options.data.test.ts > should default GET with data and auto convert to query string

AssertionError: Expected values to be strictly equal: + actual - expected + '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b' - '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b' - Expected + Received - /?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=e%28%29+%7B%0A++++++++++return+%22%22%3B%0A++++++++%7D&f=true&g=a&g=b + /?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true&g=a&g=b ❯ test/options.data.test.ts:41:12
const url = new URL(response.data.href);
assert.equal(url.searchParams.get('sql'), 'SELECT * from table');
assert.equal(url.searchParams.get('data'), '哈哈');
Expand Down

0 comments on commit 161b5d1

Please sign in to comment.