diff --git a/.mocharc.cjs b/.mocharc.cjs index 69fad0055..71feb773a 100644 --- a/.mocharc.cjs +++ b/.mocharc.cjs @@ -8,6 +8,10 @@ if (env.TEST === undefined || env.TEST === "all") { spec = ["./src/**/*.test.js", "tests/**/*.test.js"] } +if (env.TEST === "e2e") { + spec = ["tests/end-to-end/**/*.test.js"] +} + if (env.TEST === "unit") { spec = ["./src/**/*.test.js", "tests/old-unit/**/*.test.js"] } diff --git a/package.json b/package.json index 18c1ecf77..8a888390d 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,9 @@ "prettier:fix": "prettier --write .", "test": "mocha --require ./tests/mochaHooks.cjs", "test:cov": "NODE_OPTIONS='--experimental-loader @istanbuljs/esm-loader-hook' nyc --reporter=html npm test", - "test:node": "TEST=unit mocha --require ./tests/mochaHooks.cjs", - "test:unit": "TEST=unit mocha --require ./tests/mochaHooks.cjs" + "test:node": "TEST=node mocha --require ./tests/mochaHooks.cjs", + "test:unit": "TEST=unit mocha --require ./tests/mochaHooks.cjs", + "test:e2e": "TEST=e2e mocha --require ./tests/mochaHooks.cjs" }, "repository": { "type": "git", diff --git a/src/events/http/HttpServer.js b/src/events/http/HttpServer.js index e827272b7..e59175770 100644 --- a/src/events/http/HttpServer.js +++ b/src/events/http/HttpServer.js @@ -654,8 +654,7 @@ export default class HttpServer { log.debug(`Using response '${responseName}'`) - const chosenResponse = endpoint.responses[responseName] - + const chosenResponse = endpoint.responses?.[responseName] ?? {} /* RESPONSE PARAMETERS PROCCESSING */ const { responseParameters } = chosenResponse diff --git a/tests/end-to-end/defaultResponse/defaultResponse.test.js b/tests/end-to-end/defaultResponse/defaultResponse.test.js new file mode 100644 index 000000000..e1a06e2fc --- /dev/null +++ b/tests/end-to-end/defaultResponse/defaultResponse.test.js @@ -0,0 +1,34 @@ +import assert from "node:assert" +import { join } from "desm" +import { BASE_URL } from "../../config.js" +import { setup, teardown } from "../../_testHelpers/index.js" + +describe("default response", function desc() { + beforeEach(() => + setup({ + servicePath: join(import.meta.url, "src"), + }), + ) + + afterEach(() => teardown()) + + it("when no default response is provided", async () => { + const url = new URL("/dev/product_without_default", BASE_URL) + const response = await fetch(url) + const json = await response.json() + + assert.deepEqual(json, { + foo: "bar", + }) + }) + + it("when default response is provided", async () => { + const url = new URL("/dev/product_with_default", BASE_URL) + const response = await fetch(url) + const json = await response.json() + + assert.deepEqual(json, { + foo: "bar", + }) + }) +}) diff --git a/tests/end-to-end/defaultResponse/src/handler.js b/tests/end-to-end/defaultResponse/src/handler.js new file mode 100644 index 000000000..d83e3581b --- /dev/null +++ b/tests/end-to-end/defaultResponse/src/handler.js @@ -0,0 +1,10 @@ +const { stringify } = JSON + +export const hello = async () => { + return { + body: stringify({ + foo: "bar", + }), + statusCode: 200, + } +} diff --git a/tests/end-to-end/defaultResponse/src/package.json b/tests/end-to-end/defaultResponse/src/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/tests/end-to-end/defaultResponse/src/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/tests/end-to-end/defaultResponse/src/serverless.yml b/tests/end-to-end/defaultResponse/src/serverless.yml new file mode 100644 index 000000000..1a8c68373 --- /dev/null +++ b/tests/end-to-end/defaultResponse/src/serverless.yml @@ -0,0 +1,43 @@ +service: uncategorized-tests + +frameworkVersion: "3" + +plugins: + - ../../../../src/index.js + +provider: + name: aws + region: us-east-1 + runtime: nodejs18.x + stage: dev + apiGateway: + minimumCompressionSize: 1024 + shouldStartNameWithService: true + +functions: + helloWithoutDefault: + events: + - http: + method: get + path: /product_without_default + responses: + 200: + description: This is a success response + bodyType: Product + handler: handler.hello + helloWithDefault: + events: + - http: + method: get + path: /product_with_default + responses: + default: + description: This is a default response + bodyType: Product + 200: + description: This is a success response + bodyType: Product + handler: handler.hello + +package: + individually: true