Skip to content

Commit cfedc77

Browse files
PatrickJSmhevery
authored andcommitted
test(XHRConnection): normalize responseText and response
normalize xhr.responseText and xhr.response - [x] Tests Closes angular#2882
1 parent 96eefdf commit cfedc77

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

modules/angular2/src/render/xhr_impl.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Injectable} from 'angular2/di';
22
import {Promise, PromiseWrapper, PromiseCompleter} from 'angular2/src/facade/async';
3+
import {isPresent} from 'angular2/src/facade/lang';
34
import {XHR} from './xhr';
45

56
@Injectable()
@@ -13,7 +14,7 @@ export class XHRImpl extends XHR {
1314
xhr.onload = function() {
1415
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
1516
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
16-
var response = ('response' in xhr) ? xhr.response : xhr.responseText;
17+
var response = isPresent(xhr.response) ? xhr.response : xhr.responseText;
1718

1819
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
1920
var status = xhr.status === 1223 ? 204 : xhr.status;

modules/angular2/test/http/backends/xhr_backend_spec.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class MockBrowserXHR extends BrowserXhr {
5151
}
5252

5353
setStatusCode(status) { this.status = status; }
54+
55+
setResponse(value) { this.response = value; }
56+
57+
setResponseText(value) { this.responseText = value; }
58+
5459
addEventListener(type: string, cb: Function) { this.callbacks.set(type, cb); }
5560

5661
dispatchEvent(type: string) { this.callbacks.get(type)({}); }
@@ -143,7 +148,7 @@ export function main() {
143148
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
144149
new ResponseOptions({status: statusCode}));
145150

146-
ObservableWrapper.subscribe(connection.response, res => {
151+
ObservableWrapper.subscribe<Response>(connection.response, res => {
147152
expect(res.status).toBe(statusCode);
148153
async.done();
149154
});
@@ -158,14 +163,40 @@ export function main() {
158163
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
159164
new ResponseOptions({status: statusCode}));
160165

161-
ObservableWrapper.subscribe(connection.response, res => {
166+
ObservableWrapper.subscribe<Response>(connection.response, res => {
162167
expect(res.status).toBe(normalizedCode);
163168
async.done();
164169
});
165170

166171
existingXHRs[0].setStatusCode(statusCode);
167172
existingXHRs[0].dispatchEvent('load');
168173
}));
174+
175+
it('should normalize responseText and response', inject([AsyncTestCompleter], async => {
176+
var responseBody = 'Doge';
177+
178+
var connection1 =
179+
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
180+
181+
var connection2 =
182+
new XHRConnection(sampleRequest, new MockBrowserXHR(), new ResponseOptions());
183+
184+
ObservableWrapper.subscribe<Response>(connection1.response, res => {
185+
expect(res.text()).toBe(responseBody);
186+
187+
ObservableWrapper.subscribe<Response>(connection2.response, ress => {
188+
expect(ress.text()).toBe(responseBody);
189+
async.done();
190+
});
191+
existingXHRs[1].dispatchEvent('load');
192+
});
193+
194+
existingXHRs[0].setResponseText(responseBody);
195+
existingXHRs[1].setResponse(responseBody);
196+
197+
existingXHRs[0].dispatchEvent('load');
198+
}));
199+
169200
});
170201
});
171202
}

0 commit comments

Comments
 (0)