jeudi 6 octobre 2016

Why is my cookie not available in my handler function when testing?

I am using Hapi and this is my handler function:

function propertyDetailsValidateHandler(request, reply, source, error) {
console.log(request.state)  
var data = joiValidationHelper.checkForErrors(request, error);
  if (typeof data !== "undefined"){
    return reply.view('property-details', data).code(400);
  } else {
    var details = request.state.details;
    details.propertyType = request.payload.propertyType;
    details.newBuild = request.payload.newBuild;
    return reply.redirect('/property-details/postcode').state('details', details, {path: '/'});
  }
}

And this is my test written using Jasmine:

describe('tell us about the property youre buying flow', function(){
  it('test /property-details, status code and location', function(done){
    var options = {
      method: 'POST',
      url: '/property-details',
      headers: {
        cookie: {details: { test: "test"}}
      },
      payload: {
        propertyType: "freehold",
        newBuild: true
      }
    };
    server.inject(options, function(response){
      detailsTestCookie = response.headers['set-cookie'][0].split(';')[0];
      expect(response.statusCode).toBe(302);
      expect(response.headers.location).toMatch("/property-details/postcode");
      done();
    });
  });
})

The handler function runs correctly when I run my server and use the browser but when I run the test request.state is an empty object when I was expecting it to be the cookie I provided in the test hence my test fails as request.state.details is undefined. Is this the correct way to provide the headers with a cookie in my test?

Aucun commentaire:

Enregistrer un commentaire