@@ -5,75 +5,52 @@ if (!common.hasCrypto)
5
5
common . skip ( 'missing crypto' ) ;
6
6
const assert = require ( 'assert' ) ;
7
7
const http2 = require ( 'http2' ) ;
8
+ const Countdown = require ( '../common/countdown' ) ;
8
9
9
10
const {
10
- HTTP2_HEADER_METHOD ,
11
- HTTP2_HEADER_PATH ,
12
- HTTP2_METHOD_POST ,
13
11
NGHTTP2_CANCEL ,
14
12
NGHTTP2_NO_ERROR ,
15
13
NGHTTP2_PROTOCOL_ERROR ,
16
14
NGHTTP2_REFUSED_STREAM ,
17
15
NGHTTP2_INTERNAL_ERROR
18
16
} = http2 . constants ;
19
17
20
- const errCheck = common . expectsError ( { code : 'ERR_HTTP2_STREAM_ERROR' } , 6 ) ;
18
+ const tests = [
19
+ [ 'rstStream' , NGHTTP2_NO_ERROR , false ] ,
20
+ [ 'rstWithNoError' , NGHTTP2_NO_ERROR , false ] ,
21
+ [ 'rstWithProtocolError' , NGHTTP2_PROTOCOL_ERROR , true ] ,
22
+ [ 'rstWithCancel' , NGHTTP2_CANCEL , false ] ,
23
+ [ 'rstWithRefuse' , NGHTTP2_REFUSED_STREAM , true ] ,
24
+ [ 'rstWithInternalError' , NGHTTP2_INTERNAL_ERROR , true ]
25
+ ] ;
26
+
27
+ const server = http2 . createServer ( ) ;
28
+ server . on ( 'stream' , ( stream , headers ) => {
29
+ const method = headers [ 'rstmethod' ] ;
30
+ stream [ method ] ( ) ;
31
+ } ) ;
32
+
33
+ server . listen ( 0 , common . mustCall ( ( ) => {
34
+ const client = http2 . connect ( `http://localhost:${ server . address ( ) . port } ` ) ;
35
+
36
+ const countdown = new Countdown ( tests . length , common . mustCall ( ( ) => {
37
+ client . destroy ( ) ;
38
+ server . close ( ) ;
39
+ } ) ) ;
21
40
22
- function checkRstCode ( rstMethod , expectRstCode ) {
23
- const server = http2 . createServer ( ) ;
24
- server . on ( 'stream' , ( stream , headers , flags ) => {
25
- stream . respond ( {
26
- 'content-type' : 'text/html' ,
27
- ':status' : 200
41
+ tests . forEach ( ( test ) => {
42
+ const req = client . request ( {
43
+ ':method' : 'POST' ,
44
+ rstmethod : test [ 0 ]
28
45
} ) ;
29
- stream . write ( 'test' ) ;
30
- if ( rstMethod === 'rstStream' )
31
- stream [ rstMethod ] ( expectRstCode ) ;
32
- else
33
- stream [ rstMethod ] ( ) ;
34
-
35
- if ( expectRstCode !== NGHTTP2_NO_ERROR &&
36
- expectRstCode !== NGHTTP2_CANCEL ) {
37
- stream . on ( 'error' , common . mustCall ( errCheck ) ) ;
38
- } else {
39
- stream . on ( 'error' , common . mustNotCall ( ) ) ;
40
- }
41
- } ) ;
42
-
43
- server . listen ( 0 , common . mustCall ( ( ) => {
44
- const port = server . address ( ) . port ;
45
- const client = http2 . connect ( `http://localhost:${ port } ` ) ;
46
-
47
- const headers = {
48
- [ HTTP2_HEADER_PATH ] : '/' ,
49
- [ HTTP2_HEADER_METHOD ] : HTTP2_METHOD_POST
50
- } ;
51
- const req = client . request ( headers ) ;
52
-
53
- req . setEncoding ( 'utf8' ) ;
54
- req . on ( 'streamClosed' , common . mustCall ( ( actualRstCode ) => {
55
- assert . strictEqual (
56
- expectRstCode , actualRstCode , `${ rstMethod } is not match rstCode` ) ;
57
- server . close ( ) ;
58
- client . destroy ( ) ;
46
+ req . on ( 'streamClosed' , common . mustCall ( ( code ) => {
47
+ assert . strictEqual ( code , test [ 1 ] ) ;
48
+ countdown . dec ( ) ;
59
49
} ) ) ;
60
- req . on ( 'data' , common . mustCall ( ) ) ;
61
50
req . on ( 'aborted' , common . mustCall ( ) ) ;
62
- req . on ( 'end' , common . mustCall ( ) ) ;
63
-
64
- if ( expectRstCode !== NGHTTP2_NO_ERROR &&
65
- expectRstCode !== NGHTTP2_CANCEL ) {
66
- req . on ( 'error' , common . mustCall ( errCheck ) ) ;
67
- } else {
51
+ if ( test [ 2 ] )
52
+ req . on ( 'error' , common . mustCall ( ) ) ;
53
+ else
68
54
req . on ( 'error' , common . mustNotCall ( ) ) ;
69
- }
70
-
71
- } ) ) ;
72
- }
73
-
74
- checkRstCode ( 'rstStream' , NGHTTP2_NO_ERROR ) ;
75
- checkRstCode ( 'rstWithNoError' , NGHTTP2_NO_ERROR ) ;
76
- checkRstCode ( 'rstWithProtocolError' , NGHTTP2_PROTOCOL_ERROR ) ;
77
- checkRstCode ( 'rstWithCancel' , NGHTTP2_CANCEL ) ;
78
- checkRstCode ( 'rstWithRefuse' , NGHTTP2_REFUSED_STREAM ) ;
79
- checkRstCode ( 'rstWithInternalError' , NGHTTP2_INTERNAL_ERROR ) ;
55
+ } ) ;
56
+ } ) ) ;
0 commit comments