| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   afterEach, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   SpyObject | 
					
						
							|  |  |  | } from 'angular2/testing_internal'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {ObservableWrapper} from 'angular2/src/facade/async'; | 
					
						
							| 
									
										
										
										
											2015-08-20 14:28:25 -07:00
										 |  |  | import {BrowserXhr} from 'angular2/src/http/backends/browser_xhr'; | 
					
						
							|  |  |  | import {XHRConnection, XHRBackend} from 'angular2/src/http/backends/xhr_backend'; | 
					
						
							| 
									
										
										
										
											2016-04-14 12:35:24 -07:00
										 |  |  | import {provide, Injector, ReflectiveInjector} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-08-20 14:28:25 -07:00
										 |  |  | import {Request} from 'angular2/src/http/static_request'; | 
					
						
							|  |  |  | import {Response} from 'angular2/src/http/static_response'; | 
					
						
							|  |  |  | import {Headers} from 'angular2/src/http/headers'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {Map} from 'angular2/src/facade/collection'; | 
					
						
							| 
									
										
										
										
											2015-08-20 14:28:25 -07:00
										 |  |  | import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options'; | 
					
						
							|  |  |  | import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options'; | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  | import {ResponseType} from 'angular2/src/http/enums'; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  | var abortSpy: any; | 
					
						
							|  |  |  | var sendSpy: any; | 
					
						
							|  |  |  | var openSpy: any; | 
					
						
							|  |  |  | var setRequestHeaderSpy: any; | 
					
						
							|  |  |  | var addEventListenerSpy: any; | 
					
						
							|  |  |  | var existingXHRs: MockBrowserXHR[] = []; | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  | var unused: Response; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  | class MockBrowserXHR extends BrowserXhr { | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   abort: any; | 
					
						
							|  |  |  |   send: any; | 
					
						
							|  |  |  |   open: any; | 
					
						
							|  |  |  |   response: any; | 
					
						
							|  |  |  |   responseText: string; | 
					
						
							| 
									
										
										
										
											2015-07-02 01:20:09 +03:00
										 |  |  |   setRequestHeader: any; | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |   callbacks = new Map<string, Function>(); | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  |   status: number; | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |   responseHeaders: string; | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  |   responseURL: string; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   constructor() { | 
					
						
							|  |  |  |     super(); | 
					
						
							| 
									
										
										
										
											2015-06-19 12:14:12 -07:00
										 |  |  |     var spy = new SpyObject(); | 
					
						
							|  |  |  |     this.abort = abortSpy = spy.spy('abort'); | 
					
						
							|  |  |  |     this.send = sendSpy = spy.spy('send'); | 
					
						
							|  |  |  |     this.open = openSpy = spy.spy('open'); | 
					
						
							| 
									
										
										
										
											2015-07-02 01:20:09 +03:00
										 |  |  |     this.setRequestHeader = setRequestHeaderSpy = spy.spy('setRequestHeader'); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-19 12:14:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   setStatusCode(status: number) { this.status = status; } | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   setResponse(value: string) { this.response = value; } | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   setResponseText(value: string) { this.responseText = value; } | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   setResponseURL(value: string) { this.responseURL = value; } | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   setResponseHeaders(value: string) { this.responseHeaders = value; } | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   getAllResponseHeaders() { return this.responseHeaders || ''; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   getResponseHeader(key: string) { | 
					
						
							|  |  |  |     return Headers.fromResponseHeaderString(this.responseHeaders).get(key); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |   removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |   dispatchEvent(type: string) { this.callbacks.get(type)({}); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   build() { | 
					
						
							|  |  |  |     var xhr = new MockBrowserXHR(); | 
					
						
							|  |  |  |     existingXHRs.push(xhr); | 
					
						
							|  |  |  |     return xhr; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('XHRBackend', () => { | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |     var backend: XHRBackend; | 
					
						
							|  |  |  |     var sampleRequest: Request; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-04-14 12:35:24 -07:00
										 |  |  |       var injector = ReflectiveInjector.resolveAndCreate([ | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |         provide(ResponseOptions, {useClass: BaseResponseOptions}), | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |         provide(BrowserXhr, {useClass: MockBrowserXHR}), | 
					
						
							|  |  |  |         XHRBackend | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       backend = injector.get(XHRBackend); | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |       var base = new BaseRequestOptions(); | 
					
						
							|  |  |  |       sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'}))); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |     afterEach(() => { existingXHRs = []; }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     it('should create a connection', | 
					
						
							|  |  |  |        () => { expect(() => backend.createConnection(sampleRequest)).not.toThrow(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('XHRConnection', () => { | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |       it('should use the injected BaseResponseOptions to create the response', | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({type: ResponseType.Error})); | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |            connection.response.subscribe((res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |              expect(res.type).toBe(ResponseType.Error); | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |            existingXHRs[0].setStatusCode(200); | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should complete a request', inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({type: ResponseType.Error})); | 
					
						
							|  |  |  |            connection.response.subscribe((res: Response) => { | 
					
						
							|  |  |  |              expect(res.type).toBe(ResponseType.Error); | 
					
						
							|  |  |  |            }, null, () => { async.done(); }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |            existingXHRs[0].setStatusCode(200); | 
					
						
							| 
									
										
										
										
											2015-07-27 16:59:09 -07:00
										 |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |       it('should call abort when disposed', () => { | 
					
						
							| 
									
										
										
										
											2015-06-19 12:14:12 -07:00
										 |  |  |         var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |         var request = connection.response.subscribe(); | 
					
						
							|  |  |  |         request.unsubscribe(); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         expect(abortSpy).toHaveBeenCalled(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should create an error Response on error', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({type: ResponseType.Error})); | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |            connection.response.subscribe(null, (res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |              expect(res.type).toBe(ResponseType.Error); | 
					
						
							| 
									
										
										
										
											2015-08-08 14:17:43 -07:00
										 |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('error'); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |       it('should call open with method and url when subscribed to', () => { | 
					
						
							|  |  |  |         var connection = new XHRConnection(sampleRequest, new MockBrowserXHR()); | 
					
						
							|  |  |  |         expect(openSpy).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |         connection.response.subscribe(); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         expect(openSpy).toHaveBeenCalledWith('GET', sampleRequest.url); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |       it('should call send on the backend with request body when subscribed to', () => { | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         var body = 'Some body to love'; | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |         var base = new BaseRequestOptions(); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |         var connection = new XHRConnection( | 
					
						
							|  |  |  |             new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR()); | 
					
						
							|  |  |  |         expect(sendSpy).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |         connection.response.subscribe(); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |         expect(sendSpy).toHaveBeenCalledWith(body); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-07-02 01:20:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should attach headers to the request', () => { | 
					
						
							| 
									
										
										
										
											2015-10-07 14:56:18 -07:00
										 |  |  |         var headers = | 
					
						
							|  |  |  |             new Headers({'Content-Type': 'text/xml', 'Breaking-Bad': '<3', 'X-Multi': ['a', 'b']}); | 
					
						
							| 
									
										
										
										
											2015-07-02 01:20:09 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         var base = new BaseRequestOptions(); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |         var connection = new XHRConnection( | 
					
						
							|  |  |  |             new Request(base.merge(new RequestOptions({headers: headers}))), new MockBrowserXHR()); | 
					
						
							|  |  |  |         connection.response.subscribe(); | 
					
						
							| 
									
										
										
										
											2015-10-07 14:56:18 -07:00
										 |  |  |         expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/xml'); | 
					
						
							|  |  |  |         expect(setRequestHeaderSpy).toHaveBeenCalledWith('Breaking-Bad', '<3'); | 
					
						
							|  |  |  |         expect(setRequestHeaderSpy).toHaveBeenCalledWith('X-Multi', 'a,b'); | 
					
						
							| 
									
										
										
										
											2015-07-02 01:20:09 +03:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should return the correct status code', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  |            var statusCode = 418; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |            connection.response.subscribe( | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |                (res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                }, | 
					
						
							|  |  |  |                errRes => { | 
					
						
							|  |  |  |                  expect(errRes.status).toBe(statusCode); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should call next and complete on 200 codes', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |            var nextCalled = false; | 
					
						
							|  |  |  |            var errorCalled = false; | 
					
						
							|  |  |  |            var statusCode = 200; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |            connection.response.subscribe( | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |                (res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |                  nextCalled = true; | 
					
						
							|  |  |  |                  expect(res.status).toBe(statusCode); | 
					
						
							|  |  |  |                }, | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |                errRes => { errorCalled = true; }, () => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |                  expect(nextCalled).toBe(true); | 
					
						
							|  |  |  |                  expect(errorCalled).toBe(false); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should call error and not complete on 300+ codes', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |            var nextCalled = false; | 
					
						
							|  |  |  |            var errorCalled = false; | 
					
						
							|  |  |  |            var statusCode = 301; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            connection.response.subscribe((res: Response) => { nextCalled = true; }, errRes => { | 
					
						
							|  |  |  |              expect(errRes.status).toBe(statusCode); | 
					
						
							|  |  |  |              expect(nextCalled).toBe(false); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            }, () => { throw 'should not be called'; }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should normalize IE\'s 1223 status code into 204', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  |            var statusCode = 1223; | 
					
						
							|  |  |  |            var normalizedCode = 204; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |            connection.response.subscribe((res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-07-05 01:58:37 -07:00
										 |  |  |              expect(res.status).toBe(normalizedCode); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should normalize responseText and response', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  |            var responseBody = 'Doge'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var connection1 = | 
					
						
							|  |  |  |                new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var connection2 = | 
					
						
							|  |  |  |                new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |            connection1.response.subscribe((res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  |              expect(res.text()).toBe(responseBody); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |              connection2.response.subscribe(ress => { | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  |                expect(ress.text()).toBe(responseBody); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |              existingXHRs[1].setStatusCode(200); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |              existingXHRs[1].setResponse(responseBody); | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  |              existingXHRs[1].dispatchEvent('load'); | 
					
						
							|  |  |  |            }); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:29:41 -08:00
										 |  |  |            existingXHRs[0].setStatusCode(200); | 
					
						
							| 
									
										
										
										
											2015-07-27 17:01:05 -07:00
										 |  |  |            existingXHRs[0].setResponseText(responseBody); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |       it('should parse response headers and add them to the response', | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |            var statusCode = 200; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |            let responseHeaderString = | 
					
						
							|  |  |  |                `Date: Fri, 20 Nov 2015 01:45:26 GMT
 | 
					
						
							|  |  |  |                Content-Type: application/json; charset=utf-8 | 
					
						
							|  |  |  |                Transfer-Encoding: chunked | 
					
						
							|  |  |  |                Connection: keep-alive`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |                connection.response.subscribe((res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 17:51:00 -08:00
										 |  |  |                  expect(res.headers.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT'); | 
					
						
							|  |  |  |                  expect(res.headers.get('Content-Type')).toEqual('application/json; charset=utf-8'); | 
					
						
							|  |  |  |                  expect(res.headers.get('Transfer-Encoding')).toEqual('chunked'); | 
					
						
							|  |  |  |                  expect(res.headers.get('Connection')).toEqual('keep-alive'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setResponseHeaders(responseHeaderString); | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should add the responseURL to the response', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  |            var statusCode = 200; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |            connection.response.subscribe((res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  |              expect(res.url).toEqual('http://google.com'); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setResponseURL('http://google.com'); | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should add use the X-Request-URL in CORS situations', | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  |            var statusCode = 200; | 
					
						
							| 
									
										
										
										
											2016-04-12 09:40:37 -07:00
										 |  |  |            var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), | 
					
						
							|  |  |  |                                               new ResponseOptions({status: statusCode})); | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  |            var responseHeaders = `X-Request-URL: http://somedomain.com
 | 
					
						
							|  |  |  |            Foo: Bar`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |                                  connection.response.subscribe((res: Response) => { | 
					
						
							| 
									
										
										
										
											2015-11-19 18:47:29 -08:00
										 |  |  |                                    expect(res.url).toEqual('http://somedomain.com'); | 
					
						
							|  |  |  |                                    async.done(); | 
					
						
							|  |  |  |                                  }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            existingXHRs[0].setResponseHeaders(responseHeaders); | 
					
						
							|  |  |  |            existingXHRs[0].setStatusCode(statusCode); | 
					
						
							|  |  |  |            existingXHRs[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |