MCPcopy Create free account
hub / github.com/angular-ui/ui-router / $httpBackend

Function $httpBackend

test/angular/1.3/angular-mocks.js:1194–1265  ·  view source on GitHub ↗
(method, url, data, callback, headers, timeout, withCredentials)

Source from the content-addressed store, hash-verified

1192
1193 // TODO(vojta): change params to: method, url, data, headers, callback
1194 function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) {
1195 var xhr = new MockXhr(),
1196 expectation = expectations[0],
1197 wasExpected = false;
1198
1199 function prettyPrint(data) {
1200 return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
1201 ? data
1202 : angular.toJson(data);
1203 }
1204
1205 function wrapResponse(wrapped) {
1206 if (!$browser && timeout) {
1207 timeout.then ? timeout.then(handleTimeout) : $timeout(handleTimeout, timeout);
1208 }
1209
1210 return handleResponse;
1211
1212 function handleResponse() {
1213 var response = wrapped.response(method, url, data, headers);
1214 xhr.$$respHeaders = response[2];
1215 callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
1216 copy(response[3] || ''));
1217 }
1218
1219 function handleTimeout() {
1220 for (var i = 0, ii = responses.length; i < ii; i++) {
1221 if (responses[i] === handleResponse) {
1222 responses.splice(i, 1);
1223 callback(-1, undefined, '');
1224 break;
1225 }
1226 }
1227 }
1228 }
1229
1230 if (expectation && expectation.match(method, url)) {
1231 if (!expectation.matchData(data))
1232 throw new Error('Expected ' + expectation + ' with different data\n' +
1233 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
1234
1235 if (!expectation.matchHeaders(headers))
1236 throw new Error('Expected ' + expectation + ' with different headers\n' +
1237 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
1238 prettyPrint(headers));
1239
1240 expectations.shift();
1241
1242 if (expectation.response) {
1243 responses.push(wrapResponse(expectation));
1244 return;
1245 }
1246 wasExpected = true;
1247 }
1248
1249 var i = -1, definition;
1250 while ((definition = definitions[++i])) {
1251 if (definition.match(method, url, data, headers || {})) {

Callers 1

sendReqFunction · 0.70

Calls 3

matchMethod · 0.80
prettyPrintFunction · 0.70
wrapResponseFunction · 0.70

Tested by

no test coverage detected