응답 인터셉터를 테스트하려고하는데 $ 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);
};
});
그리고 내 사양입니다 : Expected 'http://localhost:8080/context.html' to equal 'http://accounts.domain.com/oauth/.....'
:
'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();
}));
});
});
});
});
내가있어 여기 내 인터셉터 코드입니다. 제대로 $ 윈도우 객체를 조롱하는 방법에 대한 도움말이나 일반적으로 401 + 리디렉션 사례를 테스트하는 방법에 대한 도움이 필요하십니까?
답변 해 주셔서 감사합니다. 그것은 내가 모르는 것들을 이해하는데 많은 도움이되었습니다. 그러나 'http : // localhost : 8080/context.html'이 'http : //dev-accounts.domain.dev : 3000/oauth/authorize ...'가 될 때까지 동일한 오류가 발생합니다. 이것은'$ window.location.href'와 관련이 있다고 생각합니다. 어떻게 든 항상 http : // localhost : 8080/context.html을 반환합니다. 어떤 아이디어? – lkartono
테스트를 실행중인 브라우저는 무엇입니까? – user2943490
PhantomJs를 사용하고 있습니다. – lkartono