2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {afterEach, beforeEach, ddescribe, describe, expect, iit, inject, it, xit,} from '@angular/core/testing/testing_internal';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {AsyncTestCompleter, SpyObject} from '@angular/core/testing/testing_internal';
|
|
|
|
import {ObservableWrapper} from '../../src/facade/async';
|
|
|
|
import {BrowserJsonp} from '../../src/backends/browser_jsonp';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {JSONPConnection, JSONPConnection_, JSONPBackend, JSONPBackend_} from '../../src/backends/jsonp_backend';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {provide, Injector, ReflectiveInjector} from '@angular/core';
|
|
|
|
import {isPresent, StringWrapper} from '../../src/facade/lang';
|
|
|
|
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 20:53:04 -04:00
|
|
|
|
2016-02-01 20:05:50 -05:00
|
|
|
var addEventListenerSpy: any;
|
|
|
|
var existingScripts: MockBrowserJsonp[] = [];
|
2015-07-07 23:03:00 -04:00
|
|
|
var unused: Response;
|
2015-07-14 20:53:04 -04:00
|
|
|
|
|
|
|
class MockBrowserJsonp extends BrowserJsonp {
|
|
|
|
src: string;
|
2015-09-29 14:11:06 -04:00
|
|
|
callbacks = new Map<string, (data: any) => any>();
|
|
|
|
constructor() { super(); }
|
2015-07-14 20:53:04 -04: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 20:53:04 -04: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 20:53:04 -04: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 20:05:50 -05:00
|
|
|
let backend: JSONPBackend_;
|
|
|
|
let sampleRequest: Request;
|
2015-07-14 20:53:04 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-04-14 15:35:24 -04:00
|
|
|
let injector = ReflectiveInjector.resolveAndCreate([
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: ResponseOptions, useClass: BaseResponseOptions},
|
|
|
|
{provide: BrowserJsonp, useClass: MockBrowserJsonp},
|
|
|
|
{provide: JSONPBackend, useClass: JSONPBackend_}
|
2015-07-14 20:53:04 -04: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 20:05:50 -05:00
|
|
|
var instance: JSONPConnection;
|
2015-07-14 20:53:04 -04: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 20:05:50 -05:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2016-06-08 19:38:52 -04: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 16:44:14 -05:00
|
|
|
expect(res.type).toBe(ResponseType.Error);
|
2015-07-14 20:53:04 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
connection.finished();
|
|
|
|
existingScripts[0].dispatchEvent('load');
|
|
|
|
}));
|
|
|
|
|
2016-02-01 20:05:50 -05:00
|
|
|
it('should ignore load/callback when disposed',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-10-06 09:53:39 -04:00
|
|
|
var connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
2015-07-14 20:53:04 -04: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 20:53:04 -04:00
|
|
|
|
|
|
|
connection.finished('Fake data');
|
|
|
|
existingScripts[0].dispatchEvent('load');
|
|
|
|
|
|
|
|
TimerWrapper.setTimeout(() => {
|
2015-12-03 16:44:14 -05:00
|
|
|
expect(connection.readyState).toBe(ReadyState.Cancelled);
|
2015-07-14 20:53:04 -04:00
|
|
|
expect(loadSpy).not.toHaveBeenCalled();
|
|
|
|
expect(errorSpy).not.toHaveBeenCalled();
|
2015-09-25 18:53:32 -04:00
|
|
|
expect(returnSpy).not.toHaveBeenCalled();
|
2015-07-14 20:53:04 -04:00
|
|
|
async.done();
|
|
|
|
}, 10);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should report error if loaded without invoking callback',
|
2016-02-01 20:05:50 -05:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-10-06 09:53:39 -04:00
|
|
|
let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
2015-09-25 18:53:32 -04:00
|
|
|
connection.response.subscribe(
|
2015-07-14 20:53:04 -04:00
|
|
|
res => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect('response listener called').toBe(false);
|
2015-07-14 20:53:04 -04:00
|
|
|
async.done();
|
|
|
|
},
|
|
|
|
err => {
|
2015-10-29 20:50:12 -04:00
|
|
|
expect(err.text()).toEqual('JSONP injected script did not invoke callback.');
|
2015-07-14 20:53:04 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
existingScripts[0].dispatchEvent('load');
|
|
|
|
}));
|
|
|
|
|
2016-02-01 20:05:50 -05:00
|
|
|
it('should report error if script contains error',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-10-06 09:53:39 -04:00
|
|
|
let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
connection.response.subscribe(
|
|
|
|
res => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect('response listener called').toBe(false);
|
2015-09-25 18:53:32 -04:00
|
|
|
async.done();
|
|
|
|
},
|
|
|
|
err => {
|
2015-10-29 20:50:12 -04:00
|
|
|
expect(err.text()).toBe('Oops!');
|
2015-09-25 18:53:32 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
existingScripts[0].dispatchEvent('error', ({message: 'Oops!'}));
|
2015-07-14 20:53:04 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should throw if request method is not GET', () => {
|
2015-12-03 16:44:14 -05:00
|
|
|
[RequestMethod.Post, RequestMethod.Put, RequestMethod.Delete, RequestMethod.Options,
|
|
|
|
RequestMethod.Head, RequestMethod.Patch]
|
2015-07-14 20:53:04 -04:00
|
|
|
.forEach(method => {
|
|
|
|
let base = new BaseRequestOptions();
|
|
|
|
let req = new Request(
|
|
|
|
base.merge(new RequestOptions({url: 'https://google.com', method: method})));
|
2015-10-06 09:53:39 -04:00
|
|
|
expect(() => new JSONPConnection_(req, new MockBrowserJsonp()).response.subscribe())
|
2015-09-25 18:53:32 -04:00
|
|
|
.toThrowError();
|
2015-07-14 20:53:04 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-01 20:05:50 -05:00
|
|
|
it('should respond with data passed to callback',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-10-06 09:53:39 -04:00
|
|
|
let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp());
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
connection.response.subscribe(res => {
|
2015-07-14 20:53:04 -04: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');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|