From bf7b82b65841e04b6d8c71d4549b1589f760c4f6 Mon Sep 17 00:00:00 2001 From: alexbyk Date: Fri, 30 Sep 2016 19:57:26 +0300 Subject: [PATCH] fix(UrlSearchParams): change a behavior when a param value is null or undefined (#11990) --- modules/@angular/http/src/url_search_params.ts | 5 +++++ .../http/test/url_search_params_spec.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/modules/@angular/http/src/url_search_params.ts b/modules/@angular/http/src/url_search_params.ts index 7703f1c871..e750a5ecac 100644 --- a/modules/@angular/http/src/url_search_params.ts +++ b/modules/@angular/http/src/url_search_params.ts @@ -102,6 +102,10 @@ export class URLSearchParams { getAll(param: string): string[] { return this.paramsMap.get(param) || []; } set(param: string, val: string) { + if (val === void 0 || val === null) { + this.delete(param); + return; + } const list = this.paramsMap.get(param) || []; list.length = 0; list.push(val); @@ -124,6 +128,7 @@ export class URLSearchParams { } append(param: string, val: string): void { + if (val === void 0 || val === null) return; const list = this.paramsMap.get(param) || []; list.push(val); this.paramsMap.set(param, list); diff --git a/modules/@angular/http/test/url_search_params_spec.ts b/modules/@angular/http/test/url_search_params_spec.ts index f075e9536b..336aa9e652 100644 --- a/modules/@angular/http/test/url_search_params_spec.ts +++ b/modules/@angular/http/test/url_search_params_spec.ts @@ -149,5 +149,23 @@ export function main() { expect(paramsC.toString()).toEqual('a=2&q=4%2B&c=8'); }); + it('should remove the parameter when set to undefined or null', () => { + const params = new URLSearchParams('q=Q'); + params.set('q', undefined); + expect(params.has('q')).toBe(false); + expect(params.toString()).toEqual(''); + params.set('q', null); + expect(params.has('q')).toBe(false); + expect(params.toString()).toEqual(''); + }); + + it('should ignore the value when append undefined or null', () => { + const params = new URLSearchParams('q=Q'); + params.append('q', undefined); + expect(params.toString()).toEqual('q=Q'); + params.append('q', null); + expect(params.toString()).toEqual('q=Q'); + }); + }); }