File tree 4 files changed +71
-0
lines changed
4 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -238,6 +238,11 @@ function respond(ctx) {
238
238
239
239
// status body
240
240
if ( null == body ) {
241
+ if ( ctx . response . _explicitNullBody ) {
242
+ ctx . response . remove ( 'Content-Type' ) ;
243
+ ctx . response . remove ( 'Transfer-Encoding' ) ;
244
+ return res . end ( ) ;
245
+ }
241
246
if ( ctx . req . httpVersionMajor >= 2 ) {
242
247
body = String ( code ) ;
243
248
} else {
Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ module.exports = {
139
139
// no content
140
140
if ( null == val ) {
141
141
if ( ! statuses . empty [ this . status ] ) this . status = 204 ;
142
+ if ( val === null ) this . _explicitNullBody = true ;
142
143
this . remove ( 'Content-Type' ) ;
143
144
this . remove ( 'Content-Length' ) ;
144
145
this . remove ( 'Transfer-Encoding' ) ;
Original file line number Diff line number Diff line change @@ -810,4 +810,41 @@ describe('app.respond', () => {
810
810
assert . equal ( res . headers . hasOwnProperty ( 'content-type' ) , false ) ;
811
811
} ) ;
812
812
} ) ;
813
+
814
+ describe ( 'with explicit null body' , ( ) => {
815
+ it ( 'should preserve given status' , async ( ) => {
816
+ const app = new Koa ( ) ;
817
+
818
+ app . use ( ctx => {
819
+ ctx . body = null ;
820
+ ctx . status = 404 ;
821
+ } ) ;
822
+
823
+ const server = app . listen ( ) ;
824
+
825
+ return request ( server )
826
+ . get ( '/' )
827
+ . expect ( 404 )
828
+ . expect ( '' )
829
+ . expect ( { } ) ;
830
+ } ) ;
831
+ it ( 'should respond with correct headers' , async ( ) => {
832
+ const app = new Koa ( ) ;
833
+
834
+ app . use ( ctx => {
835
+ ctx . body = null ;
836
+ ctx . status = 401 ;
837
+ } ) ;
838
+
839
+ const server = app . listen ( ) ;
840
+
841
+ const res = await request ( server )
842
+ . get ( '/' )
843
+ . expect ( 401 )
844
+ . expect ( '' )
845
+ . expect ( { } ) ;
846
+
847
+ assert . equal ( res . headers . hasOwnProperty ( 'content-type' ) , false ) ;
848
+ } ) ;
849
+ } ) ;
813
850
} ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ describe('app.response', () => {
10
10
app1 . response . msg = 'hello' ;
11
11
const app2 = new Koa ( ) ;
12
12
const app3 = new Koa ( ) ;
13
+ const app4 = new Koa ( ) ;
14
+ const app5 = new Koa ( ) ;
13
15
14
16
it ( 'should merge properties' , ( ) => {
15
17
app1 . use ( ( ctx , next ) => {
@@ -43,4 +45,30 @@ describe('app.response', () => {
43
45
. expect ( 404 ) ;
44
46
assert . equal ( response . text , '404' ) ;
45
47
} ) ;
48
+
49
+ it ( 'should set ._explicitNullBody correctly' , async ( ) => {
50
+ app4 . use ( ( ctx , next ) => {
51
+ ctx . body = null ;
52
+ assert . strictEqual ( ctx . response . _explicitNullBody , true ) ;
53
+ } ) ;
54
+
55
+ return request ( app4 . listen ( ) )
56
+ . get ( '/' )
57
+ . expect ( 204 ) ;
58
+ } ) ;
59
+
60
+ it ( 'should not set ._explicitNullBody incorrectly' , async ( ) => {
61
+ app5 . use ( ( ctx , next ) => {
62
+ ctx . body = undefined ;
63
+ assert . strictEqual ( ctx . response . _explicitNullBody , undefined ) ;
64
+ ctx . body = '' ;
65
+ assert . strictEqual ( ctx . response . _explicitNullBody , undefined ) ;
66
+ ctx . body = false ;
67
+ assert . strictEqual ( ctx . response . _explicitNullBody , undefined ) ;
68
+ } ) ;
69
+
70
+ return request ( app5 . listen ( ) )
71
+ . get ( '/' )
72
+ . expect ( 204 ) ;
73
+ } ) ;
46
74
} ) ;
You can’t perform that action at this time.
0 commit comments