Skip to content

Commit

Permalink
fix: set content-length header on sendResponseResult (#145)
Browse files Browse the repository at this point in the history
* Set content-length on sendResponseResult

* Use Buffer.byteLength instead of creating new buffer

* Added changeset

* Added unit test checks for content-length header
  • Loading branch information
KariHe authored Nov 25, 2021
1 parent e86800f commit d6071a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/weak-kids-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"graphql-helix": patch
---

Set content-length header on sendResponseResult
4 changes: 3 additions & 1 deletion packages/core/lib/send-result/node-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ export async function sendResponseResult(
for (const { name, value } of responseResult.headers) {
rawResponse.setHeader(name, value);
}
const data = JSON.stringify(transformResult(responseResult.payload))
rawResponse.writeHead(responseResult.status, {
"content-type": "application/json",
"content-length": Buffer.byteLength(data, "utf8")
});
rawResponse.end(JSON.stringify(transformResult(responseResult.payload)));
rawResponse.end(data);
}

export async function sendMultipartResponseResult(
Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ implementations.forEach((implementation) => {
test("POST basic query", async () => {
const {
body: { data, errors },
headers,
} = await post({
path: "/graphql",
port,
Expand All @@ -97,6 +98,7 @@ implementations.forEach((implementation) => {
});
expect(errors).toBeUndefined();
expect(data?.echo).toBeDefined();
expect(headers['content-length']).toBeDefined();
});

test("POST query with variables", async () => {
Expand Down Expand Up @@ -249,6 +251,7 @@ implementations.forEach((implementation) => {
test("GET basic query", async () => {
const {
body: { data, errors },
headers,
} = await get({
path: "/graphql",
port,
Expand All @@ -260,6 +263,7 @@ implementations.forEach((implementation) => {
});
expect(errors).toBeUndefined();
expect(data?.echo).toBeDefined();
expect(headers['content-length']).toBeDefined();
});

test("GET query with variables", async () => {
Expand Down

0 comments on commit d6071a1

Please sign in to comment.