-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathresponse-decorator.test.ts
More file actions
116 lines (103 loc) · 3.49 KB
/
Copy pathresponse-decorator.test.ts
File metadata and controls
116 lines (103 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { All, Controller, Delete, Get, HttpCode, Patch, Post, Put } from '../src/router/controller/decorator.ts';
import { Module } from '../src/module/decorator.ts';
import { DanetApplication } from '../src/app.ts';
import { assertEquals } from '../src/deps_test.ts';
import { Context, Res } from '../src/router/controller/params/decorators.ts';
import type { ExecutionContext } from '../src/mod.ts';
@Controller('nice-controller')
class SimpleController {
@Get('/')
simpleGet() {
return new Response('OK GET', { status: 201 });
}
@Get('blob')
blobWithHeaders() {
const blob = new Blob(['hello blob'], { type: 'text/plain' });
return new Response(blob, {
status: 200,
headers: {
'Content-Type': 'text/plain',
'Content-Disposition': 'attachment; filename="hello.txt"',
'X-Custom-Header': 'from-response',
},
});
}
@Get('with-res')
setHeaderWithRes(@Res() res: Response) {
res.headers.set('X-Custom-Header', 'from-res');
return { ok: true };
}
@Get('with-context')
setBodyWithContext(@Context() ctx: ExecutionContext) {
const data = new TextEncoder().encode('hello context');
ctx.header('X-Custom-Header', 'from-context');
return ctx.body(data);
}
}
@Module({
controllers: [SimpleController],
})
class MyModule {}
Deno.test('HttpCode', async () => {
const app = new DanetApplication();
await app.init(MyModule);
const listenEvent = await app.listen(0);
const res = await fetch(
`http://localhost:${listenEvent.port}/nice-controller`,
{
method: 'GET',
},
);
const text = await res.text();
assertEquals(text, `OK GET`);
assertEquals(res.status, 201);
await app.close();
});
Deno.test('returning a Response with a Blob body sets body and headers', async () => {
const app = new DanetApplication();
await app.init(MyModule);
const listenEvent = await app.listen(0);
const res = await fetch(
`http://localhost:${listenEvent.port}/nice-controller/blob`,
{
method: 'GET',
},
);
assertEquals(await res.text(), 'hello blob');
assertEquals(res.status, 200);
assertEquals(res.headers.get('Content-Type'), 'text/plain');
assertEquals(
res.headers.get('Content-Disposition'),
'attachment; filename="hello.txt"',
);
assertEquals(res.headers.get('X-Custom-Header'), 'from-response');
await app.close();
});
Deno.test('@Res lets you set headers while returning a value', async () => {
const app = new DanetApplication();
await app.init(MyModule);
const listenEvent = await app.listen(0);
const res = await fetch(
`http://localhost:${listenEvent.port}/nice-controller/with-res`,
{
method: 'GET',
},
);
assertEquals(await res.json(), { ok: true });
assertEquals(res.headers.get('X-Custom-Header'), 'from-res');
await app.close();
});
Deno.test('@Context lets you set the body and headers', async () => {
const app = new DanetApplication();
await app.init(MyModule);
const listenEvent = await app.listen(0);
const res = await fetch(
`http://localhost:${listenEvent.port}/nice-controller/with-context`,
{
method: 'GET',
},
);
assertEquals(await res.text(), 'hello context');
assertEquals(res.headers.get('X-Custom-Header'), 'from-context');
await app.close();
});