MCPcopy
hub / github.com/angular-ui/ui-router / createHttpBackendMock

Function createHttpBackendMock

test/angular/1.7/angular-mocks.js:1497–2147  ·  view source on GitHub ↗

* General factory function for $httpBackend mock. * Returns instance for unit testing (when no arguments specified): * - passing through is disabled * - auto flushing is disabled * * Returns instance for e2e testing (when `$delegate` and `$browser` specified): * - passing through (delega

($rootScope, $timeout, $delegate, $browser)

Source from the content-addressed store, hash-verified

1495 * @return {Object} Instance of $httpBackend mock
1496 */
1497function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1498 var definitions = [],
1499 expectations = [],
1500 matchLatestDefinition = false,
1501 responses = [],
1502 responsesPush = angular.bind(responses, responses.push),
1503 copy = angular.copy,
1504 // We cache the original backend so that if both ngMock and ngMockE2E override the
1505 // service the ngMockE2E version can pass through to the real backend
1506 originalHttpBackend = $delegate.$$originalHttpBackend || $delegate;
1507
1508 function createResponse(status, data, headers, statusText) {
1509 if (angular.isFunction(status)) return status;
1510
1511 return function() {
1512 return angular.isNumber(status)
1513 ? [status, data, headers, statusText, 'complete']
1514 : [200, status, data, headers, 'complete'];
1515 };
1516 }
1517
1518 // TODO(vojta): change params to: method, url, data, headers, callback
1519 function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) {
1520
1521 var xhr = new MockXhr(),
1522 expectation = expectations[0],
1523 wasExpected = false;
1524
1525 xhr.$$events = eventHandlers;
1526 xhr.upload.$$events = uploadEventHandlers;
1527
1528 function prettyPrint(data) {
1529 return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
1530 ? data
1531 : angular.toJson(data);
1532 }
1533
1534 function wrapResponse(wrapped) {
1535 if (!$browser && timeout) {
1536 if (timeout.then) {
1537 timeout.then(function() {
1538 handlePrematureEnd(angular.isDefined(timeout.$$timeoutId) ? 'timeout' : 'abort');
1539 });
1540 } else {
1541 $timeout(function() {
1542 handlePrematureEnd('timeout');
1543 }, timeout);
1544 }
1545 }
1546
1547 handleResponse.description = method + ' ' + url;
1548 return handleResponse;
1549
1550 function handleResponse() {
1551 var response = wrapped.response(method, url, data, headers, wrapped.params(url));
1552 xhr.$$respHeaders = response[2];
1553 callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
1554 copy(response[3] || ''), copy(response[4]));

Callers

nothing calls this directly

Calls 5

parseRouteUrlFunction · 0.85
whenMethod · 0.80
assertArgDefinedFunction · 0.70
createResponseFunction · 0.70
createShortMethodsFunction · 0.70

Tested by

no test coverage detected