| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   afterEach, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  |   SpyObject | 
					
						
							| 
									
										
										
										
											2015-10-13 00:29:13 -07:00
										 |  |  | } 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 {BrowserJsonp} from 'angular2/src/http/backends/browser_jsonp'; | 
					
						
							| 
									
										
										
										
											2015-10-06 19:39:44 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   JSONPConnection, | 
					
						
							|  |  |  |   JSONPConnection_, | 
					
						
							| 
									
										
										
										
											2015-10-08 08:22:11 -07:00
										 |  |  |   JSONPBackend, | 
					
						
							|  |  |  |   JSONPBackend_ | 
					
						
							| 
									
										
										
										
											2015-10-06 19:39:44 -07:00
										 |  |  | } from 'angular2/src/http/backends/jsonp_backend'; | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  | import {provide, Injector} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {isPresent, StringWrapper} from 'angular2/src/facade/lang'; | 
					
						
							|  |  |  | import {TimerWrapper} from 'angular2/src/facade/async'; | 
					
						
							| 
									
										
										
										
											2015-08-20 14:28:25 -07:00
										 |  |  | import {Request} from 'angular2/src/http/static_request'; | 
					
						
							|  |  |  | import {Response} from 'angular2/src/http/static_response'; | 
					
						
							| 
									
										
										
										
											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, ReadyState, RequestMethod} from 'angular2/src/http/enums'; | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  | var addEventListenerSpy: any; | 
					
						
							|  |  |  | var existingScripts: MockBrowserJsonp[] = []; | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  | var unused: Response; | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MockBrowserJsonp extends BrowserJsonp { | 
					
						
							|  |  |  |   src: string; | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |   callbacks = new Map<string, (data: any) => any>(); | 
					
						
							|  |  |  |   constructor() { super(); } | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   addEventListener(type: string, cb: (data: any) => any) { this.callbacks.set(type, cb); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |   removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |   dispatchEvent(type: string, argument?: any) { | 
					
						
							|  |  |  |     if (!isPresent(argument)) { | 
					
						
							|  |  |  |       argument = {}; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |     let cb = this.callbacks.get(type); | 
					
						
							|  |  |  |     if (isPresent(cb)) { | 
					
						
							|  |  |  |       cb(argument); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   build(url: string) { | 
					
						
							|  |  |  |     var script = new MockBrowserJsonp(); | 
					
						
							|  |  |  |     script.src = url; | 
					
						
							|  |  |  |     existingScripts.push(script); | 
					
						
							|  |  |  |     return script; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   send(node: any) { /* noop */ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   cleanup(node: any) { /* noop */ | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('JSONPBackend', () => { | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |     let backend: JSONPBackend_; | 
					
						
							|  |  |  |     let sampleRequest: Request; | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       let injector = Injector.resolveAndCreate([ | 
					
						
							| 
									
										
										
										
											2015-10-12 11:30:34 -07:00
										 |  |  |         provide(ResponseOptions, {useClass: BaseResponseOptions}), | 
					
						
							|  |  |  |         provide(BrowserJsonp, {useClass: MockBrowserJsonp}), | 
					
						
							|  |  |  |         provide(JSONPBackend, {useClass: JSONPBackend_}) | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |       ]); | 
					
						
							|  |  |  |       backend = injector.get(JSONPBackend); | 
					
						
							|  |  |  |       let base = new BaseRequestOptions(); | 
					
						
							|  |  |  |       sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'}))); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     afterEach(() => { existingScripts = []; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should create a connection', () => { | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       var instance: JSONPConnection; | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |       expect(() => instance = backend.createConnection(sampleRequest)).not.toThrow(); | 
					
						
							|  |  |  |       expect(instance).toBeAnInstanceOf(JSONPConnection); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('JSONPConnection', () => { | 
					
						
							|  |  |  |       it('should use the injected BaseResponseOptions to create the response', | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-06 06:53:39 -07:00
										 |  |  |            let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp(), | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |                                                  new ResponseOptions({type: ResponseType.Error})); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |            connection.response.subscribe(res => { | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |              expect(res.type).toBe(ResponseType.Error); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  |            connection.finished(); | 
					
						
							|  |  |  |            existingScripts[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should ignore load/callback when disposed', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-06 06:53:39 -07:00
										 |  |  |            var connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp()); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |            let spy = new SpyObject(); | 
					
						
							|  |  |  |            let loadSpy = spy.spy('load'); | 
					
						
							|  |  |  |            let errorSpy = spy.spy('error'); | 
					
						
							|  |  |  |            let returnSpy = spy.spy('cancelled'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |            let request = connection.response.subscribe(loadSpy, errorSpy, returnSpy); | 
					
						
							|  |  |  |            request.unsubscribe(); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |            connection.finished('Fake data'); | 
					
						
							|  |  |  |            existingScripts[0].dispatchEvent('load'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            TimerWrapper.setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |              expect(connection.readyState).toBe(ReadyState.Cancelled); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |              expect(loadSpy).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |              expect(errorSpy).not.toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |              expect(returnSpy).not.toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |              async.done(); | 
					
						
							|  |  |  |            }, 10); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should report error if loaded without invoking callback', | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-06 06:53:39 -07:00
										 |  |  |            let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp()); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |            connection.response.subscribe( | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |                res => { | 
					
						
							|  |  |  |                  expect("response listener called").toBe(false); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }, | 
					
						
							|  |  |  |                err => { | 
					
						
							| 
									
										
										
										
											2015-10-29 17:50:12 -07:00
										 |  |  |                  expect(err.text()).toEqual('JSONP injected script did not invoke callback.'); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            existingScripts[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should report error if script contains error', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-06 06:53:39 -07:00
										 |  |  |            let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp()); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |            connection.response.subscribe( | 
					
						
							|  |  |  |                res => { | 
					
						
							|  |  |  |                  expect("response listener called").toBe(false); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }, | 
					
						
							|  |  |  |                err => { | 
					
						
							| 
									
										
										
										
											2015-10-29 17:50:12 -07:00
										 |  |  |                  expect(err.text()).toBe('Oops!'); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |            existingScripts[0].dispatchEvent('error', ({message: "Oops!"})); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw if request method is not GET', () => { | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |         [RequestMethod.Post, RequestMethod.Put, RequestMethod.Delete, RequestMethod.Options, | 
					
						
							|  |  |  |          RequestMethod.Head, RequestMethod.Patch] | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |             .forEach(method => { | 
					
						
							|  |  |  |               let base = new BaseRequestOptions(); | 
					
						
							|  |  |  |               let req = new Request( | 
					
						
							|  |  |  |                   base.merge(new RequestOptions({url: 'https://google.com', method: method}))); | 
					
						
							| 
									
										
										
										
											2015-10-06 06:53:39 -07:00
										 |  |  |               expect(() => new JSONPConnection_(req, new MockBrowserJsonp()).response.subscribe()) | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |                   .toThrowError(); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |             }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |       it('should respond with data passed to callback', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-10-06 06:53:39 -07:00
										 |  |  |            let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp()); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |            connection.response.subscribe(res => { | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |              expect(res.json()).toEqual(({fake_payload: true, blob_id: 12345})); | 
					
						
							|  |  |  |              async.done(); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            connection.finished(({fake_payload: true, blob_id: 12345})); | 
					
						
							|  |  |  |            existingScripts[0].dispatchEvent('load'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |