(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers)
| 1368 | |
| 1369 | // TODO(vojta): change params to: method, url, data, headers, callback |
| 1370 | function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) { |
| 1371 | |
| 1372 | var xhr = new MockXhr(), |
| 1373 | expectation = expectations[0], |
| 1374 | wasExpected = false; |
| 1375 | |
| 1376 | xhr.$$events = eventHandlers; |
| 1377 | xhr.upload.$$events = uploadEventHandlers; |
| 1378 | |
| 1379 | function prettyPrint(data) { |
| 1380 | return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp) |
| 1381 | ? data |
| 1382 | : angular.toJson(data); |
| 1383 | } |
| 1384 | |
| 1385 | function wrapResponse(wrapped) { |
| 1386 | if (!$browser && timeout) { |
| 1387 | if (timeout.then) { |
| 1388 | timeout.then(function() { |
| 1389 | handlePrematureEnd(angular.isDefined(timeout.$$timeoutId) ? 'timeout' : 'abort'); |
| 1390 | }); |
| 1391 | } else { |
| 1392 | $timeout(function() { |
| 1393 | handlePrematureEnd('timeout'); |
| 1394 | }, timeout); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | handleResponse.description = method + ' ' + url; |
| 1399 | return handleResponse; |
| 1400 | |
| 1401 | function handleResponse() { |
| 1402 | var response = wrapped.response(method, url, data, headers, wrapped.params(url)); |
| 1403 | xhr.$$respHeaders = response[2]; |
| 1404 | callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(), |
| 1405 | copy(response[3] || ''), copy(response[4])); |
| 1406 | } |
| 1407 | |
| 1408 | function handlePrematureEnd(reason) { |
| 1409 | for (var i = 0, ii = responses.length; i < ii; i++) { |
| 1410 | if (responses[i] === handleResponse) { |
| 1411 | responses.splice(i, 1); |
| 1412 | callback(-1, undefined, '', undefined, reason); |
| 1413 | break; |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | if (expectation && expectation.match(method, url)) { |
| 1420 | if (!expectation.matchData(data)) { |
| 1421 | throw new Error('Expected ' + expectation + ' with different data\n' + |
| 1422 | 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data); |
| 1423 | } |
| 1424 | |
| 1425 | if (!expectation.matchHeaders(headers)) { |
| 1426 | throw new Error('Expected ' + expectation + ' with different headers\n' + |
| 1427 | 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' + |
no test coverage detected