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

Function createHttpBackendMock

test/angular/1.5/angular-mocks.js:1322–1897  ·  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

1320 * @return {Object} Instance of $httpBackend mock
1321 */
1322function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
1323 var definitions = [],
1324 expectations = [],
1325 responses = [],
1326 responsesPush = angular.bind(responses, responses.push),
1327 copy = angular.copy;
1328
1329 function createResponse(status, data, headers, statusText) {
1330 if (angular.isFunction(status)) return status;
1331
1332 return function() {
1333 return angular.isNumber(status)
1334 ? [status, data, headers, statusText]
1335 : [200, status, data, headers];
1336 };
1337 }
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 }

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected