Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 32c09c1

Browse files
jimlyndoncaitp
authored andcommitted
feat($http): add xhr statusText to completeRequest callback
Makes xhr status text accessible is $http success/error callback. See www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-statustext Closes #2335 Closes #2665 Closes #6713
1 parent 2606437 commit 32c09c1

File tree

6 files changed

+100
-42
lines changed

6 files changed

+100
-42
lines changed

src/ng/http.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ function $HttpProvider() {
571571
* - **status** – `{number}` – HTTP status code of the response.
572572
* - **headers** – `{function([headerName])}` – Header getter function.
573573
* - **config** – `{Object}` – The configuration object that was used to generate the request.
574+
* - **statusText** – `{string}` – HTTP status text of the response.
574575
*
575576
* @property {Array.<Object>} pendingRequests Array of config objects for currently pending
576577
* requests. This is primarily meant to be used for debugging purposes.
@@ -945,9 +946,9 @@ function $HttpProvider() {
945946
} else {
946947
// serving from cache
947948
if (isArray(cachedResp)) {
948-
resolvePromise(cachedResp[1], cachedResp[0], copy(cachedResp[2]));
949+
resolvePromise(cachedResp[1], cachedResp[0], copy(cachedResp[2]), cachedResp[3]);
949950
} else {
950-
resolvePromise(cachedResp, 200, {});
951+
resolvePromise(cachedResp, 200, {}, 'OK');
951952
}
952953
}
953954
} else {
@@ -971,33 +972,34 @@ function $HttpProvider() {
971972
* - resolves the raw $http promise
972973
* - calls $apply
973974
*/
974-
function done(status, response, headersString) {
975+
function done(status, response, headersString, statusText) {
975976
if (cache) {
976977
if (isSuccess(status)) {
977-
cache.put(url, [status, response, parseHeaders(headersString)]);
978+
cache.put(url, [status, response, parseHeaders(headersString), statusText]);
978979
} else {
979980
// remove promise from the cache
980981
cache.remove(url);
981982
}
982983
}
983984

984-
resolvePromise(response, status, headersString);
985+
resolvePromise(response, status, headersString, statusText);
985986
if (!$rootScope.$$phase) $rootScope.$apply();
986987
}
987988

988989

989990
/**
990991
* Resolves the raw $http promise.
991992
*/
992-
function resolvePromise(response, status, headers) {
993+
function resolvePromise(response, status, headers, statusText) {
993994
// normalize internal statuses to 0
994995
status = Math.max(status, 0);
995996

996997
(isSuccess(status) ? deferred.resolve : deferred.reject)({
997998
data: response,
998999
status: status,
9991000
headers: headersGetter(headers),
1000-
config: config
1001+
config: config,
1002+
statusText : statusText
10011003
});
10021004
}
10031005

src/ng/httpBackend.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
9797
completeRequest(callback,
9898
status || xhr.status,
9999
response,
100-
responseHeaders);
100+
responseHeaders,
101+
xhr.statusText || '');
101102
}
102103
};
103104

@@ -138,7 +139,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
138139
xhr && xhr.abort();
139140
}
140141

141-
function completeRequest(callback, status, response, headersString) {
142+
function completeRequest(callback, status, response, headersString, statusText) {
142143
// cancel timeout and subsequent timeout promise resolution
143144
timeoutId && $browserDefer.cancel(timeoutId);
144145
jsonpDone = xhr = null;
@@ -151,9 +152,10 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
151152
}
152153

153154
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
154-
status = status == 1223 ? 204 : status;
155+
status = status === 1223 ? 204 : status;
156+
statusText = statusText || '';
155157

156-
callback(status, response, headersString);
158+
callback(status, response, headersString, statusText);
157159
$browser.$$completeOutstandingRequest(noop);
158160
}
159161
};

src/ngMock/angular-mocks.js

+25-21
Original file line numberDiff line numberDiff line change
@@ -1090,12 +1090,12 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
10901090
responsesPush = angular.bind(responses, responses.push),
10911091
copy = angular.copy;
10921092

1093-
function createResponse(status, data, headers) {
1093+
function createResponse(status, data, headers, statusText) {
10941094
if (angular.isFunction(status)) return status;
10951095

10961096
return function() {
10971097
return angular.isNumber(status)
1098-
? [status, data, headers]
1098+
? [status, data, headers, statusText]
10991099
: [200, status, data];
11001100
};
11011101
}
@@ -1120,7 +1120,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11201120
function handleResponse() {
11211121
var response = wrapped.response(method, url, data, headers);
11221122
xhr.$$respHeaders = response[2];
1123-
callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders());
1123+
callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
1124+
copy(response[3] || ''));
11241125
}
11251126

11261127
function handleTimeout() {
@@ -1187,16 +1188,17 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11871188
* request is handled.
11881189
*
11891190
* - respond –
1190-
* `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
1191-
* – The respond method takes a set of static data to be returned or a function that can return
1192-
* an array containing response status (number), response data (string) and response headers
1193-
* (Object).
1191+
* `{function([status,] data[, headers, statusText])
1192+
* | function(function(method, url, data, headers)}`
1193+
* – The respond method takes a set of static data to be returned or a function that can
1194+
* return an array containing response status (number), response data (string), response
1195+
* headers (Object), and the text for the status (string).
11941196
*/
11951197
$httpBackend.when = function(method, url, data, headers) {
11961198
var definition = new MockHttpExpectation(method, url, data, headers),
11971199
chain = {
1198-
respond: function(status, data, headers) {
1199-
definition.response = createResponse(status, data, headers);
1200+
respond: function(status, data, headers, statusText) {
1201+
definition.response = createResponse(status, data, headers, statusText);
12001202
}
12011203
};
12021204

@@ -1304,17 +1306,18 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
13041306
* request is handled.
13051307
*
13061308
* - respond –
1307-
* `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
1308-
* – The respond method takes a set of static data to be returned or a function that can return
1309-
* an array containing response status (number), response data (string) and response headers
1310-
* (Object).
1309+
* `{function([status,] data[, headers, statusText])
1310+
* | function(function(method, url, data, headers)}`
1311+
* – The respond method takes a set of static data to be returned or a function that can
1312+
* return an array containing response status (number), response data (string), response
1313+
* headers (Object), and the text for the status (string).
13111314
*/
13121315
$httpBackend.expect = function(method, url, data, headers) {
13131316
var expectation = new MockHttpExpectation(method, url, data, headers);
13141317
expectations.push(expectation);
13151318
return {
1316-
respond: function(status, data, headers) {
1317-
expectation.response = createResponse(status, data, headers);
1319+
respond: function (status, data, headers, statusText) {
1320+
expectation.response = createResponse(status, data, headers, statusText);
13181321
}
13191322
};
13201323
};
@@ -1816,13 +1819,14 @@ angular.module('ngMockE2E', ['ng']).config(['$provide', function($provide) {
18161819
* control how a matched request is handled.
18171820
*
18181821
* - respond –
1819-
* `{function([status,] data[, headers])|function(function(method, url, data, headers)}`
1822+
* `{function([status,] data[, headers, statusText])
1823+
* | function(function(method, url, data, headers)}`
18201824
* – The respond method takes a set of static data to be returned or a function that can return
1821-
* an array containing response status (number), response data (string) and response headers
1822-
* (Object).
1823-
* - passThrough – `{function()}` – Any request matching a backend definition with `passThrough`
1824-
* handler will be passed through to the real backend (an XHR request will be made to the
1825-
* server.)
1825+
* an array containing response status (number), response data (string), response headers
1826+
* (Object), and the text for the status (string).
1827+
* - passThrough – `{function()}` – Any request matching a backend definition with
1828+
* `passThrough` handler will be passed through to the real backend (an XHR request will be made
1829+
* to the server.)
18261830
*/
18271831

18281832
/**

test/ng/httpBackendSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ describe('$httpBackend', function() {
7575
expect(xhr.$$data).toBe(null);
7676
});
7777

78+
it('should call completion function with xhr.statusText if present', function() {
79+
callback.andCallFake(function(status, response, headers, statusText) {
80+
expect(statusText).toBe('OK');
81+
});
82+
83+
$backend('GET', '/some-url', null, callback);
84+
xhr = MockXhr.$$lastInstance;
85+
xhr.statusText = 'OK';
86+
xhr.readyState = 4;
87+
xhr.onreadystatechange();
88+
expect(callback).toHaveBeenCalledOnce();
89+
});
90+
91+
it('should call completion function with empty string if not present', function() {
92+
callback.andCallFake(function(status, response, headers, statusText) {
93+
expect(statusText).toBe('');
94+
});
95+
96+
$backend('GET', '/some-url', null, callback);
97+
xhr = MockXhr.$$lastInstance;
98+
xhr.readyState = 4;
99+
xhr.onreadystatechange();
100+
expect(callback).toHaveBeenCalledOnce();
101+
});
102+
103+
78104
it('should normalize IE\'s 1223 status code into 204', function() {
79105
callback.andCallFake(function(status) {
80106
expect(status).toBe(204);

test/ng/httpSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,30 @@ describe('$http', function() {
481481
});
482482

483483

484+
it('should pass statusText in response object when a request is successful', function() {
485+
$httpBackend.expect('GET', '/url').respond(200, 'SUCCESS', {}, 'OK');
486+
$http({url: '/url', method: 'GET'}).then(function(response) {
487+
expect(response.statusText).toBe('OK');
488+
callback();
489+
});
490+
491+
$httpBackend.flush();
492+
expect(callback).toHaveBeenCalledOnce();
493+
});
494+
495+
496+
it('should pass statusText in response object when a request fails', function() {
497+
$httpBackend.expect('GET', '/url').respond(404, 'ERROR', {}, 'Not Found');
498+
$http({url: '/url', method: 'GET'}).then(null, function(response) {
499+
expect(response.statusText).toBe('Not Found');
500+
callback();
501+
});
502+
503+
$httpBackend.flush();
504+
expect(callback).toHaveBeenCalledOnce();
505+
});
506+
507+
484508
it('should pass in the response object when a request failed', function() {
485509
$httpBackend.expect('GET', '/url').respond(543, 'bad error', {'request-id': '123'});
486510
$http({url: '/url', method: 'GET'}).then(null, function(response) {

test/ngMock/angular-mocksSpec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1068,29 +1068,29 @@ describe('ngMock', function() {
10681068
hb.flush();
10691069

10701070
expect(callback.callCount).toBe(2);
1071-
expect(callback.argsForCall[0]).toEqual([201, 'second', '']);
1072-
expect(callback.argsForCall[1]).toEqual([200, 'first', '']);
1071+
expect(callback.argsForCall[0]).toEqual([201, 'second', '', '']);
1072+
expect(callback.argsForCall[1]).toEqual([200, 'first', '', '']);
10731073
});
10741074

10751075

10761076
describe('respond()', function() {
10771077
it('should take values', function() {
1078-
hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'});
1078+
hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'}, 'OK');
10791079
hb('GET', '/url1', undefined, callback);
10801080
hb.flush();
10811081

1082-
expect(callback).toHaveBeenCalledOnceWith(200, 'first', 'header: val');
1082+
expect(callback).toHaveBeenCalledOnceWith(200, 'first', 'header: val', 'OK');
10831083
});
10841084

10851085
it('should take function', function() {
1086-
hb.expect('GET', '/some').respond(function(m, u, d, h) {
1087-
return [301, m + u + ';' + d + ';a=' + h.a, {'Connection': 'keep-alive'}];
1086+
hb.expect('GET', '/some').respond(function (m, u, d, h) {
1087+
return [301, m + u + ';' + d + ';a=' + h.a, {'Connection': 'keep-alive'}, 'Moved Permanently'];
10881088
});
10891089

10901090
hb('GET', '/some', 'data', callback, {a: 'b'});
10911091
hb.flush();
10921092

1093-
expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Connection: keep-alive');
1093+
expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Connection: keep-alive', 'Moved Permanently');
10941094
});
10951095

10961096
it('should default status code to 200', function() {
@@ -1119,8 +1119,8 @@ describe('ngMock', function() {
11191119
hb.flush();
11201120

11211121
expect(callback.callCount).toBe(2);
1122-
expect(callback.argsForCall[0]).toEqual([200, 'first', '']);
1123-
expect(callback.argsForCall[1]).toEqual([200, 'second', '']);
1122+
expect(callback.argsForCall[0]).toEqual([200, 'first', '', '']);
1123+
expect(callback.argsForCall[1]).toEqual([200, 'second', '', '']);
11241124
});
11251125
});
11261126

@@ -1415,7 +1415,7 @@ describe('ngMock', function() {
14151415
hb[shortcut]('/foo').respond('bar');
14161416
hb(method, '/foo', undefined, callback);
14171417
hb.flush();
1418-
expect(callback).toHaveBeenCalledOnceWith(200, 'bar', '');
1418+
expect(callback).toHaveBeenCalledOnceWith(200, 'bar', '', '');
14191419
});
14201420
});
14211421
});

0 commit comments

Comments
 (0)