@@ -3,6 +3,7 @@ var bytes = require('bytes')
3
3
var crypto = require ( 'crypto' )
4
4
var http = require ( 'http' )
5
5
var request = require ( 'supertest' )
6
+ var zlib = require ( 'zlib' )
6
7
7
8
var compression = require ( '..' )
8
9
@@ -581,98 +582,71 @@ describe('compression()', function () {
581
582
582
583
it ( 'should flush the response' , function ( done ) {
583
584
var chunks = 0
584
- var resp
585
+ var next
585
586
var server = createServer ( { threshold : 0 } , function ( req , res ) {
586
- resp = res
587
+ next = writeAndFlush ( res , 2 , new Buffer ( 1024 ) )
587
588
res . setHeader ( 'Content-Type' , 'text/plain' )
588
589
res . setHeader ( 'Content-Length' , '2048' )
589
- write ( )
590
+ next ( )
590
591
} )
591
592
592
- function write ( ) {
593
- chunks ++
594
- if ( chunks === 2 ) return resp . end ( )
595
- if ( chunks > 2 ) return chunks --
596
- resp . write ( new Buffer ( 1024 ) )
597
- resp . flush ( )
593
+ function onchunk ( chunk ) {
594
+ assert . ok ( chunks ++ < 2 )
595
+ assert . equal ( chunk . length , 1024 )
596
+ next ( )
598
597
}
599
598
600
599
request ( server )
601
600
. get ( '/' )
602
601
. set ( 'Accept-Encoding' , 'gzip' )
603
602
. request ( )
604
- . on ( 'response' , function ( res ) {
605
- assert . equal ( res . headers [ 'content-encoding' ] , 'gzip' )
606
- res . on ( 'data' , write )
607
- res . on ( 'end' , function ( ) {
608
- assert . equal ( chunks , 2 )
609
- done ( )
610
- } )
611
- } )
603
+ . on ( 'response' , unchunk ( 'gzip' , onchunk , done ) )
612
604
. end ( )
613
605
} )
614
606
615
607
it ( 'should flush small chunks for gzip' , function ( done ) {
616
608
var chunks = 0
617
- var resp
609
+ var next
618
610
var server = createServer ( { threshold : 0 } , function ( req , res ) {
619
- resp = res
611
+ next = writeAndFlush ( res , 2 , new Buffer ( '..' ) )
620
612
res . setHeader ( 'Content-Type' , 'text/plain' )
621
- write ( )
613
+ next ( )
622
614
} )
623
615
624
- function write ( ) {
625
- chunks ++
626
- if ( chunks === 20 ) return resp . end ( )
627
- if ( chunks > 20 ) return chunks --
628
- resp . write ( '..' )
629
- resp . flush ( )
616
+ function onchunk ( chunk ) {
617
+ assert . ok ( chunks ++ < 20 )
618
+ assert . equal ( chunk . toString ( ) , '..' )
619
+ next ( )
630
620
}
631
621
632
622
request ( server )
633
623
. get ( '/' )
634
624
. set ( 'Accept-Encoding' , 'gzip' )
635
625
. request ( )
636
- . on ( 'response' , function ( res ) {
637
- assert . equal ( res . headers [ 'content-encoding' ] , 'gzip' )
638
- res . on ( 'data' , write )
639
- res . on ( 'end' , function ( ) {
640
- assert . equal ( chunks , 20 )
641
- done ( )
642
- } )
643
- } )
626
+ . on ( 'response' , unchunk ( 'gzip' , onchunk , done ) )
644
627
. end ( )
645
628
} )
646
629
647
630
it ( 'should flush small chunks for deflate' , function ( done ) {
648
631
var chunks = 0
649
- var resp
632
+ var next
650
633
var server = createServer ( { threshold : 0 } , function ( req , res ) {
651
- resp = res
634
+ next = writeAndFlush ( res , 2 , new Buffer ( '..' ) )
652
635
res . setHeader ( 'Content-Type' , 'text/plain' )
653
- write ( )
636
+ next ( )
654
637
} )
655
638
656
- function write ( ) {
657
- chunks ++
658
- if ( chunks === 20 ) return resp . end ( )
659
- if ( chunks > 20 ) return chunks --
660
- resp . write ( '..' )
661
- resp . flush ( )
639
+ function onchunk ( chunk ) {
640
+ assert . ok ( chunks ++ < 20 )
641
+ assert . equal ( chunk . toString ( ) , '..' )
642
+ next ( )
662
643
}
663
644
664
645
request ( server )
665
646
. get ( '/' )
666
647
. set ( 'Accept-Encoding' , 'deflate' )
667
648
. request ( )
668
- . on ( 'response' , function ( res ) {
669
- assert . equal ( res . headers [ 'content-encoding' ] , 'deflate' )
670
- res . on ( 'data' , write )
671
- res . on ( 'end' , function ( ) {
672
- assert . equal ( chunks , 20 )
673
- done ( )
674
- } )
675
- } )
649
+ . on ( 'response' , unchunk ( 'deflate' , onchunk , done ) )
676
650
. end ( )
677
651
} )
678
652
} )
@@ -704,3 +678,34 @@ function shouldNotHaveHeader (header) {
704
678
assert . ok ( ! ( header . toLowerCase ( ) in res . headers ) , 'should not have header ' + header )
705
679
}
706
680
}
681
+
682
+ function writeAndFlush ( stream , count , buf ) {
683
+ var writes = 0
684
+
685
+ return function ( ) {
686
+ if ( writes ++ >= count ) return
687
+ if ( writes === count ) return stream . end ( buf )
688
+ stream . write ( buf )
689
+ stream . flush ( )
690
+ }
691
+ }
692
+
693
+ function unchunk ( encoding , onchunk , onend ) {
694
+ return function ( res ) {
695
+ var stream
696
+
697
+ assert . equal ( res . headers [ 'content-encoding' ] , encoding )
698
+
699
+ switch ( encoding ) {
700
+ case 'deflate' :
701
+ stream = res . pipe ( zlib . createInflate ( ) )
702
+ break
703
+ case 'gzip' :
704
+ stream = res . pipe ( zlib . createGunzip ( ) )
705
+ break
706
+ }
707
+
708
+ stream . on ( 'data' , onchunk )
709
+ stream . on ( 'end' , onend )
710
+ }
711
+ }
0 commit comments