1

I'd like to delay the response to the following whenGET:

$httpBackend.whenGET(/^foobar/).respond(function () {
  return [200,{}];
});

However it seems impossible using $timeout to do this synchronously, so I'm not sure how to approach this?

Keir
  • 440
  • 6
  • 15
  • why do you want to delay the response? what is your end goal? – dnozay Sep 28 '14 at 17:30
  • Well I'm wanting to simulate network delays without actually hitting the network. I have a progress bar I need to verify works, if my series of requests is completed instantaneously I can't check that. – Keir Sep 28 '14 at 18:19
  • if you need to test the progress bars, why do you even need to have requests in the picture? can you not isolate the progress bar and make sure it displays properly with the different states? e.g. value changes -> progress bar changes. – dnozay Sep 28 '14 at 21:59
  • This is more for integration/e2e testing, I want to test the flow right through the app. – Keir Sep 29 '14 at 13:07
  • duplicate from http://stackoverflow.com/questions/25368691/delaying-a-response-with-httpbackend ? – dnozay Sep 29 '14 at 17:05

1 Answers1

0

If you want to delay only a specific response, than you may delay assignment of the reponse to scope property.

If you wrap your call into your custom service method, than you may wrap the response into the promise and resolve it when needed:

JS:

angular.module('app').service('myService', function($q, $timeout){

 this.getData = function() {
    var delay = 300,
        origPromise = $http.get('someUrl'),
        deferred = $q.defer();
    $timeout(function() {
       deferred.resolve(origPromise);
    }, delay);

    return defered.promise;
 };

});

[EDIT]:

To set the delay to all requests, you may apply this solution to the response interceptor

Alexey
  • 1,257
  • 7
  • 13
  • Here http://endlessindirection.wordpress.com/2013/05/18/angularjs-delay-response-from-httpbackend/ is an explanation how you may configure the delay for all requests. – Alexey Sep 28 '14 at 16:45