mercredi 3 juin 2015

AngularJs Interceptor failed to intercept request for HttpBackend in unit test

I have set up the following interceptor in my app module

   var debugHttpInterceptor = {
        'request': function (config) {
            var canceler = $q.defer();
            config.timeout = canceler.
            if (config.url === "dontgo.com/photo") {
                // Swallow request
                canceler.resolve();
                break;
            }
            return config || $q.when(config);
        }
    };
    return debugHttpInterceptor;

In my unit test, I get the following error:

Error: Unexpected request: GET http://ift.tt/1EWbANb

The request was made in the constructor of one of my AngularJS controllers. The error goes away if I add this to my unit test beforeEach()

$httpBackend.whenGET('dontgo.com/photo').respond(200, 'http://ift.tt/1dM82It');

After setting a few break points in the interceptor and the controller, I was able to confirm that

  1. The interceptor is configured before the request was made
  2. The interceptor does see the request and set the timeout

So I was thinking the $httpBackend.whenGET() must be fired before the interceptor sees the request? If this is case, Is there a way to "completely" intercept a request so even $httpBackend doesn't see it?

Aucun commentaire:

Enregistrer un commentaire