Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing angular project service for specific headers in $http requests using $httpBackend, jasmine

I want to check if a specific header is present in the request. This is the article that helped me hack to find the headers.

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

The following is the snippet from my test. This is giving me the access to headers but the problem is (will be), it is expecting the headers as a function when I flush the requests, which fails my test implicitly.

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

Any suggestions ?

like image 845
Vamshi Suram Avatar asked Nov 19 '14 10:11

Vamshi Suram


1 Answers

A couple of links helped me.

Testing AngularJs' $http.defaults.headers.common if specific header is set

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();
like image 68
Vamshi Suram Avatar answered Nov 04 '22 18:11

Vamshi Suram