mardi 17 mai 2016

Calling server.inject() POST request not calling handler in Hapi.js

I have a Jasmine test like this:

describe('my tests', () => {
  it('POST should return 201 created', () => {
    var req = {
      method: 'POST',
      url: '/api/v1.0/message',
      payload: JSON.stringify({name: 'Ethan'})
    };
    server.inject(req, res => {
      expect(res.statusCode).to.equal(201);
    });
  });
});

The route for the API call looks like this:

{
  path: '/api/v1.0/message',
  method: 'POST',
  handler: handlers.messagePost
}

And the handler is simply this:

messagePost: function(request, reply) {
  reply('Success').created();
}

When I run the tests, though, this particular test's expect() function doesn't get called because the server.inject() method doesn't call the response callback. In fact, not even the route handler method gets called (I checked with console.log statements). However, when I change the request method and the route from POST to GET, it works and the test calls the expect() method as expected. The test just doesn't work with POST requests. Am I doing it wrong?

Aucun commentaire:

Enregistrer un commentaire