File tree 3 files changed +24
-5
lines changed
3 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,6 @@ const debug = require('debug')('koa:application');
10
10
const onFinished = require ( 'on-finished' ) ;
11
11
const response = require ( './response' ) ;
12
12
const compose = require ( 'koa-compose' ) ;
13
- const isJSON = require ( 'koa-is-json' ) ;
14
13
const context = require ( './context' ) ;
15
14
const request = require ( './request' ) ;
16
15
const statuses = require ( 'statuses' ) ;
@@ -225,9 +224,10 @@ function respond(ctx) {
225
224
return res . end ( ) ;
226
225
}
227
226
228
- if ( 'HEAD' == ctx . method ) {
229
- if ( ! res . headersSent && isJSON ( body ) ) {
230
- ctx . length = Buffer . byteLength ( JSON . stringify ( body ) ) ;
227
+ if ( 'HEAD' === ctx . method ) {
228
+ if ( ! res . headersSent && ! ctx . response . has ( 'Content-Length' ) ) {
229
+ const { length } = ctx . response ;
230
+ if ( Number . isInteger ( length ) ) ctx . length = length ;
231
231
}
232
232
return res . end ( ) ;
233
233
}
Original file line number Diff line number Diff line change 40
40
"is-generator-function" : " ^1.0.7" ,
41
41
"koa-compose" : " ^4.1.0" ,
42
42
"koa-convert" : " ^1.2.0" ,
43
- "koa-is-json" : " ^1.0.0" ,
44
43
"on-finished" : " ^2.3.0" ,
45
44
"only" : " ~0.0.2" ,
46
45
"parseurl" : " ^1.3.2" ,
Original file line number Diff line number Diff line change @@ -173,6 +173,26 @@ describe('app.respond', () => {
173
173
assert ( ! res . text ) ;
174
174
} ) ;
175
175
176
+ it ( 'should keep stream header if set manually' , async ( ) => {
177
+ const app = new Koa ( ) ;
178
+
179
+ const { length } = fs . readFileSync ( 'package.json' ) ;
180
+
181
+ app . use ( ctx => {
182
+ ctx . length = length ;
183
+ ctx . body = fs . createReadStream ( 'package.json' ) ;
184
+ } ) ;
185
+
186
+ const server = app . listen ( ) ;
187
+
188
+ const res = await request ( server )
189
+ . head ( '/' )
190
+ . expect ( 200 ) ;
191
+
192
+ assert . equal ( res . header [ 'content-length' ] , length ) ;
193
+ assert ( ! res . text ) ;
194
+ } ) ;
195
+
176
196
it ( 'should respond with a 404 if no body was set' , ( ) => {
177
197
const app = new Koa ( ) ;
178
198
You can’t perform that action at this time.
0 commit comments