Skip to content

Commit

Permalink
Avoid using parsers for OPTIONS method.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail committed May 7, 2024
1 parent 81ec8a6 commit 9a3b21c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export const initRouting = ({
routing,
hasCors: !!config.cors,
onEndpoint: (endpoint, path, method, siblingMethods) => {
// @todo skip for "options" method?
const middlewares = parsers?.[endpoint.getRequestType()] || [];
if (middlewares.length) {
app.use(path, middlewares);
if (method !== "options") {
const middlewares = parsers?.[endpoint.getRequestType()] || [];
if (middlewares.length) {
app.use(path, middlewares);
}
}
app[method](path, async (request, response) => {
const logger = config.childLoggerProvider
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Server", () => {
await createServer(configMock, routingMock);
expect(appMock).toBeTruthy();
expect(appMock.disable).toHaveBeenCalledWith("x-powered-by");
expect(appMock.use).toHaveBeenCalledTimes(5);
expect(appMock.use).toHaveBeenCalledTimes(4);
expect(appMock.use.mock.calls[0]).toEqual([
"/v1/test",
[expressJsonMock],
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("Server", () => {
expect(logger).toEqual(customLogger);
expect(app).toEqual(appMock);
expect(appMock).toBeTruthy();
expect(appMock.use).toHaveBeenCalledTimes(5);
expect(appMock.use).toHaveBeenCalledTimes(4);
expect(appMock.use.mock.calls[0]).toEqual([
"/v1/test",
[configMock.server.jsonParser],
Expand Down Expand Up @@ -200,7 +200,7 @@ describe("Server", () => {
},
};
await createServer(configMock, routingMock);
expect(appMock.use).toHaveBeenCalledTimes(5);
expect(appMock.use).toHaveBeenCalledTimes(4);
expect(compressionMock).toHaveBeenCalledTimes(1);
expect(compressionMock).toHaveBeenCalledWith(undefined);
});
Expand Down Expand Up @@ -235,7 +235,7 @@ describe("Server", () => {
},
};
await createServer(configMock, routingMock);
expect(appMock.use).toHaveBeenCalledTimes(4);
expect(appMock.use).toHaveBeenCalledTimes(3);
expect(appMock.use.mock.calls[0]).toEqual([
"/v1/test",
[beforeUpload, uploader, expect.any(Function)], // 3rd: createUploadFailueHandler()
Expand Down Expand Up @@ -271,7 +271,7 @@ describe("Server", () => {
},
};
await createServer(configMock, routingMock);
expect(appMock.use).toHaveBeenCalledTimes(4);
expect(appMock.use).toHaveBeenCalledTimes(3);
expect(appMock.use.mock.calls[0]).toEqual([
"/v1/test",
[rawParserMock, rawMover],
Expand Down

0 comments on commit 9a3b21c

Please sign in to comment.