title: Angular Jasmine test response interceptor
body: <p>I'm trying to test my response interceptor but I have a hard time figuring out how to mock the $window object. Here is my interceptor code : </p>
  
  <pre><code>'use strict';
  
  angular.module('Domain.handlers')
  
  .config(function($httpProvider) {
    $httpProvider.responseInterceptors.push('UnauthorizedInterceptor');
  })
  
  .factory('UnauthorizedInterceptor', function($q, $injector, $window, ENV) {
    return function(promise) {
      var success = function(response) { return response; };
      var error   = function(response) {
        if (response.status === 401) {
          $window.location.href = ENV.account + '/oauth/authorize?client_id=' + ENV.clientId + '&amp;redirect_uri=' + ENV.app + '/oauth/callback&amp;response_type=token';
        }
        return $q.reject(response);
      };
      return promise.then(success, error);
    };
  });
  </code></pre>
  
  <p>And here is my spec : </p>
  
  <pre><code>'use strict';
  
  describe('Domain.handlers.response', function() {
    var UnauthorizedInterceptor,
        httpProvider,
        $httpBackend,
        $http,
        token = '123456789';
  
    beforeEach(module('Domain.handlers', function($httpProvider) {
      httpProvider = $httpProvider;
    }));
  
    beforeEach(inject(function(_UnauthorizedInterceptor_, _$httpBackend_, _$http_) {
      UnauthorizedInterceptor = _UnauthorizedInterceptor_;
      $httpBackend = _$httpBackend_;
      $http = _$http_;
    }));
  
    describe('UnauthorizedInterceptor', function() {
      it('should be defined', function() {
        expect(UnauthorizedInterceptor).toBeDefined();
      });
  
      describe('HTTP status', function() {
        describe('is 200 OK', function() {
          it('should return a 200 status', function() {
            $httpBackend.expectGET('http://api.domain.com/clients').respond(200, {});
            $http.get('http://api.domain.com/clients');
            $httpBackend.flush();
          });
        });
  
        describe('is 401 Unauthorized', function() {
          it('should redirect to accounts.domain.com', inject(function($window) {
            $httpBackend.expectGET('http://api.domain.com/clients').respond(401, {});
            $http.get('http://api.domain.com/clients');
            expect($window.location.href).toEqual('http://accounts.domain.com/oauth/.....');
            $httpBackend.flush();
          }));
        });
      });
    });
  });
  </code></pre>
  
  <p>I've got a : <code>Expected 'http://localhost:8080/context.html' to equal 'http://accounts.domain.com/oauth/.....'</code>. Any help on how to mock properly the $window object or more generally how to test a 401 + redirection case?</p>