噶单元测试跨域资源AngularJS [英] Karma unit test cross domain resource AngularJS
问题描述
我用因果报应我的角度项目的测试运行框架,我的角有服务器服务需要访问其他网页URL获得像 HTTP数据://本地主机:8081 /普通/国家
获得有关国家的所有信息。
我的问题是在我的本地主机人缘启动:9876
,它需要从 HTTP GET数据://本地主机:8081 /普通/国家
通过浏览同源策略这项事业跨域问题。
所以我得到下面的错误在我的控制台:
I use karma as my angular project test running framework, my angular have server service need to access another web url to get data like http://localhost:8081/common/countries
get all information about country.
my problem is my karma start at localhost:9876
and it need to get data from http://localhost:8081/common/countries
this cause cross domain problem by the browse same-origin policy.
so I get below error in my console:
Error: Unexpected request: GET http://localhost:8081/common/countries
No more request expected
at Error (<anonymous>)
at $httpBackend (http://localhost:9876/absoluteC:/WebUI/WebUI/test/lib/angular/angular-mocks.js:934:9)
at sendReq (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:9087:9)
at $http (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:8878:17)
at Object.getMock (http://localhost:9876/base/share/services.js:644:17)
at Object.get (http://localhost:9876/base/share/services.js:347:28)
at Object.getCountries (http://localhost:9876/base/share/services.js:221:22)
at Object.clSingleSelectConfig.nationality.getData (http://localhost:9876/base/share/directives.js:146:32)
at http://localhost:9876/base/share/directives.js:192:44
at nodeLinkFn (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:4360:13)
我曾尝试:
1安装插件因缘果报铬发射,并添加 - 禁用 - 网络安全
在我的配置文件。但它不工作。
2 set'Access控制允许-Origin''Access控制允许-Headers''Access控制允许的方法在头',允许在服务器响应起源访问。
What i have tried:
1 install karma plugin karma-chrome-launcher and add --disable-web-security
in my config file. but it doesn't work.
2 set'Access-Control-Allow-Origin''Access-Control-Allow-Headers''Access-Control-Allow-Methods' in header to allow origin access in server response.
以上所有没有工作,所以如何解决我的问题?
all above don't work, so how to solve my problem?
推荐答案
有关跨域请求使用的 expectJSONP 并且一定要使用回调参数。
For cross domain requests use expectJSONP and be sure to use a callback parameter.
describe('stackoverflow.activity tests', function () {
var svc, httpBackend;
beforeEach(function (){
module('ngResource');
module('stackoverflow.activity');
inject(function($httpBackend, StackoverflowActivityService) {
svc = StackoverflowActivityService;
httpBackend = $httpBackend;
});
});
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should send the message and return the response', function (){
var returnData = { testing: 'anything'};
httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK').respond(returnData);
svc.events({
user:'gigablox',
params:{
callback:'JSON_CALLBACK'
}
}).get(function(user) {
expect(user.testing).toEqual('anything');
});
httpBackend.flush();
});
});
<一个href=\"https://github.com/gigablox/angular-stackoverflow-activity/blob/master/src/test/stackoverflow-activity.test.js\"相对=nofollow> 源代码在这个例子中
这篇关于噶单元测试跨域资源AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!