Repository navigation
Conversation
gurgunday
requested changes
Oct 24, 2026
gurgunday
left a comment
Member
There was a problem hiding this comment.
The behaviour is correct, but with this branch in place 2 fastify core tests fail that pass without it. They are constrained-routes.test.js:640 ("constrained GET route after a constrained HEAD route") and "fastify.all should add all the methods" in test/internals/all.test.js, both asserting a HEAD body through inject(). Fastify core needs updating before this ships in a minor release.
Author
|
Thanks! I opened fastify/fastify#7148, which updates those two tests to check the HEAD response headers ( |
gurgunday
pushed a commit
to fastify/fastify
that referenced
this pull request
Oct 24, 2026
Two tests read the body of a HEAD response through `fastify.inject()`: - "Should allow registering a constrained GET route after a constrained HEAD route" (`test/constrained-routes.test.js`) checks `res.payload === 'custom HEAD response'`; - "fastify.all should add all the methods to the same url" (`test/internals/all.test.js`) parses the HEAD payload as JSON. A real server sends no body for HEAD, and fastify/light-my-request#408 makes `inject()` do the same. @gurgunday approved the behaviour there but asked for fastify core to be updated before it ships, since these two tests would fail with it. These tests now check the response headers instead of the body: - The constrained route test asserts the `content-type` set by the custom HEAD handler (`text/plain`) and the `content-length` of `'custom HEAD response'`. That still shows the custom handler ran, not a HEAD route derived from the GET one. - The `fastify.all` test asserts the HEAD response's `content-length` matches `JSON.stringify({ method: 'HEAD' })` and parses the body only for the other methods. Both files pass with the current light-my-request release and with the #408 branch symlinked into `node_modules` (26 tests each time), so this can land before or after it. ESLint is clean on both files.
Tony133
pushed a commit
to fastify/fastify
that referenced
this pull request
Oct 24, 2026
Two tests read the body of a HEAD response through `fastify.inject()`: - "Should allow registering a constrained GET route after a constrained HEAD route" (`test/constrained-routes.test.js`) checks `res.payload === 'custom HEAD response'`; - "fastify.all should add all the methods to the same url" (`test/internals/all.test.js`) parses the HEAD payload as JSON. A real server sends no body for HEAD, and fastify/light-my-request#408 makes `inject()` do the same. @gurgunday approved the behaviour there but asked for fastify core to be updated before it ships, since these two tests would fail with it. These tests now check the response headers instead of the body: - The constrained route test asserts the `content-type` set by the custom HEAD handler (`text/plain`) and the `content-length` of `'custom HEAD response'`. That still shows the custom handler ran, not a HEAD route derived from the GET one. - The `fastify.all` test asserts the HEAD response's `content-length` matches `JSON.stringify({ method: 'HEAD' })` and parses the body only for the other methods. Both files pass with the current light-my-request release and with the #408 branch symlinked into `node_modules` (26 tests each time), so this can land before or after it. ESLint is clean on both files. (cherry picked from commit 78d68b2)
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
inject()returns the body a handler writes for aHEADrequest, or for a204or304response. A real Node.js server never sends it:http.ServerResponsesets_hasBody = falsein these cases and ignores the writes.I found this by sending the same requests to a dispatch function through
inject()and through a realhttp.createServer(). It is the same kind of mismatch that #388 fixed for request headers.The fix checks
this._hasBodyinResponse.prototype.writeafter delegating tohttp.ServerResponse, and drops the chunk when Node would (both the buffered payload andpayloadAsStream). Headers, includingcontent-length, are kept as they are, as on a real server.Tests: three new cases at the end of
test/index.test.js(HEAD, 204/304, HEAD withpayloadAsStream) fail without the change. "chainable api: http methods should work correctly" echoedreq.methodin the body and expected it back forheadtoo. It now expects an empty body forhead.npm test(182 tests, 100% coverage, tstyche) andnpm run lintpass.Note for fastify: I ran fastify's test suite against this branch (by symlinking it into
node_modules). 2 of 2388 tests read a HEAD body throughfastify.inject(): "Should allow registering a constrained GET route after a constrained HEAD route" (test/constrained-routes.test.js) and "fastify.all should add all the methods to the same url" (test/internals/all.test.js, which parses the HEAD payload as JSON). A real fastify server sends no body in either case. I'm happy to open the matching fastify PR if you want to take this.