3

I'm trying to write a test in Protractor/Jasmine that depends upon my being able to see the headers sent in an HTTP request. To that end I'm trying to create a mock endpoint with $httpBackend that will respond to a call with the headers themselves, allowing me to look into them in my test. My code is as follows:

describe('Headers', function () {
    it('should include X-XSRF-TOKEN on HTTP calls', function () {
        browser.addMockModule('httpBackendMock', function () {
            angular.module('httpBackendMock', ['CorsApp', 'ngMockE2E'])
            .run(function ($httpBackend) {
                $httpBackend.whenGET('/xsrftest')
                    .respond(function (method, url, data, headers) {
                        return headers;
                    });
            })
        });

        loginPage.get();

        browser.executeAsyncScript(function (callback) {
            var $http = angular.injector(['ng']).get('$http');

            $http.get('/xsrftest')
                .then(function (response) {
                    callback(response);
                });
        })
        .then(function (response) {
            console.log(response);
        });
    });
});

I've tried to follow the patterns set out in many resources for utilizing $httpBackend in protractor testing. However, when I run this test, I get a Protractor timeout. It seems as though the $http.get call never receives a response, hence the callback is never called and so the executeAsyncScript call times out. If I put in a dummy call to the callback that's not dependent on the $http.get, it works as expected.

What am I doing wrong in setting up $httpBackend? How can I get it to respond to my $http request?

Thanks!

Nat Webb
  • 669
  • 1
  • 7
  • 18

0 Answers0