Skip to content

Commit 9ce688a

Browse files
authored
fix: responses without default key (dherault#1751)
* fix: responses without default key * fix: prettier errors * test: add more tests and rename folder * chore: fix prettier formatting
1 parent c85a192 commit 9ce688a

File tree

7 files changed

+98
-4
lines changed

7 files changed

+98
-4
lines changed

.mocharc.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ if (env.TEST === undefined || env.TEST === "all") {
88
spec = ["./src/**/*.test.js", "tests/**/*.test.js"]
99
}
1010

11+
if (env.TEST === "e2e") {
12+
spec = ["tests/end-to-end/**/*.test.js"]
13+
}
14+
1115
if (env.TEST === "unit") {
1216
spec = ["./src/**/*.test.js", "tests/old-unit/**/*.test.js"]
1317
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"prettier:fix": "prettier --write .",
2121
"test": "mocha --require ./tests/mochaHooks.cjs",
2222
"test:cov": "NODE_OPTIONS='--experimental-loader @istanbuljs/esm-loader-hook' nyc --reporter=html npm test",
23-
"test:node": "TEST=unit mocha --require ./tests/mochaHooks.cjs",
24-
"test:unit": "TEST=unit mocha --require ./tests/mochaHooks.cjs"
23+
"test:node": "TEST=node mocha --require ./tests/mochaHooks.cjs",
24+
"test:unit": "TEST=unit mocha --require ./tests/mochaHooks.cjs",
25+
"test:e2e": "TEST=e2e mocha --require ./tests/mochaHooks.cjs"
2526
},
2627
"repository": {
2728
"type": "git",

src/events/http/HttpServer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,7 @@ export default class HttpServer {
654654

655655
log.debug(`Using response '${responseName}'`)
656656

657-
const chosenResponse = endpoint.responses[responseName]
658-
657+
const chosenResponse = endpoint.responses?.[responseName] ?? {}
659658
/* RESPONSE PARAMETERS PROCCESSING */
660659

661660
const { responseParameters } = chosenResponse
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import assert from "node:assert"
2+
import { join } from "desm"
3+
import { BASE_URL } from "../../config.js"
4+
import { setup, teardown } from "../../_testHelpers/index.js"
5+
6+
describe("default response", function desc() {
7+
beforeEach(() =>
8+
setup({
9+
servicePath: join(import.meta.url, "src"),
10+
}),
11+
)
12+
13+
afterEach(() => teardown())
14+
15+
it("when no default response is provided", async () => {
16+
const url = new URL("/dev/product_without_default", BASE_URL)
17+
const response = await fetch(url)
18+
const json = await response.json()
19+
20+
assert.deepEqual(json, {
21+
foo: "bar",
22+
})
23+
})
24+
25+
it("when default response is provided", async () => {
26+
const url = new URL("/dev/product_with_default", BASE_URL)
27+
const response = await fetch(url)
28+
const json = await response.json()
29+
30+
assert.deepEqual(json, {
31+
foo: "bar",
32+
})
33+
})
34+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { stringify } = JSON
2+
3+
export const hello = async () => {
4+
return {
5+
body: stringify({
6+
foo: "bar",
7+
}),
8+
statusCode: 200,
9+
}
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
service: uncategorized-tests
2+
3+
frameworkVersion: "3"
4+
5+
plugins:
6+
- ../../../../src/index.js
7+
8+
provider:
9+
name: aws
10+
region: us-east-1
11+
runtime: nodejs18.x
12+
stage: dev
13+
apiGateway:
14+
minimumCompressionSize: 1024
15+
shouldStartNameWithService: true
16+
17+
functions:
18+
helloWithoutDefault:
19+
events:
20+
- http:
21+
method: get
22+
path: /product_without_default
23+
responses:
24+
200:
25+
description: This is a success response
26+
bodyType: Product
27+
handler: handler.hello
28+
helloWithDefault:
29+
events:
30+
- http:
31+
method: get
32+
path: /product_with_default
33+
responses:
34+
default:
35+
description: This is a default response
36+
bodyType: Product
37+
200:
38+
description: This is a success response
39+
bodyType: Product
40+
handler: handler.hello
41+
42+
package:
43+
individually: true

0 commit comments

Comments
 (0)