From 6df18eb1b2f7566b1e6dd803ba70883d3fe056c5 Mon Sep 17 00:00:00 2001 From: "runfeng.lrf" Date: Thu, 23 Feb 2023 20:47:34 +0800 Subject: [PATCH] feat: use querystring stringify --- src/HttpClient.ts | 7 ++++--- test/index.test.ts | 2 +- test/options.data.test.ts | 14 +++++++++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 7d2252ec..5d93b83d 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -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 extends undefined ? never : T; type UndiciRequestOption = Exists[1]>; @@ -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) { diff --git a/test/index.test.ts b/test/index.test.ts index 5ee06a00..c9f5092d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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); }); diff --git a/test/options.data.test.ts b/test/options.data.test.ts index 51c39a04..347bf581 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -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', }); @@ -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'), '哈哈'); @@ -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'), '哈哈');