Skip to content

Commit 4101b1b

Browse files
grixusethidden
andauthored
fix(ES-679): remove zero-valued Content-Length (#7239)
* fix(ES-679): remove zero-valued Content-Length * chore: changelog Co-authored-by: Artur <[email protected]> * style: reduce Co-authored-by: Artur <[email protected]> * fix: missing brackets, format, lint * fix: after cr * fix: another one --------- Co-authored-by: Artur <[email protected]>
1 parent ffafc4e commit 4101b1b

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

.changeset/yellow-ladybugs-add.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-storefront/sdk": patch
3+
---
4+
5+
**[FIXED]** `Content-Length` header will no longer be appended to requests if its value is "0" - such situation caused Next.js server to crash

packages/sdk/src/__tests__/integration/modules/middlewareModule.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,67 @@ describe("middlewareModule", () => {
252252
);
253253
});
254254

255+
it("should remove zero-valued Content-Length header as it would crash Next.js server", async () => {
256+
const customHttpClient = jest.fn();
257+
const sdkConfig = {
258+
commerce: buildModule(middlewareModule<Endpoints>, {
259+
apiUrl: "http://localhost:8181/commerce",
260+
cdnCacheBustingId: "commit-hash",
261+
httpClient: customHttpClient,
262+
defaultRequestConfig: {
263+
headers: {
264+
"Content-Length": "0",
265+
},
266+
},
267+
}),
268+
};
269+
const sdk = initSDK(sdkConfig);
270+
271+
await sdk.commerce.getProduct({ id: 1 });
272+
273+
expect(customHttpClient).toHaveBeenCalledWith(
274+
"http://localhost:8181/commerce/getProduct",
275+
expect.any(Array),
276+
expect.objectContaining({
277+
headers: {
278+
"Content-Type": "application/json",
279+
Accept: "application/json",
280+
},
281+
})
282+
);
283+
});
284+
285+
it("should pass non-zero-valued Content-Length header", async () => {
286+
const customHttpClient = jest.fn();
287+
const sdkConfig = {
288+
commerce: buildModule(middlewareModule<Endpoints>, {
289+
apiUrl: "http://localhost:8181/commerce",
290+
cdnCacheBustingId: "commit-hash",
291+
httpClient: customHttpClient,
292+
defaultRequestConfig: {
293+
headers: {
294+
"Content-Length": "1000",
295+
},
296+
},
297+
}),
298+
};
299+
const sdk = initSDK(sdkConfig);
300+
301+
await sdk.commerce.getProduct({ id: 1 });
302+
303+
expect(customHttpClient).toHaveBeenCalledWith(
304+
"http://localhost:8181/commerce/getProduct",
305+
expect.any(Array),
306+
expect.objectContaining({
307+
headers: {
308+
"Content-Type": "application/json",
309+
Accept: "application/json",
310+
"Content-Length": "1000",
311+
},
312+
})
313+
);
314+
});
315+
255316
it("should use different base URL during SSR if defined", async () => {
256317
const customHttpClient = jest.fn();
257318
const sdkConfig = {

packages/sdk/src/modules/middlewareModule/utils/getRequestSender.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,21 @@ export const getRequestSender = (options: Options): RequestSender => {
6868
Accept: "application/json",
6969
...defaultRequestConfig.headers,
7070
};
71-
const mergedHeaders = {
72-
...defaultHeaders,
73-
...methodHeaders,
74-
...headers,
75-
};
71+
const mergedHeaders = Object.fromEntries(
72+
Object.entries({
73+
...defaultHeaders,
74+
...methodHeaders,
75+
...headers,
76+
}).reduce<[string, string | string[]][]>(
77+
(headersSoFar, [key, value]) =>
78+
// Removing Content-Lenght header if it equals 0 because it would crash Next.js server
79+
// eslint-disable-next-line eqeqeq
80+
key.toLocaleLowerCase() === "content-length" && value == "0"
81+
? headersSoFar
82+
: [...headersSoFar, [key, value]],
83+
[]
84+
)
85+
);
7686

7787
const computedHeaders: ComputedConfig["headers"] = {};
7888
Object.entries(mergedHeaders).forEach(([key, value]) => {

0 commit comments

Comments
 (0)