AngularJS $http mock respond(...): 如何在响应中模拟 `location` header?
AngularJS $http mock respond(...): how to mock a `location` header in the response?
我有一个 API 响应没有数据的状态 202,但响应有一个指向 URL 的 header "Location"。
我查看了 $httpBackend respond(...) 文档,没有提到如何在响应中模拟 header。
我猜测可能是这样的:
var expectedUrl = 'http://...';
var responseConfig = {
headers: {
location: 'http://...'
}
};
$httpBackend.when(expectedUrl).respond(202, '', responseConfig);
在我的单元测试中,我得到预期状态 202,但 headers('location') 返回未定义。
建议?
呃,没关系,找到了...
$httpBackend.when(expectedUrl).respond(202, '', responseConfig.headers);
第三个参数应为 headers
而不是 config
。
我有一个 API 响应没有数据的状态 202,但响应有一个指向 URL 的 header "Location"。
我查看了 $httpBackend respond(...) 文档,没有提到如何在响应中模拟 header。
我猜测可能是这样的:
var expectedUrl = 'http://...';
var responseConfig = {
headers: {
location: 'http://...'
}
};
$httpBackend.when(expectedUrl).respond(202, '', responseConfig);
在我的单元测试中,我得到预期状态 202,但 headers('location') 返回未定义。
建议?
呃,没关系,找到了...
$httpBackend.when(expectedUrl).respond(202, '', responseConfig.headers);
第三个参数应为 headers
而不是 config
。