1
1
'use strict' ;
2
2
// test compression/decompression with dictionary
3
3
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' ) ;
8
8
9
- var spdyDict = new Buffer ( [
9
+ const spdyDict = new Buffer ( [
10
10
'optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-' ,
11
11
'languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi' ,
12
12
'f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser' ,
@@ -22,54 +22,69 @@ var spdyDict = new Buffer([
22
22
'.1statusversionurl\0'
23
23
] . join ( '' ) ) ;
24
24
25
- var deflate = zlib . createDeflate ( { dictionary : spdyDict } ) ;
26
-
27
- var input = [
25
+ const input = [
28
26
'HTTP/1.1 200 Ok' ,
29
27
'Server: node.js' ,
30
28
'Content-Length: 0' ,
31
29
''
32
30
] . join ( '\r\n' ) ;
33
31
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 } ) ;
47
36
48
- // Put data into deflate stream
49
37
deflate . on ( 'data' , function ( chunk ) {
50
38
inflate . write ( chunk ) ;
51
39
} ) ;
52
40
53
- // Get data from inflate stream
54
- var output = [ ] ;
55
41
inflate . on ( 'data' , function ( chunk ) {
56
- output . push ( chunk ) ;
42
+ output += chunk ;
43
+ } ) ;
44
+
45
+ deflate . on ( 'end' , function ( ) {
46
+ inflate . end ( ) ;
57
47
} ) ;
48
+
58
49
inflate . on ( 'end' , function ( ) {
59
- called ++ ;
50
+ assert . equal ( input , output ) ;
51
+ } ) ;
52
+
53
+ deflate . write ( input ) ;
54
+ deflate . end ( ) ;
55
+ }
60
56
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 } ) ;
62
62
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 ) ;
64
78
} ) ;
65
79
66
80
deflate . write ( input ) ;
67
81
deflate . flush ( function ( ) {
68
- inflate . end ( ) ;
82
+ deflate . reset ( ) ;
83
+ doneReset = true ;
84
+ deflate . write ( input ) ;
85
+ deflate . end ( ) ;
69
86
} ) ;
70
87
}
71
- run ( 1 ) ;
72
88
73
- process . on ( 'exit' , function ( ) {
74
- assert . equal ( called , 2 ) ;
75
- } ) ;
89
+ basicDictionaryTest ( ) ;
90
+ deflateResetDictionaryTest ( ) ;
0 commit comments