Skip to content

Commit

Permalink
feat(vite): use statusCode from Context response if superior to httpR…
Browse files Browse the repository at this point in the history
…esponse.statusCode
  • Loading branch information
camfou authored and Romakita committed Jun 13, 2024
1 parent a829a95 commit efed6a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions packages/third-parties/vike/src/services/ViteService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,49 @@ describe("ViteService", () => {
expect($ctx.response.status).toHaveBeenCalledWith(200);
expect($ctx.response.setHeader).toHaveBeenCalledWith("content-type", "text/html");
});

it("should render the page with statusCode from context.response if superior to httpResponse.statusCode", async () => {
const {$ctx, service, renderPage} = await getServiceFixture({
statusCode: 200,
headers: [["content-type", "text/html"]],
body: "html"
});

$ctx.response.status(404);
jest.spyOn($ctx.response, "status").mockReturnThis();
jest.spyOn($ctx.response, "setHeader").mockReturnThis();
const result = await service.render("*", {$ctx});

expect(result).toEqual({
body: "html",
headers: [["content-type", "text/html"]],
statusCode: 200
});
expect(renderPage).toHaveBeenCalledWith({
view: "*",
pageProps: {
view: "*"
},
contextProps: {
headers: {
host: "host",
"x-header": "x-header",
"user-agent": "ua"
},
host: "host",
method: "GET",
protocol: "https",
secure: true,
session: {},
stateSnapshot: {state: "state"},
url: "/"
},
urlOriginal: "/",
userAgent: "ua"
});
expect($ctx.response.status).toHaveBeenCalledWith(404);
expect($ctx.response.setHeader).toHaveBeenCalledWith("content-type", "text/html");
});
});
});
});
2 changes: 1 addition & 1 deletion packages/third-parties/vike/src/services/ViteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ViteService {
if (pageContext.httpResponse) {
const {httpResponse} = pageContext;
httpResponse.headers?.forEach(([name, value]: [string, string]) => $ctx.response.setHeader(name, value));
$ctx.response.status(httpResponse.statusCode);
$ctx.response.status(Math.max(httpResponse.statusCode, $ctx.response.statusCode));

if (this.enableStream) {
return httpResponse;
Expand Down

0 comments on commit efed6a0

Please sign in to comment.