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
|
|
|
|
*/
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
import {Injector} from '@angular/core';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal';
|
2017-12-17 18:10:54 -05:00
|
|
|
import {BrowserJsonp} from '@angular/http/src/backends/browser_jsonp';
|
|
|
|
import {JSONPBackend, JSONPConnection} from '@angular/http/src/backends/jsonp_backend';
|
|
|
|
import {BaseRequestOptions, RequestOptions} from '@angular/http/src/base_request_options';
|
|
|
|
import {BaseResponseOptions, ResponseOptions} from '@angular/http/src/base_response_options';
|
|
|
|
import {ReadyState, RequestMethod, ResponseType} from '@angular/http/src/enums';
|
|
|
|
import {Request} from '@angular/http/src/static_request';
|
|
|
|
import {Response} from '@angular/http/src/static_response';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
let existingScripts: MockBrowserJsonp[] = [];
|
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>();
|
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); }
|
|
|
|
|
2016-11-15 12:19:14 -05:00
|
|
|
dispatchEvent(type: string, argument: any = {}) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const cb = this.callbacks.get(type);
|
2016-11-15 12:19:14 -05:00
|
|
|
if (cb) {
|
2015-09-25 18:53:32 -04:00
|
|
|
cb(argument);
|
|
|
|
}
|
2015-07-14 20:53:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
build(url: string) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const script = new MockBrowserJsonp();
|
2015-07-14 20:53:04 -04:00
|
|
|
script.src = url;
|
|
|
|
existingScripts.push(script);
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
|
|
|
send(node: any) { /* noop */
|
|
|
|
}
|
|
|
|
cleanup(node: any) { /* noop */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2015-07-14 20:53:04 -04:00
|
|
|
describe('JSONPBackend', () => {
|
2017-09-12 14:45:02 -04:00
|
|
|
let backend: JSONPBackend;
|
2016-02-01 20:05:50 -05:00
|
|
|
let sampleRequest: Request;
|
2015-07-14 20:53:04 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 15:33:29 -04:00
|
|
|
const injector = Injector.create([
|
|
|
|
{provide: ResponseOptions, useClass: BaseResponseOptions, deps: []},
|
|
|
|
{provide: BrowserJsonp, useClass: MockBrowserJsonp, deps: []},
|
2017-09-12 14:45:02 -04:00
|
|
|
{provide: JSONPBackend, useClass: JSONPBackend, deps: [BrowserJsonp, ResponseOptions]}
|
2015-07-14 20:53:04 -04:00
|
|
|
]);
|
|
|
|
backend = injector.get(JSONPBackend);
|
2016-11-12 08:08:58 -05:00
|
|
|
const base = new BaseRequestOptions();
|
2017-04-17 14:12:53 -04:00
|
|
|
sampleRequest =
|
|
|
|
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
|
2015-07-14 20:53:04 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => { existingScripts = []; });
|
|
|
|
|
|
|
|
it('should create a connection', () => {
|
2017-04-17 14:12:53 -04:00
|
|
|
let instance: JSONPConnection = undefined !;
|
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) => {
|
2017-12-17 18:10:54 -05:00
|
|
|
const connection = new (JSONPConnection as any)(
|
2016-06-08 19:38:52 -04:00
|
|
|
sampleRequest, new MockBrowserJsonp(),
|
|
|
|
new ResponseOptions({type: ResponseType.Error}));
|
2017-12-17 18:10:54 -05:00
|
|
|
connection.response.subscribe((res: Response) => {
|
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) => {
|
2017-12-17 18:10:54 -05:00
|
|
|
const connection = new (JSONPConnection as any)(sampleRequest, new MockBrowserJsonp());
|
2016-11-12 08:08:58 -05:00
|
|
|
const spy = new SpyObject();
|
|
|
|
const loadSpy = spy.spy('load');
|
|
|
|
const errorSpy = spy.spy('error');
|
|
|
|
const returnSpy = spy.spy('cancelled');
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const request = connection.response.subscribe(loadSpy, errorSpy, returnSpy);
|
2015-09-25 18:53:32 -04:00
|
|
|
request.unsubscribe();
|
2015-07-14 20:53:04 -04:00
|
|
|
|
|
|
|
connection.finished('Fake data');
|
|
|
|
existingScripts[0].dispatchEvent('load');
|
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
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) => {
|
2017-12-17 18:10:54 -05:00
|
|
|
const connection = new (JSONPConnection as any)(sampleRequest, new MockBrowserJsonp());
|
2015-09-25 18:53:32 -04:00
|
|
|
connection.response.subscribe(
|
2017-12-17 18:10:54 -05:00
|
|
|
(res: Response) => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect('response listener called').toBe(false);
|
2015-07-14 20:53:04 -04:00
|
|
|
async.done();
|
|
|
|
},
|
2017-12-17 18:10:54 -05:00
|
|
|
(err: Response) => {
|
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) => {
|
2017-12-17 18:10:54 -05:00
|
|
|
const connection = new (JSONPConnection as any)(sampleRequest, new MockBrowserJsonp());
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2015-09-25 18:53:32 -04:00
|
|
|
connection.response.subscribe(
|
2017-12-17 18:10:54 -05:00
|
|
|
(res: Response) => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect('response listener called').toBe(false);
|
2015-09-25 18:53:32 -04:00
|
|
|
async.done();
|
|
|
|
},
|
2017-12-17 18:10:54 -05:00
|
|
|
(err: Response) => {
|
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 => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const base = new BaseRequestOptions();
|
2017-04-17 14:12:53 -04:00
|
|
|
const req = new Request(base.merge(
|
|
|
|
new RequestOptions({url: 'https://google.com', method: method})) as any);
|
2017-12-17 18:10:54 -05:00
|
|
|
expect(
|
|
|
|
() => new (JSONPConnection as any)(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) => {
|
2017-12-17 18:10:54 -05:00
|
|
|
const connection = new (JSONPConnection as any)(sampleRequest, new MockBrowserJsonp());
|
2015-07-14 20:53:04 -04:00
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
connection.response.subscribe((res: Response) => {
|
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');
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|