(method, url, data, callback, headers, timeout, withCredentials)
| 1329 | |
| 1330 | // TODO(vojta): change params to: method, url, data, headers, callback |
| 1331 | function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) { |
| 1332 | var xhr = new MockXhr(), |
| 1333 | expectation = expectations[0], |
| 1334 | wasExpected = false; |
| 1335 | |
| 1336 | function prettyPrint(data) { |
| 1337 | return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp) |
| 1338 | ? data |
| 1339 | : angular.toJson(data); |
| 1340 | } |
| 1341 | |
| 1342 | function wrapResponse(wrapped) { |
| 1343 | if (!$browser && timeout) { |
| 1344 | timeout.then ? timeout.then(handleTimeout) : $timeout(handleTimeout, timeout); |
| 1345 | } |
| 1346 | |
| 1347 | return handleResponse; |
| 1348 | |
| 1349 | function handleResponse() { |
| 1350 | var response = wrapped.response(method, url, data, headers, wrapped.params(url)); |
| 1351 | xhr.$$respHeaders = response[2]; |
| 1352 | callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(), |
| 1353 | copy(response[3] || '')); |
| 1354 | } |
| 1355 | |
| 1356 | function handleTimeout() { |
| 1357 | for (var i = 0, ii = responses.length; i < ii; i++) { |
| 1358 | if (responses[i] === handleResponse) { |
| 1359 | responses.splice(i, 1); |
| 1360 | callback(-1, undefined, ''); |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | if (expectation && expectation.match(method, url)) { |
| 1368 | if (!expectation.matchData(data)) { |
| 1369 | throw new Error('Expected ' + expectation + ' with different data\n' + |
| 1370 | 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data); |
| 1371 | } |
| 1372 | |
| 1373 | if (!expectation.matchHeaders(headers)) { |
| 1374 | throw new Error('Expected ' + expectation + ' with different headers\n' + |
| 1375 | 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' + |
| 1376 | prettyPrint(headers)); |
| 1377 | } |
| 1378 | |
| 1379 | expectations.shift(); |
| 1380 | |
| 1381 | if (expectation.response) { |
| 1382 | responses.push(wrapResponse(expectation)); |
| 1383 | return; |
| 1384 | } |
| 1385 | wasExpected = true; |
| 1386 | } |
| 1387 | |
| 1388 | var i = -1, definition; |
no test coverage detected