@@ -379,5 +379,140 @@ vows.describe('http-server').addBatch({
379
379
teardown : function ( server ) {
380
380
server . close ( ) ;
381
381
}
382
+ } ,
383
+ 'When http-server is listening on 8086 with username "good_username" and Number type password 123456' : {
384
+ topic : function ( ) {
385
+ var server = httpServer . createServer ( {
386
+ root : root ,
387
+ robots : true ,
388
+ headers : {
389
+ 'Access-Control-Allow-Origin' : '*' ,
390
+ 'Access-Control-Allow-Credentials' : 'true'
391
+ } ,
392
+ username : 'good_username' ,
393
+ password : 123456
394
+ } ) ;
395
+
396
+ server . listen ( 8086 ) ;
397
+ this . callback ( null , server ) ;
398
+ } ,
399
+ 'and the user requests an existent file with no auth details' : {
400
+ topic : function ( ) {
401
+ request ( 'http://127.0.0.1:8086/file' , this . callback ) ;
402
+ } ,
403
+ 'status code should be 401' : function ( res ) {
404
+ assert . equal ( res . statusCode , 401 ) ;
405
+ } ,
406
+ 'and file content' : {
407
+ topic : function ( res , body ) {
408
+ var self = this ;
409
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
410
+ self . callback ( err , data , body ) ;
411
+ } ) ;
412
+ } ,
413
+ 'should be a forbidden message' : function ( err , file , body ) {
414
+ assert . equal ( body , 'Access denied' ) ;
415
+ }
416
+ }
417
+ } ,
418
+ 'and the user requests an existent file with incorrect username' : {
419
+ topic : function ( ) {
420
+ request ( 'http://127.0.0.1:8086/file' , {
421
+ auth : {
422
+ user : 'wrong_username' ,
423
+ pass : '123456'
424
+ }
425
+ } , this . callback ) ;
426
+ } ,
427
+ 'status code should be 401' : function ( res ) {
428
+ assert . equal ( res . statusCode , 401 ) ;
429
+ } ,
430
+ 'and file content' : {
431
+ topic : function ( res , body ) {
432
+ var self = this ;
433
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
434
+ self . callback ( err , data , body ) ;
435
+ } ) ;
436
+ } ,
437
+ 'should be a forbidden message' : function ( err , file , body ) {
438
+ assert . equal ( body , 'Access denied' ) ;
439
+ }
440
+ }
441
+ } ,
442
+ 'and the user requests an existent file with incorrect password' : {
443
+ topic : function ( ) {
444
+ request ( 'http://127.0.0.1:8086/file' , {
445
+ auth : {
446
+ user : 'good_username' ,
447
+ pass : '654321'
448
+ }
449
+ } , this . callback ) ;
450
+ } ,
451
+ 'status code should be 401' : function ( res ) {
452
+ assert . equal ( res . statusCode , 401 ) ;
453
+ } ,
454
+ 'and file content' : {
455
+ topic : function ( res , body ) {
456
+ var self = this ;
457
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
458
+ self . callback ( err , data , body ) ;
459
+ } ) ;
460
+ } ,
461
+ 'should be a forbidden message' : function ( err , file , body ) {
462
+ assert . equal ( body , 'Access denied' ) ;
463
+ }
464
+ }
465
+ } ,
466
+ 'and the user requests a non-existent file with incorrect password' : {
467
+ topic : function ( ) {
468
+ request ( 'http://127.0.0.1:8086/404' , {
469
+ auth : {
470
+ user : 'good_username' ,
471
+ pass : '654321'
472
+ }
473
+ } , this . callback ) ;
474
+ } ,
475
+ 'status code should be 401' : function ( res ) {
476
+ assert . equal ( res . statusCode , 401 ) ;
477
+ } ,
478
+ 'and file content' : {
479
+ topic : function ( res , body ) {
480
+ var self = this ;
481
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
482
+ self . callback ( err , data , body ) ;
483
+ } ) ;
484
+ } ,
485
+ 'should be a forbidden message' : function ( err , file , body ) {
486
+ assert . equal ( body , 'Access denied' ) ;
487
+ }
488
+ }
489
+ } ,
490
+ 'and the user requests an existent file with correct auth details' : {
491
+ topic : function ( ) {
492
+ request ( 'http://127.0.0.1:8086/file' , {
493
+ auth : {
494
+ user : 'good_username' ,
495
+ pass : '123456'
496
+ }
497
+ } , this . callback ) ;
498
+ } ,
499
+ 'status code should be 200' : function ( res ) {
500
+ assert . equal ( res . statusCode , 200 ) ;
501
+ } ,
502
+ 'and file content' : {
503
+ topic : function ( res , body ) {
504
+ var self = this ;
505
+ fs . readFile ( path . join ( root , 'file' ) , 'utf8' , function ( err , data ) {
506
+ self . callback ( err , data , body ) ;
507
+ } ) ;
508
+ } ,
509
+ 'should match content of served file' : function ( err , file , body ) {
510
+ assert . equal ( body . trim ( ) , file . trim ( ) ) ;
511
+ }
512
+ }
513
+ } ,
514
+ teardown : function ( server ) {
515
+ server . close ( ) ;
516
+ }
382
517
}
383
518
} ) . export ( module ) ;
0 commit comments