Welcome to the Linux Foundation Forum!

ch-6/labs-2: validate.js test case "incorrect" if solved with Fastify

Hello everyone,

I wanted to share something that might help clarify this exercise for others.

I completed the mentioned lab using Fastify, and there's a test case that, while not entirely wrong, feels "incorrect." Specifically, the case "DELETE http://localhost:65045/boat/1 must respond with a 204 response."

By default, Fastify responds with an HTTP 400 error when it receives a request with the 'content-type' header set to 'application/json' and an empty body. This behavior is by design (see Fastify documentation).

However, in validate.js line 30, every request is built with a fixed {'content-type': 'application/json'}, causing the above test case to fail.

I know that this issue can be resolved using hooks at either the application or route level, like so:

fastify.addHook('onRequest', (request, reply, done) => {
  if (request.headers['content-type'] === 'application/json' && !request.body) {
    delete request.headers['content-type'];
  }
  done();
});

However, this solution doesn't feel "right" to me, as I believe Fastify is behaving correctly and the request is being built incorrectly.

Thanks for your answers and input!

Categories

Upcoming Training