| 
									
										
										
										
											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-07-22 09:20:51 -07:00
										 |  |  | import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, ddescribe, describe, iit, inject, it, xit,} from '@angular/core/testing/testing_internal'; | 
					
						
							|  |  |  | import {expect} from '@angular/platform-browser/testing/matchers'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {BrowserJsonp} from '../../src/backends/browser_jsonp'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {JSONPConnection, JSONPConnection_, JSONPBackend, JSONPBackend_} from '../../src/backends/jsonp_backend'; | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  | import {ReflectiveInjector} from '@angular/core'; | 
					
						
							|  |  |  | import {isPresent,} from '../../src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {TimerWrapper} from '../../src/facade/async'; | 
					
						
							|  |  |  | import {Request} from '../../src/static_request'; | 
					
						
							|  |  |  | import {Response} from '../../src/static_response'; | 
					
						
							|  |  |  | import {Map} from '../../src/facade/collection'; | 
					
						
							|  |  |  | import {RequestOptions, BaseRequestOptions} from '../../src/base_request_options'; | 
					
						
							|  |  |  | import {BaseResponseOptions, ResponseOptions} from '../../src/base_response_options'; | 
					
						
							|  |  |  | import {ResponseType, ReadyState, RequestMethod} from '../../src/enums'; | 
					
						
							| 
									
										
										
										
											2015-07-14 19:53:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  | 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(() => { | 
					
						
							| 
									
										
										
										
											2016-04-14 12:35:24 -07:00
										 |  |  |       let 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); | 
					
						
							|  |  |  |       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) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            let connection = new JSONPConnection_( | 
					
						
							|  |  |  |                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) => { | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							|  |  |  |               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'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |