From c15939c4823aed1fcf97722917b1fc84df73cb21 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 25 Mar 2023 18:38:51 +0800 Subject: [PATCH] fix: ignore undefined value data on GET query closes https://github.com/node-modules/urllib/pull/436 --- src/HttpClient.ts | 4 +++- test/options.data.test.ts | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/HttpClient.ts b/src/HttpClient.ts index 44b2f5c3..cfdd5bd4 100644 --- a/src/HttpClient.ts +++ b/src/HttpClient.ts @@ -409,7 +409,9 @@ export class HttpClient extends EventEmitter { if (isGETOrHEAD) { if (!isStringOrBufferOrReadable) { for (const field in args.data) { - requestUrl.searchParams.append(field, args.data[field]); + const fieldValue = args.data[field]; + if (fieldValue === undefined) continue; + requestUrl.searchParams.append(field, fieldValue); } } } else { diff --git a/test/options.data.test.ts b/test/options.data.test.ts index 35ab6778..c40c7af7 100644 --- a/test/options.data.test.ts +++ b/test/options.data.test.ts @@ -5,7 +5,7 @@ import { describe, it, beforeAll, afterAll } from 'vitest'; import urllib from '../src'; import { startServer } from './fixtures/server'; -describe('options.data.test.ts2', () => { +describe('options.data.test.ts', () => { let close: any; let _url: string; beforeAll(async () => { @@ -23,6 +23,11 @@ describe('options.data.test.ts2', () => { data: { sql: 'SELECT * from table', data: '哈哈', + b: undefined, + c: '2222', + d: 1111, + e: function(){}, + f: true, }, dataType: 'json', }); @@ -31,8 +36,7 @@ describe('options.data.test.ts2', () => { assert.equal(response.data.method, 'GET'); 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'); + assert.equal(response.data.url, '/?sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88&c=2222&d=1111&e=function%28%29+%7B%0A++++++++%7D&f=true'); const url = new URL(response.data.href); assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); assert.equal(url.searchParams.get('data'), '哈哈');