11'use strict' ;
22// test compression/decompression with dictionary
33
4- var common = require ( '../common' ) ;
5- var assert = require ( 'assert' ) ;
6- var zlib = require ( 'zlib' ) ;
7- var path = require ( 'path' ) ;
4+ const common = require ( '../common' ) ;
5+ const assert = require ( 'assert' ) ;
6+ const zlib = require ( 'zlib' ) ;
7+ const path = require ( 'path' ) ;
88
9- var spdyDict = new Buffer ( [
9+ const spdyDict = new Buffer ( [
1010 'optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-' ,
1111 'languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi' ,
1212 'f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser' ,
@@ -22,54 +22,69 @@ var spdyDict = new Buffer([
2222 '.1statusversionurl\0'
2323] . join ( '' ) ) ;
2424
25- var deflate = zlib . createDeflate ( { dictionary : spdyDict } ) ;
26-
27- var input = [
25+ const input = [
2826 'HTTP/1.1 200 Ok' ,
2927 'Server: node.js' ,
3028 'Content-Length: 0' ,
3129 ''
3230] . join ( '\r\n' ) ;
3331
34- var called = 0 ;
35-
36- //
37- // We'll use clean-new inflate stream each time
38- // and .reset() old dirty deflate one
39- //
40- function run ( num ) {
41- var inflate = zlib . createInflate ( { dictionary : spdyDict } ) ;
42-
43- if ( num === 2 ) {
44- deflate . reset ( ) ;
45- deflate . removeAllListeners ( 'data' ) ;
46- }
32+ function basicDictionaryTest ( ) {
33+ let output = '' ;
34+ const deflate = zlib . createDeflate ( { dictionary : spdyDict } ) ;
35+ const inflate = zlib . createInflate ( { dictionary : spdyDict } ) ;
4736
48- // Put data into deflate stream
4937 deflate . on ( 'data' , function ( chunk ) {
5038 inflate . write ( chunk ) ;
5139 } ) ;
5240
53- // Get data from inflate stream
54- var output = [ ] ;
5541 inflate . on ( 'data' , function ( chunk ) {
56- output . push ( chunk ) ;
42+ output += chunk ;
43+ } ) ;
44+
45+ deflate . on ( 'end' , function ( ) {
46+ inflate . end ( ) ;
5747 } ) ;
48+
5849 inflate . on ( 'end' , function ( ) {
59- called ++ ;
50+ assert . equal ( input , output ) ;
51+ } ) ;
52+
53+ deflate . write ( input ) ;
54+ deflate . end ( ) ;
55+ }
6056
61- assert . equal ( output . join ( '' ) , input ) ;
57+ function deflateResetDictionaryTest ( ) {
58+ let doneReset = false ;
59+ let output = '' ;
60+ const deflate = zlib . createDeflate ( { dictionary : spdyDict } ) ;
61+ const inflate = zlib . createInflate ( { dictionary : spdyDict } ) ;
6262
63- if ( num < 2 ) run ( num + 1 ) ;
63+ deflate . on ( 'data' , function ( chunk ) {
64+ if ( doneReset )
65+ inflate . write ( chunk ) ;
66+ } ) ;
67+
68+ inflate . on ( 'data' , function ( chunk ) {
69+ output += chunk ;
70+ } ) ;
71+
72+ deflate . on ( 'end' , function ( ) {
73+ inflate . end ( ) ;
74+ } ) ;
75+
76+ inflate . on ( 'end' , function ( ) {
77+ assert . equal ( input , output ) ;
6478 } ) ;
6579
6680 deflate . write ( input ) ;
6781 deflate . flush ( function ( ) {
68- inflate . end ( ) ;
82+ deflate . reset ( ) ;
83+ doneReset = true ;
84+ deflate . write ( input ) ;
85+ deflate . end ( ) ;
6986 } ) ;
7087}
71- run ( 1 ) ;
7288
73- process . on ( 'exit' , function ( ) {
74- assert . equal ( called , 2 ) ;
75- } ) ;
89+ basicDictionaryTest ( ) ;
90+ deflateResetDictionaryTest ( ) ;
0 commit comments