1
1
'use strict' ;
2
- require ( '../common' ) ;
2
+ const common = require ( '../common' ) ;
3
3
const assert = require ( 'assert' ) ;
4
4
const http = require ( 'http' ) ;
5
5
@@ -11,55 +11,63 @@ const http = require('http');
11
11
// <ClientRequest>.method
12
12
// <ClientRequest>.path
13
13
14
- var testsComplete = 0 ;
15
- var test = 'headers' ;
16
- var content = 'hello world\n' ;
17
- var cookies = [
14
+ let test = 'headers' ;
15
+ const content = 'hello world\n' ;
16
+ const cookies = [
18
17
'session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT' ,
19
18
'prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT'
20
19
] ;
21
20
22
- var s = http . createServer ( function ( req , res ) {
21
+ const s = http . createServer ( common . mustCall ( ( req , res ) => {
23
22
switch ( test ) {
24
23
case 'headers' :
25
- assert . throws ( function ( ) { res . setHeader ( ) ; } ) ;
26
- assert . throws ( function ( ) { res . setHeader ( 'someHeader' ) ; } ) ;
27
- assert . throws ( function ( ) { res . getHeader ( ) ; } ) ;
28
- assert . throws ( function ( ) { res . removeHeader ( ) ; } ) ;
24
+ assert . throws ( ( ) => {
25
+ res . setHeader ( ) ;
26
+ } , / ^ T y p e E r r o r : H e a d e r n a m e m u s t b e a v a l i d H T T P T o k e n \[ " u n d e f i n e d " \] $ / ) ;
27
+ assert . throws ( ( ) => {
28
+ res . setHeader ( 'someHeader' ) ;
29
+ } , / ^ E r r o r : " v a l u e " r e q u i r e d i n s e t H e a d e r \( " s o m e H e a d e r " , v a l u e \) $ / ) ;
30
+ assert . throws ( ( ) => {
31
+ res . getHeader ( ) ;
32
+ } , / ^ E r r o r : " n a m e " a r g u m e n t i s r e q u i r e d f o r g e t H e a d e r \( n a m e \) $ / ) ;
33
+ assert . throws ( ( ) => {
34
+ res . removeHeader ( ) ;
35
+ } , / ^ E r r o r : " n a m e " a r g u m e n t i s r e q u i r e d f o r r e m o v e H e a d e r \( n a m e \) $ / ) ;
29
36
30
37
res . setHeader ( 'x-test-header' , 'testing' ) ;
31
38
res . setHeader ( 'X-TEST-HEADER2' , 'testing' ) ;
32
39
res . setHeader ( 'set-cookie' , cookies ) ;
33
40
res . setHeader ( 'x-test-array-header' , [ 1 , 2 , 3 ] ) ;
34
41
35
- var val1 = res . getHeader ( 'x-test-header' ) ;
36
- var val2 = res . getHeader ( 'x-test-header2' ) ;
37
- assert . equal ( val1 , 'testing' ) ;
38
- assert . equal ( val2 , 'testing' ) ;
42
+ assert . strictEqual ( res . getHeader ( 'x-test-header' ) , 'testing' ) ;
43
+ assert . strictEqual ( res . getHeader ( 'x-test-header2' ) , 'testing' ) ;
39
44
40
45
res . removeHeader ( 'x-test-header2' ) ;
41
46
break ;
42
47
43
48
case 'contentLength' :
44
49
res . setHeader ( 'content-length' , content . length ) ;
45
- assert . equal ( content . length , res . getHeader ( 'Content-Length' ) ) ;
50
+ assert . strictEqual ( res . getHeader ( 'Content-Length' ) , content . length ) ;
46
51
break ;
47
52
48
53
case 'transferEncoding' :
49
54
res . setHeader ( 'transfer-encoding' , 'chunked' ) ;
50
- assert . equal ( res . getHeader ( 'Transfer-Encoding' ) , 'chunked' ) ;
55
+ assert . strictEqual ( res . getHeader ( 'Transfer-Encoding' ) , 'chunked' ) ;
51
56
break ;
52
57
53
58
case 'writeHead' :
54
59
res . statusCode = 404 ;
55
60
res . setHeader ( 'x-foo' , 'keyboard cat' ) ;
56
61
res . writeHead ( 200 , { 'x-foo' : 'bar' , 'x-bar' : 'baz' } ) ;
57
62
break ;
63
+
64
+ default :
65
+ common . fail ( 'Unknown test' ) ;
58
66
}
59
67
60
68
res . statusCode = 201 ;
61
69
res . end ( content ) ;
62
- } ) ;
70
+ } , 4 ) ) ;
63
71
64
72
s . listen ( 0 , nextTest ) ;
65
73
@@ -69,64 +77,49 @@ function nextTest() {
69
77
return s . close ( ) ;
70
78
}
71
79
72
- var bufferedResponse = '' ;
73
-
74
- http . get ( { port : s . address ( ) . port } , function ( response ) {
75
- console . log ( 'TEST: ' + test ) ;
76
- console . log ( 'STATUS: ' + response . statusCode ) ;
77
- console . log ( 'HEADERS: ' ) ;
78
- console . dir ( response . headers ) ;
80
+ let bufferedResponse = '' ;
79
81
82
+ http . get ( { port : s . address ( ) . port } , common . mustCall ( ( response ) => {
80
83
switch ( test ) {
81
84
case 'headers' :
82
- assert . equal ( response . statusCode , 201 ) ;
83
- assert . equal ( response . headers [ 'x-test-header' ] ,
84
- 'testing' ) ;
85
- assert . equal ( response . headers [ 'x-test-array-header' ] ,
86
- [ 1 , 2 , 3 ] . join ( ', ' ) ) ;
87
- assert . deepStrictEqual ( cookies ,
88
- response . headers [ 'set-cookie' ] ) ;
89
- assert . equal ( response . headers [ 'x-test-header2' ] !== undefined , false ) ;
90
- // Make the next request
85
+ assert . strictEqual ( response . statusCode , 201 ) ;
86
+ assert . strictEqual ( response . headers [ 'x-test-header' ] , 'testing' ) ;
87
+ assert . strictEqual ( response . headers [ 'x-test-array-header' ] ,
88
+ [ 1 , 2 , 3 ] . join ( ', ' ) ) ;
89
+ assert . deepStrictEqual ( cookies , response . headers [ 'set-cookie' ] ) ;
90
+ assert . strictEqual ( response . headers [ 'x-test-header2' ] , undefined ) ;
91
91
test = 'contentLength' ;
92
- console . log ( 'foobar' ) ;
93
92
break ;
94
93
95
94
case 'contentLength' :
96
- assert . equal ( response . headers [ 'content-length' ] , content . length ) ;
95
+ assert . strictEqual ( + response . headers [ 'content-length' ] , content . length ) ;
97
96
test = 'transferEncoding' ;
98
97
break ;
99
98
100
99
case 'transferEncoding' :
101
- assert . equal ( response . headers [ 'transfer-encoding' ] , 'chunked' ) ;
100
+ assert . strictEqual ( response . headers [ 'transfer-encoding' ] , 'chunked' ) ;
102
101
test = 'writeHead' ;
103
102
break ;
104
103
105
104
case 'writeHead' :
106
- assert . equal ( response . headers [ 'x-foo' ] , 'bar' ) ;
107
- assert . equal ( response . headers [ 'x-bar' ] , 'baz' ) ;
108
- assert . equal ( 200 , response . statusCode ) ;
105
+ assert . strictEqual ( response . headers [ 'x-foo' ] , 'bar' ) ;
106
+ assert . strictEqual ( response . headers [ 'x-bar' ] , 'baz' ) ;
107
+ assert . strictEqual ( response . statusCode , 200 ) ;
109
108
test = 'end' ;
110
109
break ;
111
110
112
111
default :
113
- throw new Error ( '? ') ;
112
+ common . fail ( 'Unknown test ') ;
114
113
}
115
114
116
115
response . setEncoding ( 'utf8' ) ;
117
- response . on ( 'data' , function ( s ) {
116
+ response . on ( 'data' , ( s ) => {
118
117
bufferedResponse += s ;
119
118
} ) ;
120
119
121
- response . on ( 'end' , function ( ) {
122
- assert . equal ( content , bufferedResponse ) ;
123
- testsComplete ++ ;
124
- nextTest ( ) ;
125
- } ) ;
126
- } ) ;
120
+ response . on ( 'end' , common . mustCall ( ( ) => {
121
+ assert . strictEqual ( bufferedResponse , content ) ;
122
+ common . mustCall ( nextTest ) ( ) ;
123
+ } ) ) ;
124
+ } ) ) ;
127
125
}
128
-
129
-
130
- process . on ( 'exit' , function ( ) {
131
- assert . equal ( 4 , testsComplete ) ;
132
- } ) ;
0 commit comments