Skip to content

Commit

Permalink
fix(handler): remove content-type on options request
Browse files Browse the repository at this point in the history
  • Loading branch information
brunozoric committed Jan 9, 2025
1 parent 3b921bd commit 9dac616
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class NotAuthorizedAssetReply extends AssetReply {
code: 403,
headers: ResponseHeaders.create({
"cache-control": "no-store",
"content-type": "application/json"
"content-type": "application/json; charset=utf-8"
}),
body: () => ({ error: "Not authorized!", code: "NOT_AUTHORIZED" })
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe("HTTP Options request", () => {
body: "",
headers: {
...versionHeaders,
"content-type": "application/json; charset=utf-8",
"access-control-allow-headers": "*",
"access-control-allow-methods": ["OPTIONS", "POST"].sort().join(","),
"access-control-allow-origin": "*",
Expand Down
1 change: 0 additions & 1 deletion packages/handler/__tests__/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ describe("ResponseHeaders class", () => {
cookies: [],
headers: {
"cache-control": "public, max-age=86400",
"content-type": "application/json; charset=utf-8",
"access-control-allow-origin": "*",
"access-control-allow-headers": "*",
"access-control-allow-methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH",
Expand Down
58 changes: 46 additions & 12 deletions packages/handler/__tests__/onRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { createHandlerOnRequest } from "~/plugins/HandlerOnRequestPlugin";

jest.setTimeout(5000);

const createOptionsRoute = () => {
return createRoute(({ onOptions }) => {
onOptions("/webiny-test", async (request, reply) => {
const createRoutes = () => {
return createRoute(({ onPost, onOptions }) => {
onPost("/webiny-test", async (_, reply) => {
return reply.send({
weGotToPostReply: true
});
});
onOptions("/webiny-test", async (_, reply) => {
return reply.send({
weGotToOptionsReply: true
});
Expand All @@ -17,37 +22,66 @@ const createOptionsRoute = () => {
describe("fastify onRequest event", () => {
it("should return our built-in headers when sending options request", async () => {
const app = createHandler({
plugins: [createOptionsRoute()]
plugins: [createRoutes()]
});

const result = await app.inject({
const optionsResult = await app.inject({
path: "/webiny-test",
method: "OPTIONS",
query: {},
payload: JSON.stringify({})
payload: JSON.stringify({}),
headers: {
"content-type": "application/json"
}
});

expect(result).toMatchObject({
expect(optionsResult).toMatchObject({
statusCode: 204,
cookies: [],
headers: {
"cache-control": "public, max-age=86400",
"content-type": "application/json; charset=utf-8",
"access-control-allow-origin": "*",
"access-control-allow-headers": "*",
"access-control-allow-methods": "OPTIONS",
"access-control-allow-methods": "OPTIONS,POST",
"access-control-max-age": "86400",
connection: "keep-alive"
connection: "keep-alive",
date: expect.toBeDateString()
},
body: "",
payload: ""
});

const postResult = await app.inject({
path: "/webiny-test",
method: "POST",
query: {},
payload: JSON.stringify({}),
headers: {
"content-type": "application/json"
}
});

expect(postResult).toMatchObject({
statusCode: 200,
cookies: [],
headers: {
"cache-control": "no-store",
"content-type": "application/json; charset=utf-8",
"access-control-allow-origin": "*",
"access-control-allow-headers": "*",
"access-control-allow-methods": "OPTIONS,POST",
connection: "keep-alive",
date: expect.toBeDateString()
},
body: JSON.stringify({ weGotToPostReply: true }),
payload: JSON.stringify({ weGotToPostReply: true })
});
});

it("should return users headers set via the plugin", async () => {
const app = createHandler({
plugins: [
createOptionsRoute(),
createRoutes(),
createHandlerOnRequest(async (request, reply) => {
const raw = reply.code(205).hijack().raw;

Expand Down Expand Up @@ -82,7 +116,7 @@ describe("fastify onRequest event", () => {
it("should throw a log if user did not end onRequest plugin correctly", async () => {
const app = createHandler({
plugins: [
createOptionsRoute(),
createRoutes(),
createHandlerOnRequest(async (request, reply) => {
const raw = reply.code(205).hijack().raw;

Expand Down
4 changes: 3 additions & 1 deletion packages/handler/__tests__/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ describe("route plugins", () => {
HEAD: ["/webiny-get", "/webiny-head", "/webiny-all"],
UNLOCK: ["/webiny-all"],
TRACE: ["/webiny-all"],
REPORT: ["/webiny-all"],
SEARCH: ["/webiny-all"],
LOCK: ["/webiny-all"],
MOVE: ["/webiny-all"],
PROPPATCH: ["/webiny-all"],
COPY: ["/webiny-all"],
PROPFIND: ["/webiny-all"],
MKCOL: ["/webiny-all"]
MKCOL: ["/webiny-all"],
MKCALENDAR: ["/webiny-all"]
};

expect(app.webiny.routes.defined).toEqual(expected);
Expand Down

0 comments on commit 9dac616

Please sign in to comment.