Skip to content

Commit

Permalink
feat: use querystring stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
runfeng.lrf committed Feb 23, 2023
1 parent 26afa32 commit 6df18eb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { parseJSON, sleep, digestAuthHeader, globalId, performanceTime, isReadab
import symbols from './symbols';
import { initDiagnosticsChannel } from './diagnosticsChannel';
import type { IncomingHttpHeaders } from 'http';
import * as querystring from 'querystring';

type Exists<T> = T extends undefined ? never : T;
type UndiciRequestOption = Exists<Parameters<typeof undiciRequest>[1]>;
Expand Down Expand Up @@ -405,10 +406,10 @@ export class HttpClient extends EventEmitter {
|| isReadable(args.data);
if (isGETOrHEAD) {
if (!isStringOrBufferOrReadable) {
for (const field in args.data) {
const value = typeof args.data[field] !== 'string' ? '' : args.data[field];
requestUrl.searchParams.append(field, value);
for (const [ k, v ] of requestUrl.searchParams.entries()) {
args.data[k] = v;
}
requestUrl.search = querystring.stringify(args.data);
}
} else {
if (isStringOrBufferOrReadable) {
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('index.test.ts', () => {
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'text/html');
assert(response.headers.date);
assert.equal(response.url, `${_url}html?abc=123&foo=bar`);
assert.equal(response.url, `${_url}html?foo=bar&abc=123`);
assert(!response.redirected);
});

Expand Down
14 changes: 9 additions & 5 deletions test/options.data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ describe('options.data.test.ts2', () => {
data: {
sql: 'SELECT * from table',
data: '哈哈',
a: undefined,
b: '',
c: null,
b: undefined,
c: '2222',
d: 1111,
e: function(){

},
f: true,
},
dataType: 'json',
});
Expand All @@ -35,7 +39,7 @@ describe('options.data.test.ts2', () => {
assert(response.url.startsWith(_url));
assert(!response.redirected);
// console.log(response.headers);
assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&a=&b=&c=');
assert.equal(response.data.url, '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&b=&c=2222&d=1111&e=&f=true');
const url = new URL(response.data.href);
assert.equal(url.searchParams.get('sql'), 'SELECT * from table');
assert.equal(url.searchParams.get('data'), '哈哈');
Expand Down Expand Up @@ -77,7 +81,7 @@ describe('options.data.test.ts2', () => {
assert(response.url.startsWith(_url));
assert(!response.redirected);
// console.log(response.data);
assert.equal(response.data.url, '/?that=in_path&sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88');
assert.equal(response.data.url, '/?sql=SELECT%20*%20from%20table&data=%E5%93%88%E5%93%88&that=in_path');
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 6df18eb

Please sign in to comment.