| 
									
										
										
										
											2016-06-23 09:47:54 -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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 15:10:54 -08:00
										 |  |  | import {Headers} from '@angular/http/src/headers'; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   describe('Headers', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('initialization', () => { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |       it('should conform to spec', () => { | 
					
						
							|  |  |  |         const httpHeaders = { | 
					
						
							|  |  |  |           'Content-Type': 'image/jpeg', | 
					
						
							|  |  |  |           'Accept-Charset': 'utf-8', | 
					
						
							|  |  |  |           'X-My-Custom-Header': 'Zeke are cool', | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         const secondHeaders = new Headers(httpHeaders); | 
					
						
							|  |  |  |         const secondHeadersObj = new Headers(secondHeaders); | 
					
						
							|  |  |  |         expect(secondHeadersObj.get('Content-Type')).toEqual('image/jpeg'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       it('should merge values in provided dictionary', () => { | 
					
						
							| 
									
										
										
										
											2016-09-18 15:39:26 -07:00
										 |  |  |         const headers = new Headers({'foo': 'bar'}); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         expect(headers.get('foo')).toEqual('bar'); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         expect(headers.getAll('foo')).toEqual(['bar']); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 00:00:44 +02:00
										 |  |  |       it('should not alter the values of a provided header template', () => { | 
					
						
							|  |  |  |         // Spec at https://fetch.spec.whatwg.org/#concept-headers-fill
 | 
					
						
							|  |  |  |         // test for https://github.com/angular/angular/issues/6845
 | 
					
						
							|  |  |  |         const firstHeaders = new Headers(); | 
					
						
							|  |  |  |         const secondHeaders = new Headers(firstHeaders); | 
					
						
							|  |  |  |         secondHeaders.append('Content-Type', 'image/jpeg'); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         expect(firstHeaders.has('Content-Type')).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-08-19 00:00:44 +02:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-10-05 17:05:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should preserve the list of values', () => { | 
					
						
							|  |  |  |         const src = new Headers(); | 
					
						
							|  |  |  |         src.append('foo', 'a'); | 
					
						
							|  |  |  |         src.append('foo', 'b'); | 
					
						
							|  |  |  |         src.append('foo', 'c'); | 
					
						
							|  |  |  |         const dst = new Headers(src); | 
					
						
							|  |  |  |         expect(dst.getAll('foo')).toEqual(src.getAll('foo')); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should keep the last value when initialized from an object', () => { | 
					
						
							|  |  |  |         const headers = new Headers({ | 
					
						
							|  |  |  |           'foo': 'first', | 
					
						
							|  |  |  |           'fOo': 'second', | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['second']); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('.set()', () => { | 
					
						
							|  |  |  |       it('should clear all values and re-set for the provided key', () => { | 
					
						
							| 
									
										
										
										
											2016-09-18 15:39:26 -07:00
										 |  |  |         const headers = new Headers({'foo': 'bar'}); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         expect(headers.get('foo')).toEqual('bar'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         headers.set('foo', 'baz'); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         expect(headers.get('foo')).toEqual('baz'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         headers.set('fOO', 'bat'); | 
					
						
							|  |  |  |         expect(headers.get('foo')).toEqual('bat'); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |       it('should preserve the case of the first call', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  |         headers.set('fOo', 'baz'); | 
					
						
							|  |  |  |         headers.set('foo', 'bat'); | 
					
						
							|  |  |  |         expect(JSON.stringify(headers)).toEqual('{"fOo":["bat"]}'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-04 13:26:38 -07:00
										 |  |  |       it('should preserve cases after cloning', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  |         headers.set('fOo', 'baz'); | 
					
						
							|  |  |  |         headers.set('foo', 'bat'); | 
					
						
							|  |  |  |         expect(JSON.stringify(new Headers(headers))).toEqual('{"fOo":["bat"]}'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       it('should convert input array to string', () => { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  |         headers.set('foo', ['bar', 'baz']); | 
					
						
							|  |  |  |         expect(headers.get('foo')).toEqual('bar,baz'); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['bar,baz']); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     describe('.get()', () => { | 
					
						
							|  |  |  |       it('should be case insensitive', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  |         headers.set('foo', 'baz'); | 
					
						
							|  |  |  |         expect(headers.get('foo')).toEqual('baz'); | 
					
						
							|  |  |  |         expect(headers.get('FOO')).toEqual('baz'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |       it('should return null if the header is not present', () => { | 
					
						
							|  |  |  |         const headers = new Headers({bar: []}); | 
					
						
							|  |  |  |         expect(headers.get('bar')).toEqual(null); | 
					
						
							|  |  |  |         expect(headers.get('foo')).toEqual(null); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     describe('.getAll()', () => { | 
					
						
							|  |  |  |       it('should be case insensitive', () => { | 
					
						
							|  |  |  |         const headers = new Headers({foo: ['bar', 'baz']}); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['bar', 'baz']); | 
					
						
							|  |  |  |         expect(headers.getAll('FOO')).toEqual(['bar', 'baz']); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |       it('should return null if the header is not present', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(null); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     describe('.delete', () => { | 
					
						
							|  |  |  |       it('should be case insensitive', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         headers.set('foo', 'baz'); | 
					
						
							|  |  |  |         expect(headers.has('foo')).toEqual(true); | 
					
						
							|  |  |  |         headers.delete('foo'); | 
					
						
							|  |  |  |         expect(headers.has('foo')).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         headers.set('foo', 'baz'); | 
					
						
							|  |  |  |         expect(headers.has('foo')).toEqual(true); | 
					
						
							|  |  |  |         headers.delete('FOO'); | 
					
						
							|  |  |  |         expect(headers.has('foo')).toEqual(false); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     describe('.append', () => { | 
					
						
							| 
									
										
										
										
											2016-10-05 16:36:42 -07:00
										 |  |  |       it('should append a value to the list', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							|  |  |  |         headers.append('foo', 'bar'); | 
					
						
							|  |  |  |         headers.append('foo', 'baz'); | 
					
						
							|  |  |  |         expect(headers.get('foo')).toEqual('bar'); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['bar', 'baz']); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |       it('should preserve the case of the first call', () => { | 
					
						
							|  |  |  |         const headers = new Headers(); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         headers.append('FOO', 'bar'); | 
					
						
							|  |  |  |         headers.append('foo', 'baz'); | 
					
						
							|  |  |  |         expect(JSON.stringify(headers)).toEqual('{"FOO":["bar","baz"]}'); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |     describe('.toJSON()', () => { | 
					
						
							|  |  |  |       let headers: Headers; | 
					
						
							|  |  |  |       let values: string[]; | 
					
						
							|  |  |  |       let ref: {[name: string]: string[]}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							|  |  |  |         values = ['application/jeisen', 'application/jason', 'application/patrickjs']; | 
					
						
							|  |  |  |         headers = new Headers(); | 
					
						
							|  |  |  |         headers.set('Accept', values); | 
					
						
							|  |  |  |         ref = {'Accept': values}; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should be serializable with toJSON', | 
					
						
							|  |  |  |          () => { expect(JSON.stringify(headers)).toEqual(JSON.stringify(ref)); }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should be able to recreate serializedHeaders', () => { | 
					
						
							| 
									
										
										
										
											2016-10-03 15:27:56 -07:00
										 |  |  |         const parsedHeaders = JSON.parse(JSON.stringify(headers)); | 
					
						
							|  |  |  |         const recreatedHeaders = new Headers(parsedHeaders); | 
					
						
							|  |  |  |         expect(JSON.stringify(parsedHeaders)).toEqual(JSON.stringify(recreatedHeaders)); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-05 16:36:11 -07:00
										 |  |  |     describe('.fromResponseHeaderString()', () => { | 
					
						
							|  |  |  |       it('should parse a response header string', () => { | 
					
						
							|  |  |  |         const response = `Date: Fri, 20 Nov 2015 01:45:26 GMT\n` + | 
					
						
							|  |  |  |             `Content-Type: application/json; charset=utf-8\n` + | 
					
						
							|  |  |  |             `Transfer-Encoding: chunked\n` + | 
					
						
							|  |  |  |             `Connection: keep-alive`; | 
					
						
							|  |  |  |         const headers = Headers.fromResponseHeaderString(response); | 
					
						
							|  |  |  |         expect(headers.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT'); | 
					
						
							|  |  |  |         expect(headers.get('Content-Type')).toEqual('application/json; charset=utf-8'); | 
					
						
							|  |  |  |         expect(headers.get('Transfer-Encoding')).toEqual('chunked'); | 
					
						
							|  |  |  |         expect(headers.get('Connection')).toEqual('keep-alive'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | } |