Skip to content

Commit

Permalink
fix: ignore undefined value data on GET query
Browse files Browse the repository at this point in the history
closes #436
  • Loading branch information
fengmk2 committed Mar 25, 2023
1 parent 95a51ce commit c15939c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions test/options.data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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',
});
Expand All @@ -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'), '哈哈');
Expand Down

0 comments on commit c15939c

Please sign in to comment.