test(XHRConnection): normalize responseText and response
normalize xhr.responseText and xhr.response - [x] Tests Closes #2882
This commit is contained in:
parent
96eefdfebc
commit
cfedc77ce1
|
@ -1,5 +1,6 @@
|
||||||
import {Injectable} from 'angular2/di';
|
import {Injectable} from 'angular2/di';
|
||||||
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/facade/async';
|
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/facade/async';
|
||||||
|
import {isPresent} from 'angular2/src/facade/lang';
|
||||||
import {XHR} from './xhr';
|
import {XHR} from './xhr';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -13,7 +14,7 @@ export class XHRImpl extends XHR {
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
||||||
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
|
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
|
||||||
var response = ('response' in xhr) ? xhr.response : xhr.responseText;
|
var response = isPresent(xhr.response) ? xhr.response : xhr.responseText;
|
||||||
|
|
||||||
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
||||||
var status = xhr.status === 1223 ? 204 : xhr.status;
|
var status = xhr.status === 1223 ? 204 : xhr.status;
|
||||||
|
|
|
@ -51,6 +51,11 @@ class MockBrowserXHR extends BrowserXhr {
|
||||||
}
|
}
|
||||||
|
|
||||||
setStatusCode(status) { this.status = status; }
|
setStatusCode(status) { this.status = status; }
|
||||||
|
|
||||||
|
setResponse(value) { this.response = value; }
|
||||||
|
|
||||||
|
setResponseText(value) { this.responseText = value; }
|
||||||
|
|
||||||
addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); }
|
addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); }
|
||||||
|
|
||||||
dispatchEvent(type: string) { this.callbacks.get(type)({}); }
|
dispatchEvent(type: string) { this.callbacks.get(type)({}); }
|
||||||
|
@ -143,7 +148,7 @@ export function main() {
|
||||||
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
|
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
|
||||||
new ResponseOptions({status: statusCode}));
|
new ResponseOptions({status: statusCode}));
|
||||||
|
|
||||||
ObservableWrapper.subscribe(connection.response, res => {
|
ObservableWrapper.subscribe<Response>(connection.response, res => {
|
||||||
expect(res.status).toBe(statusCode);
|
expect(res.status).toBe(statusCode);
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -158,7 +163,7 @@ export function main() {
|
||||||
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
|
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
|
||||||
new ResponseOptions({status: statusCode}));
|
new ResponseOptions({status: statusCode}));
|
||||||
|
|
||||||
ObservableWrapper.subscribe(connection.response, res => {
|
ObservableWrapper.subscribe<Response>(connection.response, res => {
|
||||||
expect(res.status).toBe(normalizedCode);
|
expect(res.status).toBe(normalizedCode);
|
||||||
async.done();
|
async.done();
|
||||||
});
|
});
|
||||||
|
@ -166,6 +171,32 @@ export function main() {
|
||||||
existingXHRs[0].setStatusCode(statusCode);
|
existingXHRs[0].setStatusCode(statusCode);
|
||||||
existingXHRs[0].dispatchEvent('load');
|
existingXHRs[0].dispatchEvent('load');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
it('should normalize responseText and response', inject([AsyncTestCompleter], async => {
|
||||||
|
var responseBody = 'Doge';
|
||||||
|
|
||||||
|
var connection1 =
|
||||||
|
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||||
|
|
||||||
|
var connection2 =
|
||||||
|
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
|
||||||
|
|
||||||
|
ObservableWrapper.subscribe<Response>(connection1.response, res => {
|
||||||
|
expect(res.text()).toBe(responseBody);
|
||||||
|
|
||||||
|
ObservableWrapper.subscribe<Response>(connection2.response, ress => {
|
||||||
|
expect(ress.text()).toBe(responseBody);
|
||||||
|
async.done();
|
||||||
|
});
|
||||||
|
existingXHRs[1].dispatchEvent('load');
|
||||||
|
});
|
||||||
|
|
||||||
|
existingXHRs[0].setResponseText(responseBody);
|
||||||
|
existingXHRs[1].setResponse(responseBody);
|
||||||
|
|
||||||
|
existingXHRs[0].dispatchEvent('load');
|
||||||
|
}));
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue