@@ -2,13 +2,40 @@ import { All, Controller, Delete, Get, HttpCode, Patch, Post, Put } from '../src
22import { Module } from '../src/module/decorator.ts' ;
33import { DanetApplication } from '../src/app.ts' ;
44import { 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' )
79class 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+ } ) ;
0 commit comments