@@ -86,46 +86,12 @@ describe('res', function(){
86
86
} )
87
87
} )
88
88
89
- describe ( '.download(path, filename, fn)' , function ( ) {
90
- it ( 'should invoke the callback' , function ( done ) {
91
- var app = express ( ) ;
92
- var cb = after ( 2 , done ) ;
93
-
94
- app . use ( function ( req , res ) {
95
- res . download ( 'test/fixtures/user.html' , 'document' , cb )
96
- } ) ;
97
-
98
- request ( app )
99
- . get ( '/' )
100
- . expect ( 'Content-Type' , 'text/html; charset=UTF-8' )
101
- . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
102
- . expect ( 200 , cb ) ;
103
- } )
104
- } )
105
-
106
- describe ( '.download(path, filename, options, fn)' , function ( ) {
107
- it ( 'should invoke the callback' , function ( done ) {
108
- var app = express ( )
109
- var cb = after ( 2 , done )
110
- var options = { }
111
-
112
- app . use ( function ( req , res ) {
113
- res . download ( 'test/fixtures/user.html' , 'document' , options , cb )
114
- } )
115
-
116
- request ( app )
117
- . get ( '/' )
118
- . expect ( 200 )
119
- . expect ( 'Content-Type' , 'text/html; charset=UTF-8' )
120
- . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
121
- . end ( cb )
122
- } )
123
-
89
+ describe ( '.download(path, options)' , function ( ) {
124
90
it ( 'should allow options to res.sendFile()' , function ( done ) {
125
91
var app = express ( )
126
92
127
93
app . use ( function ( req , res ) {
128
- res . download ( 'test/fixtures/.name' , 'document' , {
94
+ res . download ( 'test/fixtures/.name' , {
129
95
dotfiles : 'allow' ,
130
96
maxAge : '4h'
131
97
} )
@@ -134,51 +100,124 @@ describe('res', function(){
134
100
request ( app )
135
101
. get ( '/' )
136
102
. expect ( 200 )
137
- . expect ( 'Content-Disposition' , 'attachment; filename="document "' )
103
+ . expect ( 'Content-Disposition' , 'attachment; filename=".name "' )
138
104
. expect ( 'Cache-Control' , 'public, max-age=14400' )
139
105
. expect ( utils . shouldHaveBody ( Buffer . from ( 'tobi' ) ) )
140
106
. end ( done )
141
107
} )
142
108
143
- describe ( 'when options. headers contains Content-Disposition ' , function ( ) {
144
- it ( 'should be ignored ' , function ( done ) {
109
+ describe ( 'with " headers" option ' , function ( ) {
110
+ it ( 'should set headers on response ' , function ( done ) {
145
111
var app = express ( )
146
112
147
113
app . use ( function ( req , res ) {
148
- res . download ( 'test/fixtures/user.html' , 'document' , {
114
+ res . download ( 'test/fixtures/user.html' , {
149
115
headers : {
150
- 'Content-Type ' : 'text/x-custom ' ,
151
- 'Content-Disposition ' : 'inline '
116
+ 'X-Foo ' : 'Bar ' ,
117
+ 'X-Bar ' : 'Foo '
152
118
}
153
119
} )
154
120
} )
155
121
156
122
request ( app )
157
- . get ( '/' )
158
- . expect ( 200 )
159
- . expect ( 'Content-Type ' , 'text/x-custom ' )
160
- . expect ( 'Content-Disposition ' , 'attachment; filename="document" ' )
161
- . end ( done )
123
+ . get ( '/' )
124
+ . expect ( 200 )
125
+ . expect ( 'X-Foo ' , 'Bar ' )
126
+ . expect ( 'X-Bar ' , 'Foo ' )
127
+ . end ( done )
162
128
} )
163
129
164
- it ( 'should be ignored case-insensitively ' , function ( done ) {
130
+ it ( 'should use last header when duplicated ' , function ( done ) {
165
131
var app = express ( )
166
132
167
133
app . use ( function ( req , res ) {
168
- res . download ( 'test/fixtures/user.html' , 'document' , {
134
+ res . download ( 'test/fixtures/user.html' , {
169
135
headers : {
170
- 'content-type ' : 'text/x-custom ' ,
171
- 'content-disposition ' : 'inline '
136
+ 'X-Foo ' : 'Bar ' ,
137
+ 'x-foo ' : 'bar '
172
138
}
173
139
} )
174
140
} )
175
141
176
142
request ( app )
177
- . get ( '/' )
178
- . expect ( 200 )
179
- . expect ( 'Content-Type' , 'text/x-custom' )
180
- . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
181
- . end ( done )
143
+ . get ( '/' )
144
+ . expect ( 200 )
145
+ . expect ( 'X-Foo' , 'bar' )
146
+ . end ( done )
147
+ } )
148
+
149
+ it ( 'should override Content-Type' , function ( done ) {
150
+ var app = express ( )
151
+
152
+ app . use ( function ( req , res ) {
153
+ res . download ( 'test/fixtures/user.html' , {
154
+ headers : {
155
+ 'Content-Type' : 'text/x-custom'
156
+ }
157
+ } )
158
+ } )
159
+
160
+ request ( app )
161
+ . get ( '/' )
162
+ . expect ( 200 )
163
+ . expect ( 'Content-Type' , 'text/x-custom' )
164
+ . end ( done )
165
+ } )
166
+
167
+ it ( 'should not set headers on 404' , function ( done ) {
168
+ var app = express ( )
169
+
170
+ app . use ( function ( req , res ) {
171
+ res . download ( 'test/fixtures/does-not-exist' , {
172
+ headers : {
173
+ 'X-Foo' : 'Bar'
174
+ }
175
+ } )
176
+ } )
177
+
178
+ request ( app )
179
+ . get ( '/' )
180
+ . expect ( 404 )
181
+ . expect ( utils . shouldNotHaveHeader ( 'X-Foo' ) )
182
+ . end ( done )
183
+ } )
184
+
185
+ describe ( 'when headers contains Content-Disposition' , function ( ) {
186
+ it ( 'should be ignored' , function ( done ) {
187
+ var app = express ( )
188
+
189
+ app . use ( function ( req , res ) {
190
+ res . download ( 'test/fixtures/user.html' , {
191
+ headers : {
192
+ 'Content-Disposition' : 'inline'
193
+ }
194
+ } )
195
+ } )
196
+
197
+ request ( app )
198
+ . get ( '/' )
199
+ . expect ( 200 )
200
+ . expect ( 'Content-Disposition' , 'attachment; filename="user.html"' )
201
+ . end ( done )
202
+ } )
203
+
204
+ it ( 'should be ignored case-insensitively' , function ( done ) {
205
+ var app = express ( )
206
+
207
+ app . use ( function ( req , res ) {
208
+ res . download ( 'test/fixtures/user.html' , {
209
+ headers : {
210
+ 'content-disposition' : 'inline'
211
+ }
212
+ } )
213
+ } )
214
+
215
+ request ( app )
216
+ . get ( '/' )
217
+ . expect ( 200 )
218
+ . expect ( 'Content-Disposition' , 'attachment; filename="user.html"' )
219
+ . end ( done )
220
+ } )
182
221
} )
183
222
} )
184
223
@@ -187,15 +226,15 @@ describe('res', function(){
187
226
var app = express ( )
188
227
189
228
app . use ( function ( req , res ) {
190
- res . download ( 'name.txt' , 'document' , {
229
+ res . download ( 'name.txt' , {
191
230
root : FIXTURES_PATH
192
231
} )
193
232
} )
194
233
195
234
request ( app )
196
235
. get ( '/' )
197
236
. expect ( 200 )
198
- . expect ( 'Content-Disposition' , 'attachment; filename="document "' )
237
+ . expect ( 'Content-Disposition' , 'attachment; filename="name.txt "' )
199
238
. expect ( utils . shouldHaveBody ( Buffer . from ( 'tobi' ) ) )
200
239
. end ( done )
201
240
} )
@@ -204,15 +243,15 @@ describe('res', function(){
204
243
var app = express ( )
205
244
206
245
app . use ( function ( req , res ) {
207
- res . download ( 'fake/../name.txt' , 'document' , {
246
+ res . download ( 'fake/../name.txt' , {
208
247
root : FIXTURES_PATH
209
248
} )
210
249
} )
211
250
212
251
request ( app )
213
252
. get ( '/' )
214
253
. expect ( 200 )
215
- . expect ( 'Content-Disposition' , 'attachment; filename="document "' )
254
+ . expect ( 'Content-Disposition' , 'attachment; filename="name.txt "' )
216
255
. expect ( utils . shouldHaveBody ( Buffer . from ( 'tobi' ) ) )
217
256
. end ( done )
218
257
} )
@@ -224,7 +263,7 @@ describe('res', function(){
224
263
var p = '..' + path . sep +
225
264
path . relative ( path . dirname ( FIXTURES_PATH ) , path . join ( FIXTURES_PATH , 'name.txt' ) )
226
265
227
- res . download ( p , 'document' , {
266
+ res . download ( p , {
228
267
root : FIXTURES_PATH
229
268
} )
230
269
} )
@@ -240,7 +279,7 @@ describe('res', function(){
240
279
var app = express ( )
241
280
242
281
app . use ( function ( req , res ) {
243
- res . download ( '../name.txt' , 'document' , {
282
+ res . download ( '../name.txt' , {
244
283
root : FIXTURES_PATH
245
284
} )
246
285
} )
@@ -254,6 +293,103 @@ describe('res', function(){
254
293
} )
255
294
} )
256
295
296
+ describe ( '.download(path, filename, fn)' , function ( ) {
297
+ it ( 'should invoke the callback' , function ( done ) {
298
+ var app = express ( ) ;
299
+ var cb = after ( 2 , done ) ;
300
+
301
+ app . use ( function ( req , res ) {
302
+ res . download ( 'test/fixtures/user.html' , 'document' , cb )
303
+ } ) ;
304
+
305
+ request ( app )
306
+ . get ( '/' )
307
+ . expect ( 'Content-Type' , 'text/html; charset=UTF-8' )
308
+ . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
309
+ . expect ( 200 , cb ) ;
310
+ } )
311
+ } )
312
+
313
+ describe ( '.download(path, filename, options, fn)' , function ( ) {
314
+ it ( 'should invoke the callback' , function ( done ) {
315
+ var app = express ( )
316
+ var cb = after ( 2 , done )
317
+ var options = { }
318
+
319
+ app . use ( function ( req , res ) {
320
+ res . download ( 'test/fixtures/user.html' , 'document' , options , cb )
321
+ } )
322
+
323
+ request ( app )
324
+ . get ( '/' )
325
+ . expect ( 200 )
326
+ . expect ( 'Content-Type' , 'text/html; charset=UTF-8' )
327
+ . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
328
+ . end ( cb )
329
+ } )
330
+
331
+ it ( 'should allow options to res.sendFile()' , function ( done ) {
332
+ var app = express ( )
333
+
334
+ app . use ( function ( req , res ) {
335
+ res . download ( 'test/fixtures/.name' , 'document' , {
336
+ dotfiles : 'allow' ,
337
+ maxAge : '4h'
338
+ } )
339
+ } )
340
+
341
+ request ( app )
342
+ . get ( '/' )
343
+ . expect ( 200 )
344
+ . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
345
+ . expect ( 'Cache-Control' , 'public, max-age=14400' )
346
+ . expect ( utils . shouldHaveBody ( Buffer . from ( 'tobi' ) ) )
347
+ . end ( done )
348
+ } )
349
+
350
+ describe ( 'when options.headers contains Content-Disposition' , function ( ) {
351
+ it ( 'should be ignored' , function ( done ) {
352
+ var app = express ( )
353
+
354
+ app . use ( function ( req , res ) {
355
+ res . download ( 'test/fixtures/user.html' , 'document' , {
356
+ headers : {
357
+ 'Content-Type' : 'text/x-custom' ,
358
+ 'Content-Disposition' : 'inline'
359
+ }
360
+ } )
361
+ } )
362
+
363
+ request ( app )
364
+ . get ( '/' )
365
+ . expect ( 200 )
366
+ . expect ( 'Content-Type' , 'text/x-custom' )
367
+ . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
368
+ . end ( done )
369
+ } )
370
+
371
+ it ( 'should be ignored case-insensitively' , function ( done ) {
372
+ var app = express ( )
373
+
374
+ app . use ( function ( req , res ) {
375
+ res . download ( 'test/fixtures/user.html' , 'document' , {
376
+ headers : {
377
+ 'content-type' : 'text/x-custom' ,
378
+ 'content-disposition' : 'inline'
379
+ }
380
+ } )
381
+ } )
382
+
383
+ request ( app )
384
+ . get ( '/' )
385
+ . expect ( 200 )
386
+ . expect ( 'Content-Type' , 'text/x-custom' )
387
+ . expect ( 'Content-Disposition' , 'attachment; filename="document"' )
388
+ . end ( done )
389
+ } )
390
+ } )
391
+ } )
392
+
257
393
describe ( 'on failure' , function ( ) {
258
394
it ( 'should invoke the callback' , function ( done ) {
259
395
var app = express ( ) ;
0 commit comments