@@ -157,5 +157,137 @@ vows.describe('http-server').addBatch({
157
157
assert . ok ( res . headers [ 'access-control-allow-headers' ] . split ( / \s * , \s * / g) . indexOf ( 'X-Test' ) >= 0 , 204 ) ;
158
158
}
159
159
}
160
+ } ,
161
+ 'When http-server is listening on 8083 with username "good_username" and password "good_password"' : {
162
+ topic : function ( ) {
163
+ var server = httpServer . createServer ( {
164
+ root : root ,
165
+ robots : true ,
166
+ headers : {
167
+ 'Access-Control-Allow-Origin' : '*' ,
168
+ 'Access-Control-Allow-Credentials' : 'true'
169
+ } ,
170
+ username : 'good_username' ,
171
+ password : 'good_password'
172
+ } ) ;
173
+
174
+ server . listen ( 8083 ) ;
175
+ this . callback ( null , server ) ;
176
+ } ,
177
+ 'and the user requests an existent file with no auth details' : {
178
+ topic : function ( ) {
179
+ request ( 'http://127.0.0.1:8083/file' , this . callback ) ;
180
+ } ,
181
+ 'status code should be 401' : function ( res ) {
182
+ assert . equal ( res . statusCode , 401 ) ;
183
+ } ,
184
+ 'and file content' : {
185
+ topic : function ( res , body ) {
186
+ var self = this ;
187
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
188
+ self . callback ( err , data , body ) ;
189
+ } ) ;
190
+ } ,
191
+ 'should be a forbidden message' : function ( err , file , body ) {
192
+ assert . equal ( body , 'Access denied' ) ;
193
+ }
194
+ }
195
+ } ,
196
+ 'and the user requests an existent file with incorrect username' : {
197
+ topic : function ( ) {
198
+ request ( 'http://127.0.0.1:8083/file' , {
199
+ auth : {
200
+ user : 'wrong_username' ,
201
+ pass : 'good_password'
202
+ }
203
+ } , this . callback ) ;
204
+ } ,
205
+ 'status code should be 401' : function ( res ) {
206
+ assert . equal ( res . statusCode , 401 ) ;
207
+ } ,
208
+ 'and file content' : {
209
+ topic : function ( res , body ) {
210
+ var self = this ;
211
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
212
+ self . callback ( err , data , body ) ;
213
+ } ) ;
214
+ } ,
215
+ 'should be a forbidden message' : function ( err , file , body ) {
216
+ assert . equal ( body , 'Access denied' ) ;
217
+ }
218
+ }
219
+ } ,
220
+ 'and the user requests an existent file with incorrect password' : {
221
+ topic : function ( ) {
222
+ request ( 'http://127.0.0.1:8083/file' , {
223
+ auth : {
224
+ user : 'good_username' ,
225
+ pass : 'wrong_password'
226
+ }
227
+ } , this . callback ) ;
228
+ } ,
229
+ 'status code should be 401' : function ( res ) {
230
+ assert . equal ( res . statusCode , 401 ) ;
231
+ } ,
232
+ 'and file content' : {
233
+ topic : function ( res , body ) {
234
+ var self = this ;
235
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
236
+ self . callback ( err , data , body ) ;
237
+ } ) ;
238
+ } ,
239
+ 'should be a forbidden message' : function ( err , file , body ) {
240
+ assert . equal ( body , 'Access denied' ) ;
241
+ }
242
+ }
243
+ } ,
244
+ 'and the user requests a non-existent file with incorrect password' : {
245
+ topic : function ( ) {
246
+ request ( 'http://127.0.0.1:8083/404' , {
247
+ auth : {
248
+ user : 'good_username' ,
249
+ pass : 'wrong_password'
250
+ }
251
+ } , this . callback ) ;
252
+ } ,
253
+ 'status code should be 401' : function ( res ) {
254
+ assert . equal ( res . statusCode , 401 ) ;
255
+ } ,
256
+ 'and file content' : {
257
+ topic : function ( res , body ) {
258
+ var self = this ;
259
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
260
+ self . callback ( err , data , body ) ;
261
+ } ) ;
262
+ } ,
263
+ 'should be a forbidden message' : function ( err , file , body ) {
264
+ assert . equal ( body , 'Access denied' ) ;
265
+ }
266
+ }
267
+ } ,
268
+ 'and the user requests an existent file with correct auth details' : {
269
+ topic : function ( ) {
270
+ request ( 'http://127.0.0.1:8083/file' , {
271
+ auth : {
272
+ user : 'good_username' ,
273
+ pass : 'good_password'
274
+ }
275
+ } , this . callback ) ;
276
+ } ,
277
+ 'status code should be 200' : function ( res ) {
278
+ assert . equal ( res . statusCode , 200 ) ;
279
+ } ,
280
+ 'and file content' : {
281
+ topic : function ( res , body ) {
282
+ var self = this ;
283
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
284
+ self . callback ( err , data , body ) ;
285
+ } ) ;
286
+ } ,
287
+ 'should match content of served file' : function ( err , file , body ) {
288
+ assert . equal ( body . trim ( ) , file . trim ( ) ) ;
289
+ }
290
+ }
291
+ }
160
292
}
161
293
} ) . export ( module ) ;
0 commit comments