11var assert = require ( 'assert' ) ;
2+ var fs = require ( 'fs' ) ;
23var sinon = require ( 'sinon' ) ;
34var support = require ( '../support' ) ;
45var responder = main . server . responder ;
@@ -7,7 +8,8 @@ var fake_res = {
78 header : function ( ) { } ,
89 status : function ( ) { } ,
910 json : function ( ) { } ,
10- send : function ( ) { }
11+ send : function ( ) { } ,
12+ setHeader : function ( ) { }
1113} ;
1214
1315var fake_req = {
@@ -125,4 +127,117 @@ describe("responder.js", function () {
125127 } ) ;
126128 } ) ;
127129 } ) ;
130+
131+ describe ( "download()" , function ( ) {
132+ var args ;
133+
134+ beforeEach ( function ( ) {
135+ args = {
136+ filename : 'foo.js' ,
137+ contentType : 'application/javascript' ,
138+ stream : fs . createReadStream ( __filename ) ,
139+ contentLength : 2000
140+
141+ } ;
142+ } ) ;
143+
144+ context ( "when no args passed in" , function ( ) {
145+ it ( "responds with an InternalError" , function ( done ) {
146+ sinon . stub ( responder , 'error' , function ( res , err , next ) {
147+ next ( ) ;
148+ } ) ;
149+
150+ responder . download ( fake_res , null , fake_next ) ;
151+
152+ assert . ok ( responder . error . called ) ;
153+
154+ responder . error . restore ( ) ;
155+ done ( ) ;
156+ } ) ;
157+ } ) ;
158+
159+ context ( "when args.filename not passed in" , function ( ) {
160+ it ( "responds with an InternalError" , function ( done ) {
161+ sinon . stub ( responder , 'error' , function ( res , err , next ) {
162+ next ( ) ;
163+ } ) ;
164+
165+ delete args . filename ;
166+ responder . download ( fake_res , args , fake_next ) ;
167+
168+ assert . ok ( responder . error . called ) ;
169+
170+ responder . error . restore ( ) ;
171+ done ( ) ;
172+ } ) ;
173+ } ) ;
174+
175+ context ( "when args.stream not passed in" , function ( ) {
176+ it ( "responds with an InternalError" , function ( done ) {
177+ sinon . stub ( responder , 'error' , function ( res , err , next ) {
178+ next ( ) ;
179+ } ) ;
180+
181+ delete args . stream ;
182+ responder . download ( fake_res , args , fake_next ) ;
183+
184+ assert . ok ( responder . error . called ) ;
185+
186+ responder . error . restore ( ) ;
187+ done ( ) ;
188+ } ) ;
189+ } ) ;
190+
191+ context ( "when args.contentType not passed in" , function ( ) {
192+ it ( "responds with Content-Type header set to application/octet-stream" , function ( done ) {
193+ sinon . spy ( fake_res , 'setHeader' ) ;
194+ sinon . stub ( args . stream , 'pipe' , function ( ) { } ) ;
195+
196+ delete args . contentType ;
197+ responder . download ( fake_res , args , fake_next ) ;
198+
199+ assert . ok ( fake_res . setHeader . calledWith ( 'Content-Type' , 'application/octet-stream' ) ) ;
200+
201+ fake_res . setHeader . restore ( ) ;
202+ args . stream . pipe . restore ( ) ;
203+ done ( ) ;
204+ } ) ;
205+ } ) ;
206+
207+ context ( "when args.contentLength not passed in" , function ( ) {
208+ it ( "responds without Content-Length header set" , function ( done ) {
209+ sinon . spy ( fake_res , 'setHeader' ) ;
210+ sinon . stub ( args . stream , 'pipe' , function ( ) { } ) ;
211+
212+ delete args . contentLength ;
213+ responder . download ( fake_res , args , fake_next ) ;
214+
215+ assert . ok ( ! fake_res . setHeader . calledWith ( 'Content-Length' ) ) ;
216+
217+ fake_res . setHeader . restore ( ) ;
218+ args . stream . pipe . restore ( ) ;
219+ done ( ) ;
220+ } ) ;
221+ } ) ;
222+
223+ context ( "when args passed in correctly" , function ( ) {
224+ it ( "responds without 200 status and content" , function ( done ) {
225+ sinon . spy ( fake_res , 'setHeader' ) ;
226+ sinon . spy ( fake_res , 'status' ) ;
227+ sinon . stub ( args . stream , 'pipe' , function ( ) { } ) ;
228+
229+ responder . download ( fake_res , args , fake_next ) ;
230+
231+ assert . ok ( fake_res . setHeader . calledWith ( 'Content-Type' , 'application/javascript' ) ) ;
232+ assert . ok ( fake_res . setHeader . calledWith ( 'Content-Length' , 2000 ) ) ;
233+ assert . ok ( fake_res . setHeader . calledWith ( 'Content-Disposition' , 'attachment; filename=foo.js' ) ) ;
234+ assert . ok ( fake_res . status . calledWith ( 200 ) ) ;
235+ assert . ok ( args . stream . pipe . calledWith ( fake_res ) ) ;
236+
237+ fake_res . setHeader . restore ( ) ;
238+ args . stream . pipe . restore ( ) ;
239+ done ( ) ;
240+ } ) ;
241+ } ) ;
242+ } ) ;
128243} ) ;
0 commit comments