Skip to content

Commit

Permalink
fix: responses without default key (#1751)
Browse files Browse the repository at this point in the history
* fix: responses without default key

* fix: prettier errors

* test: add more tests and rename folder

* chore: fix prettier formatting
  • Loading branch information
DorianMazur committed Apr 18, 2024
1 parent c85a192 commit 9ce688a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .mocharc.cjs
Expand Up @@ -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"]
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/events/http/HttpServer.js
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions 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",
})
})
})
10 changes: 10 additions & 0 deletions 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,
}
}
3 changes: 3 additions & 0 deletions tests/end-to-end/defaultResponse/src/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
43 changes: 43 additions & 0 deletions 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

0 comments on commit 9ce688a

Please sign in to comment.