@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import {Injector} from '@angular/core';
|
||||
import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal';
|
||||
import {afterEach, AsyncTestCompleter, beforeEach, describe, inject, it, SpyObject} from '@angular/core/testing/src/testing_internal';
|
||||
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';
|
||||
@ -21,12 +21,16 @@ let existingScripts: MockBrowserJsonp[] = [];
|
||||
|
||||
class MockBrowserJsonp extends BrowserJsonp {
|
||||
// TODO(issue/24571): remove '!'.
|
||||
src !: string;
|
||||
src!: string;
|
||||
callbacks = new Map<string, (data: any) => any>();
|
||||
|
||||
addEventListener(type: string, cb: (data: any) => any) { this.callbacks.set(type, cb); }
|
||||
addEventListener(type: string, cb: (data: any) => any) {
|
||||
this.callbacks.set(type, cb);
|
||||
}
|
||||
|
||||
removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); }
|
||||
removeEventListener(type: string, cb: Function) {
|
||||
this.callbacks.delete(type);
|
||||
}
|
||||
|
||||
dispatchEvent(type: string, argument: any = {}) {
|
||||
const cb = this.callbacks.get(type);
|
||||
@ -65,10 +69,12 @@ class MockBrowserJsonp extends BrowserJsonp {
|
||||
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
|
||||
});
|
||||
|
||||
afterEach(() => { existingScripts = []; });
|
||||
afterEach(() => {
|
||||
existingScripts = [];
|
||||
});
|
||||
|
||||
it('should create a connection', () => {
|
||||
let instance: JSONPConnection = undefined !;
|
||||
let instance: JSONPConnection = undefined!;
|
||||
expect(() => instance = backend.createConnection(sampleRequest)).not.toThrow();
|
||||
expect(instance).toBeAnInstanceOf(JSONPConnection);
|
||||
});
|
||||
@ -141,8 +147,9 @@ class MockBrowserJsonp extends BrowserJsonp {
|
||||
RequestMethod.Head, RequestMethod.Patch]
|
||||
.forEach(method => {
|
||||
const base = new BaseRequestOptions();
|
||||
const req = new Request(base.merge(
|
||||
new RequestOptions({url: 'https://google.com', method: method})) as any);
|
||||
const req = new Request(
|
||||
base.merge(new RequestOptions({url: 'https://google.com', method: method})) as
|
||||
any);
|
||||
expect(
|
||||
() => new (JSONPConnection as any)(req, new MockBrowserJsonp())
|
||||
.response.subscribe())
|
||||
|
@ -18,7 +18,6 @@ import {ReplaySubject} from 'rxjs';
|
||||
|
||||
{
|
||||
describe('MockBackend', () => {
|
||||
|
||||
let backend: MockBackend;
|
||||
let sampleRequest1: Request;
|
||||
let sampleResponse1: Response;
|
||||
@ -40,7 +39,9 @@ import {ReplaySubject} from 'rxjs';
|
||||
sampleResponse2 = new Response(new ResponseOptions({body: 'response2'}));
|
||||
});
|
||||
|
||||
it('should create a new MockBackend', () => { expect(backend).toBeAnInstanceOf(MockBackend); });
|
||||
it('should create a new MockBackend', () => {
|
||||
expect(backend).toBeAnInstanceOf(MockBackend);
|
||||
});
|
||||
|
||||
it('should create a new MockConnection', () => {
|
||||
expect(backend.createConnection(sampleRequest1)).toBeAnInstanceOf(MockConnection);
|
||||
@ -54,7 +55,9 @@ import {ReplaySubject} from 'rxjs';
|
||||
it('should allow responding after subscription',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
connection.response.subscribe(() => {
|
||||
async.done();
|
||||
});
|
||||
connection.mockRespond(sampleResponse1);
|
||||
}));
|
||||
|
||||
@ -62,20 +65,26 @@ import {ReplaySubject} from 'rxjs';
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.mockRespond(sampleResponse1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
connection.response.subscribe(() => {
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should allow responding after subscription with an error',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(null !, () => { async.done(); });
|
||||
connection.response.subscribe(null!, () => {
|
||||
async.done();
|
||||
});
|
||||
connection.mockError(new Error('nope'));
|
||||
}));
|
||||
|
||||
it('should not throw when there are no unresolved requests',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
connection.response.subscribe(() => {
|
||||
async.done();
|
||||
});
|
||||
connection.mockRespond(sampleResponse1);
|
||||
backend.verifyNoPendingRequests();
|
||||
}));
|
||||
@ -83,7 +92,9 @@ import {ReplaySubject} from 'rxjs';
|
||||
xit('should throw when there are unresolved requests',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection.response.subscribe(() => { async.done(); });
|
||||
connection.response.subscribe(() => {
|
||||
async.done();
|
||||
});
|
||||
backend.verifyNoPendingRequests();
|
||||
}));
|
||||
|
||||
@ -91,7 +102,9 @@ import {ReplaySubject} from 'rxjs';
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const connection1: MockConnection = backend.createConnection(sampleRequest1);
|
||||
const connection2: MockConnection = backend.createConnection(sampleRequest1);
|
||||
connection1.response.subscribe(() => { async.done(); });
|
||||
connection1.response.subscribe(() => {
|
||||
async.done();
|
||||
});
|
||||
connection2.response.subscribe(() => {});
|
||||
connection2.mockRespond(sampleResponse1);
|
||||
connection1.mockRespond(sampleResponse1);
|
||||
@ -101,12 +114,12 @@ import {ReplaySubject} from 'rxjs';
|
||||
xit('should allow double subscribing',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const responses: Response[] = [sampleResponse1, sampleResponse2];
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(responses.shift() !));
|
||||
backend.connections.subscribe((c: MockConnection) => c.mockRespond(responses.shift()!));
|
||||
const responseObservable: ReplaySubject<Response> =
|
||||
backend.createConnection(sampleRequest1).response;
|
||||
responseObservable.subscribe(res => expect(res.text()).toBe('response1'));
|
||||
responseObservable.subscribe(
|
||||
res => expect(res.text()).toBe('response2'), null !, async.done);
|
||||
res => expect(res.text()).toBe('response2'), null!, async.done);
|
||||
}));
|
||||
|
||||
// TODO(robwormald): readyStates are leaving?
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
import {ɵgetDOM as getDOM} from '@angular/common';
|
||||
import {Injectable} from '@angular/core';
|
||||
import {AsyncTestCompleter, SpyObject, afterEach, beforeEach, beforeEachProviders, describe, expect, inject, it} from '@angular/core/testing/src/testing_internal';
|
||||
import {afterEach, AsyncTestCompleter, beforeEach, beforeEachProviders, describe, expect, inject, it, SpyObject} from '@angular/core/testing/src/testing_internal';
|
||||
import {BrowserXhr} from '@angular/http/src/backends/browser_xhr';
|
||||
import {CookieXSRFStrategy, XHRBackend, XHRConnection} from '@angular/http/src/backends/xhr_backend';
|
||||
import {BaseRequestOptions, RequestOptions} from '@angular/http/src/base_request_options';
|
||||
@ -34,19 +34,19 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
response: any;
|
||||
responseType: string;
|
||||
// TODO(issue/24571): remove '!'.
|
||||
responseText !: string;
|
||||
responseText!: string;
|
||||
setRequestHeader: any;
|
||||
callbacks = new Map<string, Function>();
|
||||
// TODO(issue/24571): remove '!'.
|
||||
status !: number;
|
||||
status!: number;
|
||||
// TODO(issue/24571): remove '!'.
|
||||
responseHeaders !: string;
|
||||
responseHeaders!: string;
|
||||
// TODO(issue/24571): remove '!'.
|
||||
responseURL !: string;
|
||||
responseURL!: string;
|
||||
// TODO(issue/24571): remove '!'.
|
||||
statusText !: string;
|
||||
statusText!: string;
|
||||
// TODO(issue/24571): remove '!'.
|
||||
withCredentials !: boolean;
|
||||
withCredentials!: boolean;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
@ -60,29 +60,49 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
this.responseType = '';
|
||||
}
|
||||
|
||||
setStatusCode(status: number) { this.status = status; }
|
||||
setStatusCode(status: number) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
setStatusText(statusText: string) { this.statusText = statusText; }
|
||||
setStatusText(statusText: string) {
|
||||
this.statusText = statusText;
|
||||
}
|
||||
|
||||
setResponse(value: string) { this.response = value; }
|
||||
setResponse(value: string) {
|
||||
this.response = value;
|
||||
}
|
||||
|
||||
setResponseText(value: string) { this.responseText = value; }
|
||||
setResponseText(value: string) {
|
||||
this.responseText = value;
|
||||
}
|
||||
|
||||
setResponseURL(value: string) { this.responseURL = value; }
|
||||
setResponseURL(value: string) {
|
||||
this.responseURL = value;
|
||||
}
|
||||
|
||||
setResponseHeaders(value: string) { this.responseHeaders = value; }
|
||||
setResponseHeaders(value: string) {
|
||||
this.responseHeaders = value;
|
||||
}
|
||||
|
||||
getAllResponseHeaders() { return this.responseHeaders || ''; }
|
||||
getAllResponseHeaders() {
|
||||
return this.responseHeaders || '';
|
||||
}
|
||||
|
||||
getResponseHeader(key: string) {
|
||||
return Headers.fromResponseHeaderString(this.responseHeaders).get(key);
|
||||
}
|
||||
|
||||
addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); }
|
||||
addEventListener(type: string, cb: Function) {
|
||||
this.callbacks.set(type, cb);
|
||||
}
|
||||
|
||||
removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); }
|
||||
removeEventListener(type: string, cb: Function) {
|
||||
this.callbacks.delete(type);
|
||||
}
|
||||
|
||||
dispatchEvent(type: string) { this.callbacks.get(type) !({}); }
|
||||
dispatchEvent(type: string) {
|
||||
this.callbacks.get(type)!({});
|
||||
}
|
||||
|
||||
build() {
|
||||
const xhr = new MockBrowserXHR();
|
||||
@ -99,7 +119,8 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
beforeEachProviders(
|
||||
() =>
|
||||
[{provide: ResponseOptions, useClass: BaseResponseOptions},
|
||||
{provide: BrowserXhr, useClass: MockBrowserXHR}, XHRBackend,
|
||||
{provide: BrowserXhr, useClass: MockBrowserXHR},
|
||||
XHRBackend,
|
||||
{provide: XSRFStrategy, useValue: new CookieXSRFStrategy()},
|
||||
]);
|
||||
|
||||
@ -110,7 +131,9 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
|
||||
}));
|
||||
|
||||
afterEach(() => { existingXHRs = []; });
|
||||
afterEach(() => {
|
||||
existingXHRs = [];
|
||||
});
|
||||
|
||||
describe('creating a connection', () => {
|
||||
@Injectable()
|
||||
@ -119,8 +142,9 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
}
|
||||
beforeEachProviders(() => [{provide: XSRFStrategy, useClass: NoopXsrfStrategy}]);
|
||||
|
||||
it('succeeds',
|
||||
() => { expect(() => backend.createConnection(sampleRequest)).not.toThrow(); });
|
||||
it('succeeds', () => {
|
||||
expect(() => backend.createConnection(sampleRequest)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
if (getDOM().supportsCookies()) {
|
||||
@ -171,8 +195,13 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(
|
||||
(res: Response) => { expect(res.type).toBe(ResponseType.Error); }, null !,
|
||||
() => { async.done(); });
|
||||
(res: Response) => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
},
|
||||
null!,
|
||||
() => {
|
||||
async.done();
|
||||
});
|
||||
existingXHRs[0].setStatusCode(200);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
}));
|
||||
@ -189,7 +218,7 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(null !, (res: Response) => {
|
||||
connection.response.subscribe(null!, (res: Response) => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
async.done();
|
||||
});
|
||||
@ -201,7 +230,7 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
const connection = new XHRConnection(
|
||||
sampleRequest, new MockBrowserXHR(),
|
||||
new ResponseOptions({type: ResponseType.Error}));
|
||||
connection.response.subscribe(null !, (res: Response) => {
|
||||
connection.response.subscribe(null!, (res: Response) => {
|
||||
expect(res.type).toBe(ResponseType.Error);
|
||||
expect(res.status).toEqual(0);
|
||||
expect(res.statusText).toEqual('');
|
||||
@ -365,7 +394,7 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
});
|
||||
|
||||
it('should use blob body without type to the request', () => {
|
||||
const body = createBlob(['body { color: red; }'], null !);
|
||||
const body = createBlob(['body { color: red; }'], null!);
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
|
||||
@ -377,7 +406,7 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
it('should use blob body without type with custom content type header to the request',
|
||||
() => {
|
||||
const headers = new Headers({'Content-Type': 'text/css'});
|
||||
const body = createBlob(['body { color: red; }'], null !);
|
||||
const body = createBlob(['body { color: red; }'], null!);
|
||||
const base = new BaseRequestOptions();
|
||||
const connection = new XHRConnection(
|
||||
new Request(base.merge(new RequestOptions({body: body, headers: headers}))),
|
||||
@ -451,7 +480,9 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
nextCalled = true;
|
||||
expect(res.status).toBe(statusCode);
|
||||
},
|
||||
(errRes: Response) => { errorCalled = true; },
|
||||
(errRes: Response) => {
|
||||
errorCalled = true;
|
||||
},
|
||||
() => {
|
||||
expect(nextCalled).toBe(true);
|
||||
expect(errorCalled).toBe(false);
|
||||
@ -484,7 +515,9 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(
|
||||
(res: Response) => { throw 'should not be called'; },
|
||||
(res: Response) => {
|
||||
throw 'should not be called';
|
||||
},
|
||||
(errRes: Response) => {
|
||||
expect(errRes.ok).toBe(false);
|
||||
async.done();
|
||||
@ -503,13 +536,17 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
sampleRequest, new MockBrowserXHR(), new ResponseOptions({status: statusCode}));
|
||||
|
||||
connection.response.subscribe(
|
||||
(res: Response) => { nextCalled = true; },
|
||||
(res: Response) => {
|
||||
nextCalled = true;
|
||||
},
|
||||
(errRes: Response) => {
|
||||
expect(errRes.status).toBe(statusCode);
|
||||
expect(nextCalled).toBe(false);
|
||||
async.done();
|
||||
},
|
||||
() => { throw 'should not be called'; });
|
||||
() => {
|
||||
throw 'should not be called';
|
||||
});
|
||||
|
||||
existingXHRs[0].setStatusCode(statusCode);
|
||||
existingXHRs[0].dispatchEvent('load');
|
||||
@ -601,7 +638,7 @@ class MockBrowserXHR extends BrowserXhr {
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
const conn =
|
||||
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||
conn.response.subscribe(null !, (res: Response) => {
|
||||
conn.response.subscribe(null!, (res: Response) => {
|
||||
expect(res.text()).toBe('{json: "object"}');
|
||||
async.done();
|
||||
});
|
||||
@ -622,10 +659,10 @@ Transfer-Encoding: chunked
|
||||
Connection: keep-alive`;
|
||||
|
||||
connection.response.subscribe((res: Response) => {
|
||||
expect(res.headers !.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT');
|
||||
expect(res.headers !.get('Content-Type')).toEqual('application/json; charset=utf-8');
|
||||
expect(res.headers !.get('Transfer-Encoding')).toEqual('chunked');
|
||||
expect(res.headers !.get('Connection')).toEqual('keep-alive');
|
||||
expect(res.headers!.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT');
|
||||
expect(res.headers!.get('Content-Type')).toEqual('application/json; charset=utf-8');
|
||||
expect(res.headers!.get('Transfer-Encoding')).toEqual('chunked');
|
||||
expect(res.headers!.get('Connection')).toEqual('keep-alive');
|
||||
async.done();
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user