| 
									
										
										
										
											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-08-02 15:53:34 -07:00
										 |  |  | import {ReflectiveInjector} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							|  |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {BrowserJsonp} from '../../src/backends/browser_jsonp'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | import {JSONPBackend, JSONPBackend_, JSONPConnection, JSONPConnection_} from '../../src/backends/jsonp_backend'; | 
					
						
							|  |  |  | import {BaseRequestOptions, RequestOptions} from '../../src/base_request_options'; | 
					
						
							|  |  |  | import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options'; | 
					
						
							|  |  |  | import {ReadyState, RequestMethod, ResponseType} from '../../src/enums'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Request} from '../../src/static_request'; | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  | let existingScripts: MockBrowserJsonp[] = []; | 
					
						
							| 
									
										
										
										
											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>(); | 
					
						
							| 
									
										
										
										
											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); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 18:19:14 +01:00
										 |  |  |   dispatchEvent(type: string, argument: any = {}) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const cb = this.callbacks.get(type); | 
					
						
							| 
									
										
										
										
											2016-11-15 18:19:14 +01:00
										 |  |  |     if (cb) { | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |       cb(argument); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   build(url: string) { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const script = new MockBrowserJsonp(); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |     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(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const injector = ReflectiveInjector.resolveAndCreate([ | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -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); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const base = new BaseRequestOptions(); | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |       sampleRequest = | 
					
						
							|  |  |  |           new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     afterEach(() => { existingScripts = []; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should create a connection', () => { | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |       let instance: JSONPConnection = undefined !; | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const connection = new JSONPConnection_( | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                sampleRequest, new MockBrowserJsonp(), | 
					
						
							|  |  |  |                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) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp()); | 
					
						
							|  |  |  |            const spy = new SpyObject(); | 
					
						
							|  |  |  |            const loadSpy = spy.spy('load'); | 
					
						
							|  |  |  |            const errorSpy = spy.spy('error'); | 
					
						
							|  |  |  |            const returnSpy = spy.spy('cancelled'); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const request = connection.response.subscribe(loadSpy, errorSpy, returnSpy); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |            request.unsubscribe(); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |            connection.finished('Fake data'); | 
					
						
							|  |  |  |            existingScripts[0].dispatchEvent('load'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            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) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const 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 => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                  expect('response listener called').toBe(false); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |                  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) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const 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 => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |                  expect('response listener called').toBe(false); | 
					
						
							| 
									
										
										
										
											2015-09-25 18:53:32 -04:00
										 |  |  |                  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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            existingScripts[0].dispatchEvent('error', ({message: 'Oops!'})); | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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 => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |               const base = new BaseRequestOptions(); | 
					
						
							| 
									
										
										
										
											2017-04-17 11:12:53 -07:00
										 |  |  |               const req = new Request(base.merge( | 
					
						
							|  |  |  |                   new RequestOptions({url: 'https://google.com', method: method})) as any); | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const 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'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |