1
1
2
- var express = require ( '../' )
3
- , request = require ( 'supertest' )
4
- , assert = require ( 'assert' ) ;
2
+ var after = require ( 'after' ) ;
3
+ var assert = require ( 'assert' ) ;
4
+ var express = require ( '..' ) ;
5
+ var request = require ( 'supertest' ) ;
5
6
6
7
describe ( 'res' , function ( ) {
7
8
describe ( '.download(path)' , function ( ) {
@@ -38,27 +39,25 @@ describe('res', function(){
38
39
39
40
describe ( '.download(path, fn)' , function ( ) {
40
41
it ( 'should invoke the callback' , function ( done ) {
41
- var app = express ( )
42
- , calls = 0 ;
42
+ var app = express ( ) ;
43
+ var cb = after ( 2 , done ) ;
43
44
44
45
app . use ( function ( req , res ) {
45
- res . download ( 'test/fixtures/user.html' , done ) ;
46
+ res . download ( 'test/fixtures/user.html' , cb ) ;
46
47
} ) ;
47
48
48
49
request ( app )
49
50
. get ( '/' )
50
51
. expect ( 'Content-Type' , 'text/html; charset=UTF-8' )
51
52
. expect ( 'Content-Disposition' , 'attachment; filename="user.html"' )
52
- . expect ( 200 , function ( err ) {
53
- assert . ifError ( err )
54
- } )
53
+ . expect ( 200 , cb ) ;
55
54
} )
56
55
} )
57
56
58
57
describe ( '.download(path, filename, fn)' , function ( ) {
59
58
it ( 'should invoke the callback' , function ( done ) {
60
- var app = express ( )
61
- , calls = 0 ;
59
+ var app = express ( ) ;
60
+ var cb = after ( 2 , done ) ;
62
61
63
62
app . use ( function ( req , res ) {
64
63
res . download ( 'test/fixtures/user.html' , 'document' , done ) ;
@@ -68,48 +67,47 @@ describe('res', function(){
68
67
. get ( '/' )
69
68
. expect ( 'Content-Type' , 'text/html; charset=UTF-8' )
70
69
. expect ( 'Content-Disposition' , 'attachment; filename="document"' )
71
- . expect ( 200 , function ( err ) {
72
- assert . ifError ( err )
73
- } )
70
+ . expect ( 200 , cb ) ;
74
71
} )
75
72
} )
76
73
77
74
describe ( 'on failure' , function ( ) {
78
75
it ( 'should invoke the callback' , function ( done ) {
79
- var app = express ( )
80
- , calls = 0 ;
76
+ var app = express ( ) ;
81
77
82
- app . use ( function ( req , res ) {
78
+ app . use ( function ( req , res , next ) {
83
79
res . download ( 'test/fixtures/foobar.html' , function ( err ) {
84
- assert ( 404 == err . status ) ;
85
- assert ( 'ENOENT' == err . code ) ;
86
- done ( ) ;
80
+ if ( ! err ) return next ( new Error ( 'expected error' ) ) ;
81
+ res . send ( 'got ' + err . status + ' ' + err . code ) ;
87
82
} ) ;
88
83
} ) ;
89
84
90
85
request ( app )
91
86
. get ( '/' )
92
- . end ( function ( ) { } ) ;
87
+ . expect ( 200 , 'got 404 ENOENT' , done ) ;
93
88
} )
94
89
95
90
it ( 'should remove Content-Disposition' , function ( done ) {
96
91
var app = express ( )
97
92
, calls = 0 ;
98
93
99
- app . use ( function ( req , res ) {
94
+ app . use ( function ( req , res , next ) {
100
95
res . download ( 'test/fixtures/foobar.html' , function ( err ) {
96
+ if ( ! err ) return next ( new Error ( 'expected error' ) ) ;
101
97
res . end ( 'failed' ) ;
102
98
} ) ;
103
99
} ) ;
104
100
105
101
request ( app )
106
102
. get ( '/' )
107
- . expect ( 'failed' )
108
- . end ( function ( err , res ) {
109
- if ( err ) return done ( err ) ;
110
- res . header . should . not . have . property ( 'content-disposition' ) ;
111
- done ( ) ;
112
- } ) ;
103
+ . expect ( shouldNotHaveHeader ( 'Content-Disposition' ) )
104
+ . expect ( 200 , 'failed' , done ) ;
113
105
} )
114
106
} )
115
107
} )
108
+
109
+ function shouldNotHaveHeader ( header ) {
110
+ return function ( res ) {
111
+ assert . ok ( ! ( header . toLowerCase ( ) in res . headers ) , 'should not have header ' + header ) ;
112
+ } ;
113
+ }
0 commit comments