使用$ httpBackend,jasmine测试$ http请求中特定标头的角度项目服务

Vam*_*ram 5 javascript angularjs

我想检查请求中是否存在特定标头.这篇文章帮助我找到了标题.

http://jbavari.github.io/blog/2014/06/20/testing-interceptor-headers-in-angularjs/

以下是我测试的片段.这让我可以访问标题,但问题是(将会),当我刷新请求时,它期望标题作为一个函数,这隐式地使我的测试失败.

$httpBackend
    .expect('POST', 'https://www.someurl.com/login',
      userObj, function (headers) {
        expect(headers['content-type']).toBe('application/x-www-form-urlencoded');
      })
    .respond();
Run Code Online (Sandbox Code Playgroud)

有什么建议 ?

Vam*_*ram 6

一些链接帮助了我.

如果设置了特定标头,则测试AngularJs的$ http.defaults.headers.common

http://andrewfong.com/blog/2014/09/22/angularjs-testing-headers-with-whenget-expectget/

$httpBackend
    .expect('POST', 'https://www.someurl.com/login',
      userObj, function (headers) {
        expect(headers['content-type']).toBe('application/x-www-form-urlencoded');

        // MUST return boolean
        return headers['content-type'] ===  'application/x-www-form-urlencoded';
      })
    .respond();
Run Code Online (Sandbox Code Playgroud)