1
+ 'use strict' ;
2
+
3
+ const net = require ( 'net' ) ;
4
+ const http = require ( 'http' ) ;
5
+ const assert = require ( 'assert' ) ;
6
+ const common = require ( '../common' ) ;
7
+
8
+ const bodySent = 'This is my request' ;
9
+
10
+ function assertResponse ( headers , body , expectClosed ) {
11
+ if ( expectClosed ) {
12
+ assert . match ( headers , / C o n n e c t i o n : c l o s e \r \n / m) ;
13
+ assert ( headers . search ( / K e e p - A l i v e : t i m e o u t = 5 , m a x = 3 \r \n / m) === - 1 ) ;
14
+ assert . match ( body , / H e l l o W o r l d ! / m) ;
15
+ } else {
16
+ assert . match ( headers , / C o n n e c t i o n : k e e p - a l i v e \r \n / m) ;
17
+ assert . match ( headers , / K e e p - A l i v e : t i m e o u t = 5 , m a x = 3 \r \n / m) ;
18
+ assert . match ( body , / H e l l o W o r l d ! / m) ;
19
+ }
20
+ }
21
+
22
+ function writeRequest ( socket ) {
23
+ socket . write ( 'POST / HTTP/1.1\r\n' ) ;
24
+ socket . write ( 'Connection: keep-alive\r\n' ) ;
25
+ socket . write ( 'Content-Type: text/plain\r\n' ) ;
26
+ socket . write ( `Content-Length: ${ bodySent . length } \r\n\r\n` ) ;
27
+ socket . write ( `${ bodySent } \r\n` ) ;
28
+ socket . write ( '\r\n\r\n' )
29
+ }
30
+
31
+ const server = http . createServer ( function ( req , res ) {
32
+ let body = ''
33
+ req . on ( 'data' , ( data ) => {
34
+ body += data
35
+ } ) ;
36
+
37
+ req . on ( 'end' , ( ) => {
38
+ if ( req . method === 'POST' ) {
39
+ assert ( bodySent === body )
40
+ }
41
+
42
+ res . writeHead ( 200 , { 'Content-Type' : 'text/plain' } ) ;
43
+ res . write ( 'Hello World!' ) ;
44
+ res . end ( ) ;
45
+ } )
46
+ } )
47
+
48
+ server . maxRequestsPerSocket = 3 ;
49
+
50
+ server . listen ( 0 , common . mustCall ( ( res ) => {
51
+ const socket = new net . Socket ( ) ;
52
+
53
+ socket . on ( 'end' , common . mustCall ( ( ) => {
54
+ server . close ( ) ;
55
+ } ) ) ;
56
+
57
+ socket . on ( 'ready' , common . mustCall ( ( ) => {
58
+ writeRequest ( socket ) ;
59
+ writeRequest ( socket ) ;
60
+ writeRequest ( socket ) ;
61
+ writeRequest ( socket ) ;
62
+ } ) ) ;
63
+
64
+ let buffer = ''
65
+
66
+ socket . on ( 'data' , ( data ) => {
67
+ buffer += data ;
68
+
69
+ const responseParts = buffer . trim ( ) . split ( '\r\n\r\n' ) ;
70
+
71
+ if ( responseParts . length === 8 ) {
72
+ assertResponse ( responseParts [ 0 ] , responseParts [ 1 ] ) ;
73
+ assertResponse ( responseParts [ 2 ] , responseParts [ 3 ] ) ;
74
+ assertResponse ( responseParts [ 4 ] , responseParts [ 5 ] , true ) ;
75
+
76
+ assert . match ( responseParts [ 6 ] , / H T T P \/ 1 .1 5 0 3 S e r v i c e U n a v a i l a b l e / m)
77
+ assert . match ( responseParts [ 6 ] , / C o n n e c t i o n : c l o s e \r \n / m) ;
78
+ assert ( responseParts [ 6 ] . search ( / K e e p - A l i v e : t i m e o u t = 5 , m a x = 3 \r \n / m) === - 1 ) ;
79
+ assert ( responseParts [ 7 ] . search ( / H e l l o W o r l d ! / m) === - 1 ) ;
80
+
81
+ socket . end ( ) ;
82
+ }
83
+ } )
84
+
85
+ socket . connect ( { port : server . address ( ) . port } ) ;
86
+ } ) ) ;
0 commit comments