| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {Json} from '../src/facade/lang'; | 
					
						
							|  |  |  | import {Headers} from '../src/headers'; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('Headers', () => { | 
					
						
							|  |  |  |     it('should conform to spec', () => { | 
					
						
							|  |  |  |       // Examples borrowed from https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers
 | 
					
						
							|  |  |  |       // Spec at https://fetch.spec.whatwg.org/#dom-headers
 | 
					
						
							| 
									
										
										
										
											2015-06-19 12:14:12 -07:00
										 |  |  |       var firstHeaders = new Headers();  // Currently empty
 | 
					
						
							|  |  |  |       firstHeaders.append('Content-Type', 'image/jpeg'); | 
					
						
							|  |  |  |       expect(firstHeaders.get('Content-Type')).toBe('image/jpeg'); | 
					
						
							| 
									
										
										
										
											2016-07-26 12:30:43 +08:00
										 |  |  |       // "HTTP character sets are identified by case-insensitive tokens"
 | 
					
						
							|  |  |  |       // Spec at https://tools.ietf.org/html/rfc2616
 | 
					
						
							|  |  |  |       expect(firstHeaders.get('content-type')).toBe('image/jpeg'); | 
					
						
							|  |  |  |       expect(firstHeaders.get('content-Type')).toBe('image/jpeg'); | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |       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); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       expect(secondHeadersObj.get('Content-Type')).toBe('image/jpeg'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('initialization', () => { | 
					
						
							|  |  |  |       it('should merge values in provided dictionary', () => { | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |         var headers = new Headers({'foo': 'bar'}); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         expect(headers.get('foo')).toBe('bar'); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['bar']); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  |         expect(firstHeaders.has('Content-Type')).toBeFalsy(); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('.set()', () => { | 
					
						
							|  |  |  |       it('should clear all values and re-set for the provided key', () => { | 
					
						
							| 
									
										
										
										
											2016-09-19 17:15:57 -07:00
										 |  |  |         var headers = new Headers({'foo': 'bar'}); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         expect(headers.get('foo')).toBe('bar'); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['bar']); | 
					
						
							|  |  |  |         headers.set('foo', 'baz'); | 
					
						
							|  |  |  |         expect(headers.get('foo')).toBe('baz'); | 
					
						
							|  |  |  |         expect(headers.getAll('foo')).toEqual(['baz']); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should convert input array to string', () => { | 
					
						
							|  |  |  |         var headers = new Headers(); | 
					
						
							| 
									
										
										
										
											2015-06-19 12:14:12 -07:00
										 |  |  |         var inputArr = ['bar', 'baz']; | 
					
						
							|  |  |  |         headers.set('foo', inputArr); | 
					
						
							|  |  |  |         expect(/bar, ?baz/g.test(headers.get('foo'))).toBe(true); | 
					
						
							|  |  |  |         expect(/bar, ?baz/g.test(headers.getAll('foo')[0])).toBe(true); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('.toJSON()', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |       let headers: any /** TODO #9100 */ = null; | 
					
						
							|  |  |  |       let inputArr: any /** TODO #9100 */ = null; | 
					
						
							|  |  |  |       let obj: any /** TODO #9100 */ = null; | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							|  |  |  |         headers = new Headers(); | 
					
						
							|  |  |  |         inputArr = ['application/jeisen', 'application/jason', 'application/patrickjs']; | 
					
						
							| 
									
										
										
										
											2016-07-26 12:30:43 +08:00
										 |  |  |         obj = {'accept': inputArr}; | 
					
						
							| 
									
										
										
										
											2016-01-26 23:24:50 -08:00
										 |  |  |         headers.set('Accept', inputArr); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should be serializable with toJSON', () => { | 
					
						
							|  |  |  |         let stringifed = Json.stringify(obj); | 
					
						
							|  |  |  |         let serializedHeaders = Json.stringify(headers); | 
					
						
							|  |  |  |         expect(serializedHeaders).toEqual(stringifed); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should be able to parse serialized header', () => { | 
					
						
							|  |  |  |         let stringifed = Json.stringify(obj); | 
					
						
							|  |  |  |         let serializedHeaders = Json.stringify(headers); | 
					
						
							|  |  |  |         expect(Json.parse(serializedHeaders)).toEqual(Json.parse(stringifed)); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should be able to recreate serializedHeaders', () => { | 
					
						
							|  |  |  |         let serializedHeaders = Json.stringify(headers); | 
					
						
							|  |  |  |         let parsedHeaders = Json.parse(serializedHeaders); | 
					
						
							|  |  |  |         let recreatedHeaders = new Headers(parsedHeaders); | 
					
						
							|  |  |  |         expect(Json.stringify(parsedHeaders)).toEqual(Json.stringify(recreatedHeaders)); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('.fromResponseHeaderString()', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should parse a response header string', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       let responseHeaderString = `Date: Fri, 20 Nov 2015 01:45:26 GMT
 | 
					
						
							| 
									
										
										
										
											2016-07-29 13:19:49 +08:00
										 |  |  | Content-Type: application/json; charset=utf-8 | 
					
						
							|  |  |  | Transfer-Encoding: chunked | 
					
						
							|  |  |  | Connection: keep-alive`;
 | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       let responseHeaders = Headers.fromResponseHeaderString(responseHeaderString); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(responseHeaders.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT'); | 
					
						
							|  |  |  |       expect(responseHeaders.get('Content-Type')).toEqual('application/json; charset=utf-8'); | 
					
						
							|  |  |  |       expect(responseHeaders.get('Transfer-Encoding')).toEqual('chunked'); | 
					
						
							|  |  |  |       expect(responseHeaders.get('Connection')).toEqual('keep-alive'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | } |