当前位置: 首页 > 面试题库 >

角茉莉测试响应拦截器

皮安顺
2023-03-14
问题内容

我正在尝试测试响应拦截器,但是很难弄清楚如何模拟$ window对象。这是我的拦截器代码:

'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 + '&redirect_uri=' + ENV.app + '/oauth/callback&response_type=token';
      }
      return $q.reject(response);
    };
    return promise.then(success, error);
  };
});

这是我的规格:

'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();
        }));
      });
    });
  });
});

我有一个:Expected 'http://localhost:8080/context.html' to equal 'http://accounts.domain.com/oauth/.....'。关于如何正确模拟$ window对象或更一般地说如何测试401
+重定向情况的任何帮助?


问题答案:

您应该使用最新的语法来构造拦截器定义。您的URL构造也应包含在服务中,以便可以在测试中轻松模拟它。

.factory('UnauthorizedInterceptor', function($q, $window, OtherService) {
  var service = {
    responseError: handleUnauthorized
  };

  return service;

  function handleUnauthorized(rejection) {
    if (rejection.status === 401) {
      $window.location.href = OtherService.getUnauthorizedRedirectURL();
    }
    return $q.reject(rejection);
  }
});

这样做可以让您像其他任何工厂一样对其进行测试,而不必担心$http拦截器的内部实现,也不必使用来模拟响应$httpBackend

describe('Domain.handlers.response', function() {
  var $window,
      UnauthorizedInterceptor,
      OtherService,
      redirectUrl = 'someUrl';

  beforeEach(module('Domain.handlers'));

  beforeEach(function () {
    $window = { location: { href: null } };

    module(function($provide) {
      $provide.value('$window', $window);
    });
  });

  beforeEach(inject(function(_UnauthorizedInterceptor_, _OtherService_) {
    UnauthorizedInterceptor = _UnauthorizedInterceptor_;
    OtherService = _OtherService_;

    spyOn(OtherService, 'getUnauthorizedRedirectURL').andReturn(redirectUrl);
  }));

  describe('UnauthorizedInterceptor', function() {
    it('should be defined', function() {
      expect(UnauthorizedInterceptor).toBeDefined();
    });

    it('should have a handler for responseError', function () {
      expect(angular.isFunction(UnauthorizedInterceptor.responseError)).toBe(true);
    });

    describe('when HTTP 401', function () {
      beforeEach(function () {
        var rejection = { status: 401 };
        UnauthorizedInterceptor.responseError(rejection);
      });

      it('should set window location', function () {
        expect($window.location.href).toBe(redirectUrl);
      });
    });

    describe('when not HTTP 401', function () {
      beforeEach(function () {
        var rejection = { status: 500 };
        UnauthorizedInterceptor.responseError(rejection);
      });

      it('should not set window location', function () {
        expect($window.location.href).not.toBe(redirectUrl);
      });
    });
  });
});


 类似资料:
  • 我无法在控制台记录时在拦截器中获取自定义响应头。控制台记录了拦截器httpResponse "控制台日志响应" HTTP响应{标头: HttpHeaders,状态: 200, statusText:"OK", ok: true,...} HttpHeaders lazyInit:ƒ()lazyUpdate:null normalizedNames:Map(0){}proto:Object ok:t

  • 问题内容: 如何在angularjs量角器茉莉花测试中查看console.log输出?截至目前,浏览器自身关闭速度过快。 更多信息-我正在使用angularjs教程,步骤8。我试图将e2e测试更改为量角器。我正在使用的量角器配置文件基于%appdata%\ npm \ node_modules \ protractor \ referenceConf.js。在配置文件引用的js规范文件中,我有co

  • 当试图弄清楚如何使某些jasmine期望语句依赖于先前的期望语句时,我发现在Jasmine 2.3.0之前,没有办法。(请参阅第一次期望失败后停止jasmine测试)但是,Jasmine 2.3.0添加了一个选项,当设置为true时将停止对第一次失败的测试。 对这个前景感到兴奋,我修改了我的conf.js以包括以下选项: 但是这对我不起作用。 在我的测试中,我有: 在上面的代码中,将尝试两个期望语

  • 我注意到在Chrome中运行我的量角器E2E测试时,每当一个规范失败,我的记者试图截图时,Chrome就会崩溃,错误日志如下。没有失败的测试工作正常。有故障的IE和FF工作正常。 正在使用的工具: Protractor版本:4.0.9 protractor-jasmine2-screenshot-reporter:0.3.2 NPM版本:3.10.9 Node版本:4.4.3 Chrome版本:5

  • 问题内容: 当我尝试使用 $ httpBackend.flush();时 我收到错误 TypeError:$ browser.cookies不是一个函数。 我找不到有关这种错误和解决方案的任何信息。 角度:1.3.15 茉莉花:2.3.4 问题答案: 我相信您在版本中使用的是角度模拟,而您的代码使用的是angular 。请检查您是否在为应用程序中实现的版本使用模拟程序。同样,提供您的茉莉花测试配置

  • 问题内容: 我正在尝试编写业力/茉莉花测试,我想对模拟如何在正在返回诺言的服务上工作进行一些解释。我解释一下我的情况: 我有一个控制器,在其中执行以下调用: 这是我的服务方式: 最后,这是我的单元测试: 我真正想做的是将响应对象({“ elements:…})作为fillMapDatas函数的datas参数。我不了解如何模拟所有服务内容(服务,承诺,然后) 问题答案: 因此,如果您的服务是否按预期