Skip to content

Commit d5538d5

Browse files
authored
test(response): cover blob/header responses; fix @Req/@res typing; bump to 2.11.1 (#128)
Addresses discussion #121. Adds tests for returning a Response with a Blob body and custom headers, @res() header setting, and @context() body/headers. Fixes @Req/@res to be typed as decorator factories so the documented @res() usage type-checks. Bumps version to 2.11.1.
1 parent 80e7c9b commit d5538d5

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@danet/core",
3-
"version": "2.11.0",
3+
"version": "2.11.1",
44
"license": "MIT",
55
"exports": {
66
".": "./mod.ts",

spec/response-decorator.test.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,40 @@ import { All, Controller, Delete, Get, HttpCode, Patch, Post, Put } from '../src
22
import { Module } from '../src/module/decorator.ts';
33
import { DanetApplication } from '../src/app.ts';
44
import { assertEquals } from '../src/deps_test.ts';
5+
import { Context, Res } from '../src/router/controller/params/decorators.ts';
6+
import type { ExecutionContext } from '../src/mod.ts';
57

68
@Controller('nice-controller')
79
class SimpleController {
810
@Get('/')
911
simpleGet() {
1012
return new Response('OK GET', { status: 201 });
1113
}
14+
15+
@Get('blob')
16+
blobWithHeaders() {
17+
const blob = new Blob(['hello blob'], { type: 'text/plain' });
18+
return new Response(blob, {
19+
status: 200,
20+
headers: {
21+
'Content-Type': 'text/plain',
22+
'Content-Disposition': 'attachment; filename="hello.txt"',
23+
'X-Custom-Header': 'from-response',
24+
},
25+
});
26+
}
27+
28+
@Get('with-res')
29+
setHeaderWithRes(@Res() res: Response) {
30+
res.headers.set('X-Custom-Header', 'from-res');
31+
return { ok: true };
32+
}
33+
34+
@Get('with-context')
35+
setBodyWithContext(@Context() ctx: ExecutionContext) {
36+
ctx.header('X-Custom-Header', 'from-context');
37+
return ctx.body('hello context');
38+
}
1239
}
1340

1441
@Module({
@@ -31,4 +58,58 @@ Deno.test('HttpCode', async () => {
3158
assertEquals(text, `OK GET`);
3259
assertEquals(res.status, 201);
3360
await app.close();
34-
});
61+
});
62+
63+
Deno.test('returning a Response with a Blob body sets body and headers', async () => {
64+
const app = new DanetApplication();
65+
await app.init(MyModule);
66+
const listenEvent = await app.listen(0);
67+
68+
const res = await fetch(
69+
`http://localhost:${listenEvent.port}/nice-controller/blob`,
70+
{
71+
method: 'GET',
72+
},
73+
);
74+
assertEquals(await res.text(), 'hello blob');
75+
assertEquals(res.status, 200);
76+
assertEquals(res.headers.get('Content-Type'), 'text/plain');
77+
assertEquals(
78+
res.headers.get('Content-Disposition'),
79+
'attachment; filename="hello.txt"',
80+
);
81+
assertEquals(res.headers.get('X-Custom-Header'), 'from-response');
82+
await app.close();
83+
});
84+
85+
Deno.test('@Res lets you set headers while returning a value', async () => {
86+
const app = new DanetApplication();
87+
await app.init(MyModule);
88+
const listenEvent = await app.listen(0);
89+
90+
const res = await fetch(
91+
`http://localhost:${listenEvent.port}/nice-controller/with-res`,
92+
{
93+
method: 'GET',
94+
},
95+
);
96+
assertEquals(await res.json(), { ok: true });
97+
assertEquals(res.headers.get('X-Custom-Header'), 'from-res');
98+
await app.close();
99+
});
100+
101+
Deno.test('@Context lets you set the body and headers', async () => {
102+
const app = new DanetApplication();
103+
await app.init(MyModule);
104+
const listenEvent = await app.listen(0);
105+
106+
const res = await fetch(
107+
`http://localhost:${listenEvent.port}/nice-controller/with-context`,
108+
{
109+
method: 'GET',
110+
},
111+
);
112+
assertEquals(await res.text(), 'hello context');
113+
assertEquals(res.headers.get('X-Custom-Header'), 'from-context');
114+
await app.close();
115+
});

src/router/controller/params/decorators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function createParamDecorator(
8787
/**
8888
* Get current request
8989
*/
90-
export const Req: DecoratorFunction = createParamDecorator(
90+
export const Req: () => DecoratorFunction = createParamDecorator(
9191
(context: ExecutionContext) => {
9292
return context.req;
9393
},
@@ -96,7 +96,7 @@ export const Req: DecoratorFunction = createParamDecorator(
9696
/**
9797
* Get current response
9898
*/
99-
export const Res: DecoratorFunction = createParamDecorator(
99+
export const Res: () => DecoratorFunction = createParamDecorator(
100100
(context: ExecutionContext) => {
101101
return context.res;
102102
},

0 commit comments

Comments
 (0)