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

Function createHttpBackendMock

test/angular/1.6/angular-mocks.js:1349–1959  ·  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

1347 * @return {Object} Instance of $httpBackend mock
1348 */
1349function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1350 var definitions = [],
1351 expectations = [],
1352 responses = [],
1353 responsesPush = angular.bind(responses, responses.push),
1354 copy = angular.copy,
1355 // We cache the original backend so that if both ngMock and ngMockE2E override the
1356 // service the ngMockE2E version can pass through to the real backend
1357 originalHttpBackend = $delegate.$$originalHttpBackend || $delegate;
1358
1359 function createResponse(status, data, headers, statusText) {
1360 if (angular.isFunction(status)) return status;
1361
1362 return function() {
1363 return angular.isNumber(status)
1364 ? [status, data, headers, statusText, 'complete']
1365 : [200, status, data, headers, 'complete'];
1366 };
1367 }
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 }

Callers

nothing calls this directly

Calls 5

whenMethod · 0.80
assertArgDefinedFunction · 0.70
createResponseFunction · 0.70
createShortMethodsFunction · 0.70
parseRouteFunction · 0.70

Tested by

no test coverage detected