| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 11:35:08 -08:00
										 |  |  | import {HttpParams} from '@angular/common/http/src/params'; | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |   describe('HttpUrlEncodedParams', () => { | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |     describe('initialization', () => { | 
					
						
							|  |  |  |       it('should be empty at construction', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams(); | 
					
						
							| 
									
										
										
										
											2017-07-27 16:13:16 -07:00
										 |  |  |         expect(body.toString()).toEqual(''); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should parse an existing url', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=b&c=d&c=e'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         expect(body.getAll('a')).toEqual(['b']); | 
					
						
							|  |  |  |         expect(body.getAll('c')).toEqual(['d', 'e']); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('lazy mutation', () => { | 
					
						
							|  |  |  |       it('should allow setting parameters', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=b'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         const mutated = body.set('a', 'c'); | 
					
						
							|  |  |  |         expect(mutated.toString()).toEqual('a=c'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow appending parameters', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=b'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         const mutated = body.append('a', 'c'); | 
					
						
							|  |  |  |         expect(mutated.toString()).toEqual('a=b&a=c'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow deletion of parameters', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=b&c=d&e=f'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         const mutated = body.delete('c'); | 
					
						
							|  |  |  |         expect(mutated.toString()).toEqual('a=b&e=f'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow chaining of mutations', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=b&c=d&e=f'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         const mutated = body.append('e', 'y').delete('c').set('a', 'x').append('e', 'z'); | 
					
						
							|  |  |  |         expect(mutated.toString()).toEqual('a=x&e=f&e=y&e=z'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow deletion of one value of a parameter', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=1&a=2&a=3&a=4&a=5'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         const mutated = body.delete('a', '2').delete('a', '4'); | 
					
						
							|  |  |  |         expect(mutated.getAll('a')).toEqual(['1', '3', '5']); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-03-01 00:52:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should not repeat mutations that have already been materialized', () => { | 
					
						
							|  |  |  |         const body = new HttpParams({fromString: 'a=b'}); | 
					
						
							|  |  |  |         const mutated = body.append('a', 'c'); | 
					
						
							|  |  |  |         expect(mutated.toString()).toEqual('a=b&a=c'); | 
					
						
							|  |  |  |         const mutated2 = mutated.append('c', 'd'); | 
					
						
							|  |  |  |         expect(mutated.toString()).toEqual('a=b&a=c'); | 
					
						
							|  |  |  |         expect(mutated2.toString()).toEqual('a=b&a=c&c=d'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('read operations', () => { | 
					
						
							|  |  |  |       it('should give null if parameter is not set', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=b&c=d'}); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |         expect(body.get('e')).toBeNull(); | 
					
						
							|  |  |  |         expect(body.getAll('e')).toBeNull(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should give an accurate list of keys', () => { | 
					
						
							| 
									
										
										
										
											2017-07-07 14:56:36 -07:00
										 |  |  |         const body = new HttpParams({fromString: 'a=1&b=2&c=3&d=4'}); | 
					
						
							|  |  |  |         expect(body.keys()).toEqual(['a', 'b', 'c', 'd']); | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |