fix(http): return Response headers

Properly parse and add response Headers to Response.

Closes #5237
This commit is contained in:
Rob Wormald
2015-11-19 17:51:00 -08:00
parent 201f189d0e
commit 4332ccf72c
4 changed files with 64 additions and 3 deletions
@@ -40,6 +40,7 @@ class MockBrowserXHR extends BrowserXhr {
setRequestHeader: any;
callbacks = new Map<string, Function>();
status: number;
responseHeaders: string;
constructor() {
super();
var spy = new SpyObject();
@@ -55,6 +56,10 @@ class MockBrowserXHR extends BrowserXhr {
setResponseText(value) { this.responseText = value; }
setResponseHeaders(value) { this.responseHeaders = value; }
getAllResponseHeaders() { return this.responseHeaders || ''; }
addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); }
removeEventListener(type: string, cb: Function) { this.callbacks.delete(type); }
@@ -256,6 +261,30 @@ export function main() {
existingXHRs[0].dispatchEvent('load');
}));
it('should parse response headers and add them to the response',
inject([AsyncTestCompleter], async => {
var statusCode = 200;
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
new ResponseOptions({status: statusCode}));
let responseHeaderString =
`Date: Fri, 20 Nov 2015 01:45:26 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive`
connection.response.subscribe(res => {
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();
});
existingXHRs[0].setResponseHeaders(responseHeaderString);
existingXHRs[0].setStatusCode(statusCode);
existingXHRs[0].dispatchEvent('load');
}));
});
});
}
@@ -63,4 +63,23 @@ export function main() {
});
});
});
describe('.fromResponseHeaderString()', () => {
it('should parse a response header string', () => {
let responseHeaderString = `Date: Fri, 20 Nov 2015 01:45:26 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive`;
let responseHeaders = Headers.fromResponseHeaderString(responseHeaderString);
expect(responseHeaders.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT');
expect(responseHeaders.get('Content-Type')).toEqual('application/json; charset=utf-8');
expect(responseHeaders.get('Transfer-Encoding')).toEqual('chunked');
expect(responseHeaders.get('Connection')).toEqual('keep-alive');
});
});
}