Skip to content

Commit 0f70577

Browse files
author
Lukas Siemon
committed
feat: added api error if incorrect request method is used
1 parent a522771 commit 0f70577

14 files changed

Lines changed: 53 additions & 11 deletions

lib/api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const endpoints = {};
1010
const Parser = (request, params) => {
1111
endpoints[request] = params;
1212
return (event) => {
13+
const expectedRequestMethod = request.split(" ")[0];
14+
const receivedRequestMethod = get(event, 'httpMethod');
15+
if (receivedRequestMethod !== expectedRequestMethod) {
16+
throw response.ApiError(`Request Method "${expectedRequestMethod}" expected.`, 400, 99004, {
17+
value: receivedRequestMethod
18+
});
19+
}
1320
let body;
1421
try {
1522
body = JSON.parse(get(event, 'body', '{}'));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

test/handler/test_error.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
"to.deep.equal": []
1313
}
1414
],
15-
"event": {}
15+
"event": {
16+
"httpMethod": "GET"
17+
}
1618
}

test/handler/test_exception.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
"to.deep.equal": []
1313
}
1414
],
15-
"event": {}
15+
"event": {
16+
"httpMethod": "GET"
17+
}
1618
}

test/handler/test_json.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
"to.deep.equal": []
1313
}
1414
],
15-
"event": {}
15+
"event": {
16+
"httpMethod": "GET"
17+
}
1618
}

test/handler/test_param.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"queryStringParameters": {
1717
"ref": "12345"
1818
},
19-
"body": "{\"username\": \"Anja Smith\", \"email\": \"anja@test.ca\"}"
19+
"body": "{\"username\": \"Anja Smith\", \"email\": \"anja@test.ca\"}",
20+
"httpMethod": "POST"
2021
}
2122
}

test/handler/test_param_invalid_body.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
}
1414
],
1515
"event": {
16-
"body": "invalid_json_string"
16+
"body": "invalid_json_string",
17+
"httpMethod": "POST"
1718
}
1819
}

test/handler/test_param_invalid_email.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
}
1414
],
1515
"event": {
16-
"body": "{\"username\": \"Anja Smith\", \"email\": \"invalid@email\"}"
16+
"body": "{\"username\": \"Anja Smith\", \"email\": \"invalid@email\"}",
17+
"httpMethod": "POST"
1718
}
1819
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"handler": "param",
3+
"success": true,
4+
"timeout": 2000,
5+
"response": [
6+
{
7+
"to.deep.equal": "{\"statusCode\":400,\"body\":\"{\\\"message\\\":\\\"Request Method \\\\\\\"POST\\\\\\\" expected.\\\",\\\"messageId\\\":99004,\\\"context\\\":{\\\"value\\\":\\\"GET\\\"}}\"}"
8+
}
9+
],
10+
"logs": [
11+
{
12+
"to.deep.equal": []
13+
}
14+
],
15+
"event": {
16+
"httpMethod": "GET"
17+
}
18+
}

test/handler/test_param_missing_required.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
}
1414
],
1515
"event": {
16-
"body": "{}"
16+
"body": "{}",
17+
"httpMethod": "POST"
1718
}
1819
}

0 commit comments

Comments
 (0)