@@ -6,6 +6,7 @@ import {assertRequest} from "./test-utils";
6
6
import { User } from "../fakes/global-options/User" ;
7
7
import { Controller } from "../../src/decorator/Controller" ;
8
8
import { Get } from "../../src/decorator/Get" ;
9
+ import { Ctx } from "../../src/decorator/Ctx" ;
9
10
import { Req } from "../../src/decorator/Req" ;
10
11
import { Res } from "../../src/decorator/Res" ;
11
12
import { Param } from "../../src/decorator/Param" ;
@@ -88,6 +89,24 @@ describe("action parameters", () => {
88
89
return "<html><body>hello</body></html>" ;
89
90
}
90
91
92
+ @Get ( "/users/direct" )
93
+ getUsersDirectExpress ( @Res ( ) response : any ) : any {
94
+ if ( typeof response . send === "function" )
95
+ response . status ( 201 ) . contentType ( "custom/x-sample" ) . send ( "hi, I was written directly to the response" ) ;
96
+ else {
97
+ response . status = 201 ;
98
+ response . type = "custom/x-sample" ;
99
+ response . body = "hi, I was written directly to the response" ;
100
+ }
101
+ }
102
+
103
+ @Get ( "/users/direct/ctx" )
104
+ getUsersDirectKoa ( @Ctx ( ) ctx : any ) : any {
105
+ ctx . response . status = 201 ;
106
+ ctx . response . type = "custom/x-sample" ;
107
+ ctx . response . body = "hi, I was written directly to the response using Koa Ctx" ;
108
+ }
109
+
91
110
@Get ( "/users/:userId" )
92
111
getUser ( @Param ( "userId" ) userId : number ) {
93
112
paramUserId = userId ;
@@ -346,6 +365,22 @@ describe("action parameters", () => {
346
365
} ) ;
347
366
} ) ;
348
367
368
+ describe ( "writing directly to the response using @Res should work" , ( ) => {
369
+ assertRequest ( [ 3001 , 3002 ] , "get" , "users/direct" , response => {
370
+ expect ( response ) . to . be . status ( 201 ) ;
371
+ expect ( response . body ) . to . be . equal ( "hi, I was written directly to the response" ) ;
372
+ expect ( response ) . to . have . header ( "content-type" , "custom/x-sample; charset=utf-8" ) ;
373
+ } ) ;
374
+ } ) ;
375
+
376
+ describe ( "writing directly to the response using @Ctx should work" , ( ) => {
377
+ assertRequest ( [ 3002 ] , "get" , "users/direct/ctx" , response => {
378
+ expect ( response ) . to . be . status ( 201 ) ;
379
+ expect ( response . body ) . to . be . equal ( "hi, I was written directly to the response using Koa Ctx" ) ;
380
+ expect ( response ) . to . have . header ( "content-type" , "custom/x-sample; charset=utf-8" ) ;
381
+ } ) ;
382
+ } ) ;
383
+
349
384
describe ( "@Param should give a param from route" , ( ) => {
350
385
assertRequest ( [ 3001 , 3002 ] , "get" , "users/1" , response => {
351
386
expect ( paramUserId ) . to . be . equal ( 1 ) ;
0 commit comments