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