Skip to content

Commit

Permalink
fix(platform-serverless): add log when the operation isn't defined on…
Browse files Browse the repository at this point in the history
… the given endpoint
  • Loading branch information
Romakita committed Jun 12, 2024
1 parent 356205e commit 7cf34f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PlatformTest} from "@tsed/common";
import {Get, JsonEntityStore, Redirect, Returns} from "@tsed/schema";
import {setResponseHeaders} from "./setResponseHeaders.js";
import {createServerlessContext} from "../../test/utils/createServerlessContext.js";
import {setResponseHeaders} from "./setResponseHeaders.js";

describe("setResponseHeaders", () => {
beforeEach(() => PlatformTest.create());
Expand Down Expand Up @@ -126,4 +126,30 @@ describe("setResponseHeaders", () => {
// THEN
return expect(ctx.response.isDone).toHaveBeenCalled();
});
it("should do nothing if the endpoint isn't an operation", async () => {
class Test {
test() {}
}

const endpoint = JsonEntityStore.fromMethod(Test, "test");

const ctx = createServerlessContext({
endpoint: {}
});

jest.spyOn(ctx.response, "setHeaders");
jest.spyOn(ctx.response, "isDone");
jest.spyOn(ctx.logger, "debug");

// WHEN
await setResponseHeaders(ctx);

// THEN
expect(ctx.response.isDone).toHaveBeenCalled();
expect(ctx.logger.debug).toHaveBeenCalledWith({
event: "MISSING_OPERATION_METADATA",
message: "No operation found on the endpoint. The response headers are not set.",
endpoint: {}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ function mergeHeaders(specHeaders: Record<string, JsonHeader & {example: string}
*/
export function setResponseHeaders(ctx: ServerlessContext) {
const {response, endpoint} = ctx;
const operation = endpoint.operation!;
const operation = endpoint.operation;

if (ctx.isDone()) {
return;
}

if (!operation) {
ctx.logger.debug({
event: "MISSING_OPERATION_METADATA",
message: "No operation found on the endpoint. The response headers are not set.",
endpoint: endpoint
});
return;
}

if (!response.hasStatus()) {
// apply status only if the isn't already modified
response.status(operation.getStatus());
Expand Down

0 comments on commit 7cf34f6

Please sign in to comment.