fix(http): Update types for TypeScript nullability support

This commit is contained in:
Miško Hevery
2017-03-24 09:55:16 -07:00
committed by Tobias Bosch
parent 86396a43e9
commit c36ec9bf60
18 changed files with 138 additions and 128 deletions
@@ -59,13 +59,14 @@ export function main() {
]);
backend = injector.get(JSONPBackend);
const base = new BaseRequestOptions();
sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
sampleRequest =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
});
afterEach(() => { existingScripts = []; });
it('should create a connection', () => {
let instance: JSONPConnection;
let instance: JSONPConnection = undefined !;
expect(() => instance = backend.createConnection(sampleRequest)).not.toThrow();
expect(instance).toBeAnInstanceOf(JSONPConnection);
});
@@ -146,8 +147,8 @@ export function main() {
RequestMethod.Head, RequestMethod.Patch]
.forEach(method => {
const base = new BaseRequestOptions();
const req = new Request(
base.merge(new RequestOptions({url: 'https://google.com', method: method})));
const req = new Request(base.merge(
new RequestOptions({url: 'https://google.com', method: method})) as any);
expect(() => new JSONPConnection_(req, new MockBrowserJsonp()).response.subscribe())
.toThrowError();
});
@@ -31,9 +31,11 @@ export function main() {
[{provide: ResponseOptions, useClass: BaseResponseOptions}, MockBackend]);
backend = injector.get(MockBackend);
const base = new BaseRequestOptions();
sampleRequest1 = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
sampleRequest1 =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
sampleResponse1 = new Response(new ResponseOptions({body: 'response1'}));
sampleRequest2 = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
sampleRequest2 =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
sampleResponse2 = new Response(new ResponseOptions({body: 'response2'}));
});
@@ -65,7 +67,7 @@ export function main() {
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'));
}));
@@ -98,12 +100,12 @@ export function main() {
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?
+27 -23
View File
@@ -75,7 +75,7 @@ class MockBrowserXHR extends BrowserXhr {
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 +99,8 @@ export function main() {
beforeEach(inject([XHRBackend], (be: XHRBackend) => {
backend = be;
const base = new BaseRequestOptions();
sampleRequest = new Request(base.merge(new RequestOptions({url: 'https://google.com'})));
sampleRequest =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
}));
afterEach(() => { existingXHRs = []; });
@@ -163,7 +164,7 @@ export function main() {
sampleRequest, new MockBrowserXHR(),
new ResponseOptions({type: ResponseType.Error}));
connection.response.subscribe(
(res: Response) => { expect(res.type).toBe(ResponseType.Error); }, null,
(res: Response) => { expect(res.type).toBe(ResponseType.Error); }, null !,
() => { async.done(); });
existingXHRs[0].setStatusCode(200);
existingXHRs[0].dispatchEvent('load');
@@ -181,7 +182,7 @@ export function main() {
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();
});
@@ -193,7 +194,7 @@ export function main() {
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('');
@@ -217,7 +218,7 @@ export function main() {
const body = 'Some body to love';
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR());
expect(sendSpy).not.toHaveBeenCalled();
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith(body);
@@ -229,7 +230,8 @@ export function main() {
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({headers: headers}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({headers: headers})) as any),
new MockBrowserXHR());
connection.response.subscribe();
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/xml');
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Breaking-Bad', '<3');
@@ -240,7 +242,7 @@ export function main() {
const headers = new Headers();
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({headers})) as any), new MockBrowserXHR());
connection.response.subscribe();
expect(setRequestHeaderSpy)
.toHaveBeenCalledWith('Accept', 'application/json, text/plain, */*');
@@ -250,7 +252,7 @@ export function main() {
const headers = new Headers({'Accept': 'text/xml'});
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({headers}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({headers})) as any), new MockBrowserXHR());
connection.response.subscribe();
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Accept', 'text/xml');
});
@@ -260,7 +262,7 @@ export function main() {
const body = {test: 'val'};
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body, headers: headers}))),
new Request(base.merge(new RequestOptions({body: body, headers: headers})) as any),
new MockBrowserXHR());
connection.response.subscribe();
expect(setRequestHeaderSpy).toHaveBeenCalledWith('Content-Type', 'text/plain');
@@ -272,7 +274,7 @@ export function main() {
const body = {test: 'val'};
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith(JSON.stringify(body, null, 2));
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'application/json');
@@ -282,7 +284,7 @@ export function main() {
const body = 23;
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith('23');
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/plain');
@@ -292,7 +294,7 @@ export function main() {
const body = 'some string';
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith(body);
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/plain');
@@ -304,7 +306,7 @@ export function main() {
body.set('test2', 'val2');
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any), new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith('test1=val1&test2=val2');
expect(setRequestHeaderSpy)
@@ -337,7 +339,8 @@ export function main() {
body.append('userfile', blob);
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any),
new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith(body);
expect(setRequestHeaderSpy).not.toHaveBeenCalledWith();
@@ -347,14 +350,15 @@ export function main() {
const body = createBlob(['body { color: red; }'], 'text/css');
const base = new BaseRequestOptions();
const connection = new XHRConnection(
new Request(base.merge(new RequestOptions({body: body}))), new MockBrowserXHR());
new Request(base.merge(new RequestOptions({body: body})) as any),
new MockBrowserXHR());
connection.response.subscribe();
expect(sendSpy).toHaveBeenCalledWith(body);
expect(setRequestHeaderSpy).toHaveBeenCalledWith('content-type', 'text/css');
});
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());
@@ -366,7 +370,7 @@ export function main() {
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}))),
@@ -590,7 +594,7 @@ export function main() {
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();
});
@@ -611,10 +615,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();
});
+2 -2
View File
@@ -175,7 +175,7 @@ export function main() {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
http.request('http://basic.connection')
.subscribe(
(res: Response) => { expect(res.text()).toBe('base response'); }, null,
(res: Response) => { expect(res.text()).toBe('base response'); }, null !,
() => { async.done(); });
}));
@@ -188,7 +188,7 @@ export function main() {
});
http.request('http://basic.connection')
.subscribe(
(res: Response) => { expect(res.text()).toBe('base response'); }, null,
(res: Response) => { expect(res.text()).toBe('base response'); }, null !,
() => { async.done(); });
}));
+10 -9
View File
@@ -17,7 +17,8 @@ export function main() {
describe('Request', () => {
describe('detectContentType', () => {
it('should return ContentType.NONE', () => {
const req = new Request(new RequestOptions({url: 'test', method: 'GET', body: null}));
const req =
new Request(new RequestOptions({url: 'test', method: 'GET', body: null}) as any);
expect(req.detectContentType()).toEqual(ContentType.NONE);
});
@@ -28,7 +29,7 @@ export function main() {
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/json'})
}));
}) as any);
expect(req.detectContentType()).toEqual(ContentType.JSON);
});
@@ -39,7 +40,7 @@ export function main() {
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/x-www-form-urlencoded'})
}));
}) as any);
expect(req.detectContentType()).toEqual(ContentType.FORM);
});
@@ -50,7 +51,7 @@ export function main() {
method: 'GET',
body: null,
headers: new Headers({'content-type': 'multipart/form-data'})
}));
}) as any);
expect(req.detectContentType()).toEqual(ContentType.FORM_DATA);
});
@@ -61,7 +62,7 @@ export function main() {
method: 'GET',
body: null,
headers: new Headers({'content-type': 'text/plain'})
}));
}) as any);
expect(req.detectContentType()).toEqual(ContentType.TEXT);
});
@@ -72,7 +73,7 @@ export function main() {
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/octet-stream'})
}));
}) as any);
expect(req.detectContentType()).toEqual(ContentType.BLOB);
});
@@ -83,7 +84,7 @@ export function main() {
method: 'GET',
body: new ArrayBuffer(1),
headers: new Headers({'content-type': 'application/octet-stream'})
}));
}) as any);
expect(req.detectContentType()).toEqual(ContentType.ARRAY_BUFFER);
});
@@ -95,7 +96,7 @@ export function main() {
method: 'GET',
body: null,
headers: new Headers({'content-type': 'application/json'})
}));
}) as any);
expect(req.text()).toEqual('');
});
@@ -104,7 +105,7 @@ export function main() {
const reqOptions = new RequestOptions(
{url: 'test', method: 'GET', headers: new Headers({'content-type': 'application/json'})});
delete reqOptions.body;
const req = new Request(reqOptions);
const req = new Request(reqOptions as any);
expect(req.text()).toEqual('');
});
+4 -4
View File
@@ -151,19 +151,19 @@ export function main() {
it('should remove the parameter when set to undefined or null', () => {
const params = new URLSearchParams('q=Q');
params.set('q', undefined);
params.set('q', undefined !);
expect(params.has('q')).toBe(false);
expect(params.toString()).toEqual('');
params.set('q', null);
params.set('q', null !);
expect(params.has('q')).toBe(false);
expect(params.toString()).toEqual('');
});
it('should ignore the value when append undefined or null', () => {
const params = new URLSearchParams('q=Q');
params.append('q', undefined);
params.append('q', undefined !);
expect(params.toString()).toEqual('q=Q');
params.append('q', null);
params.append('q', null !);
expect(params.toString()).toEqual('q=Q');
});