【问题标题】:AngularJS $http mock respond(...): how to mock a `location` header in the response?AngularJS $http 模拟响应(...):如何在响应中模拟“位置”标头?
【发布时间】:2015-05-03 11:39:25
【问题描述】:

我有一个 API 响应没有数据的状态 202,但响应有一个指向 URL 的标头“位置”。

我查看了 $httpBackend respond(...) 文档,并没有看到如何在响应中模拟标头。

我猜测可能是这样的:

var expectedUrl = 'http://...';
var responseConfig = {
    headers: {
        location: 'http://...'
    }
};
$httpBackend.when(expectedUrl).respond(202, '', responseConfig);

在我的单元测试中,我得到了预期的状态 202,但 headers('location') 返回未定义。

建议?

【问题讨论】:

    标签: angularjs unit-testing mocking httpbackend


    【解决方案1】:

    呃,没关系,找到了……

    $httpBackend.when(expectedUrl).respond(202, '', responseConfig.headers);

    第三个参数应为headers 而不是config

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 2018-08-11
      • 2020-02-04
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多