Skip to content

Commit bb1d0db

Browse files
authored
v0.12.1 - see CHANGELOG for details (#37)
1 parent f03686a commit bb1d0db

File tree

5 files changed

+62
-35
lines changed

5 files changed

+62
-35
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## [0.12.1] - 2024-11-05
2+
3+
### Changed
4+
5+
- laxed parsing rule for requests with header 'content-type: application/json'
6+
now covers all 3 methods: GET, DELETE, and HEAD
7+
- doc updated in README
8+
19
## [0.12.0] - 2024-11-05
210

311
### Changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ deno add @oak/oak @dklab/oak-routing-ctrl
5757
```ts
5858
// main.ts
5959

60-
import { Application } from "@oak/oak/application";
60+
import { Application } from "jsr:@oak/oak/application";
6161
import {
6262
Controller,
6363
ControllerMethodArgs,
6464
Get,
6565
useOakServer,
66-
} from "@dklab/oak-routing-ctrl";
66+
} from "jsr:@dklab/oak-routing-ctrl";
6767

6868
const app = new Application();
6969

@@ -96,13 +96,13 @@ curl localhost:1993/v1/hello/world # prints: hello, world
9696
<summary>View Example</summary>
9797

9898
```ts
99-
import { Application } from "@oak/oak/application";
99+
import { Application } from "jsr:@oak/oak/application";
100100
import {
101101
Controller,
102102
ControllerMethodArgs,
103103
Post,
104104
useOakServer,
105-
} from "@dklab/oak-routing-ctrl";
105+
} from "jsr:@dklab/oak-routing-ctrl";
106106

107107
@Controller("/v1")
108108
class MyController {
@@ -133,13 +133,13 @@ curl -H"Content-Type: application/json" localhost:1993/v1/tell/alice -d'{"messag
133133
<summary>View Example</summary>
134134

135135
```ts
136-
import { Application } from "@oak/oak/application";
136+
import { Application } from "jsr:@oak/oak/application";
137137
import {
138138
Controller,
139139
ControllerMethodArgs,
140140
Get,
141141
useOakServer,
142-
} from "@dklab/oak-routing-ctrl";
142+
} from "jsr:@dklab/oak-routing-ctrl";
143143

144144
@Controller("/v1")
145145
class MyController {
@@ -170,8 +170,8 @@ curl localhost:1993/v1/books/thriller\?page=2
170170
<summary>View Example</summary>
171171

172172
```ts
173-
import { Application } from "@oak/oak/application";
174-
import { Controller, Get, useOakServer } from "@dklab/oak-routing-ctrl";
173+
import { Application } from "jsr:@oak/oak/application";
174+
import { Controller, Get, useOakServer } from "jsr:@dklab/oak-routing-ctrl";
175175

176176
@Controller()
177177
class MyController {

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dklab/oak-routing-ctrl",
3-
"version": "0.12.0",
3+
"version": "0.12.1",
44
"exports": {
55
".": "./mod.ts",
66
"./mod": "./mod.ts"

src/ControllerMethodArgs.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,16 @@ function getEnhancedHandler(
129129
parsedReqBody = await _internal.parseOakReqBody(ctx);
130130
} catch (e) {
131131
if (
132-
ctx.request.method === "GET" &&
132+
["GET", "DELETE", "HEAD"].includes(ctx.request.method) &&
133133
ctx.request.headers.get("Content-Type") === "application/json" &&
134134
(e as Error).message?.includes("Unexpected end of JSON input")
135135
) {
136136
// we ignore this parsing error because the client was sending
137-
// a weird combination of method & content-type header
137+
// a weird combination of method & content-type header, but here to
138+
// the "Japanese engineering mindset":
139+
// https://www.500eboard.co/forums/threads/engineers-japanese-vs-german.14695/
138140
} else {
139-
// for other case, we trigger the error back to userland
141+
// for other scenarios, we trigger the error back to userland
140142
return ctx.throw(
141143
400,
142144
`Unable to parse request body: ${(e as Error).message}`,

src/ControllerMethodArgs_test.ts

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -636,30 +636,47 @@ Deno.test("getEnhancedHandler with a faulty ctx.request.body", async () => {
636636
spyParseOakRequestBody.restore();
637637
});
638638

639-
Deno.test("getEnhancedHandler with a faulty request method and content-type combination", async () => {
639+
Deno.test("getEnhancedHandler with a non-conventional request method and content-type combination", async (t) => {
640+
const methodsToTest = ["GET", "HEAD", "DELETE"];
640641
const spyParseOakRequestBody = spy(_internal, "parseOakReqBody");
641-
function testHandler() {
642-
return "weird.method.content-type.combination.handled";
643-
}
644-
// deno-lint-ignore ban-types
645-
const enhancedHandler: Function = _internal.getEnhancedHandler(testHandler);
646-
const ctx = createMockContext({
647-
method: "GET",
648-
headers: [["Content-Type", "application/json"]],
649-
});
650-
Object.defineProperty(ctx.request, "body", {
651-
get: () => createMockRequestBody("json", "Unexpected end of JSON input"),
652-
});
653-
const spyCtxThrow = spy();
654-
Object.defineProperty(ctx, "throw", {
655-
value: (errorStatus: unknown, message?: string, props?: unknown) => {
656-
spyCtxThrow(errorStatus, message, props);
657-
},
658-
});
659-
const retVal = await enhancedHandler(ctx);
660-
assertSpyCalls(spyCtxThrow, 0);
661-
assertSpyCalls(spyParseOakRequestBody, 1);
662-
assertEquals(retVal, "weird.method.content-type.combination.handled");
642+
await Promise.all(methodsToTest.map((method) =>
643+
t.step({
644+
name: `testing content-type: application/json and ${method} request`,
645+
fn: async () => {
646+
function testHandler() {
647+
return `method ${method} and Content-Type: application/json handled`;
648+
}
649+
// deno-lint-ignore ban-types
650+
const enhancedHandler: Function = _internal.getEnhancedHandler(
651+
testHandler,
652+
);
653+
const ctx = createMockContext({
654+
method, // GET | HEAD | DELETE
655+
headers: [["Content-Type", "application/json"]],
656+
});
657+
Object.defineProperty(ctx.request, "body", {
658+
get: () =>
659+
createMockRequestBody("json", "Unexpected end of JSON input"),
660+
});
661+
const spyCtxThrow = spy();
662+
Object.defineProperty(ctx, "throw", {
663+
value: (errorStatus: unknown, message?: string, props?: unknown) => {
664+
spyCtxThrow(errorStatus, message, props);
665+
},
666+
});
667+
const retVal = await enhancedHandler(ctx);
668+
assertSpyCalls(spyCtxThrow, 0);
669+
assertEquals(
670+
retVal,
671+
`method ${method} and Content-Type: application/json handled`,
672+
);
673+
},
674+
sanitizeOps: false,
675+
sanitizeResources: false,
676+
sanitizeExit: false,
677+
})
678+
));
679+
assertSpyCalls(spyParseOakRequestBody, methodsToTest.length);
663680
spyParseOakRequestBody.restore();
664681
});
665682

0 commit comments

Comments
 (0)