|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +constcommon=require('../common'); |
| 4 | +constnet=require('net'); |
| 5 | +consthttp=require('http'); |
| 6 | +constassert=require('assert'); |
| 7 | + |
| 8 | +constbodySent='This is my request'; |
| 9 | + |
| 10 | +functionassertResponse(headers,body,expectClosed){ |
| 11 | +if(expectClosed){ |
| 12 | +assert.match(headers,/Connection:close\r\n/m); |
| 13 | +assert.strictEqual(headers.search(/Keep-Alive:timeout=5\r\n/m),-1); |
| 14 | +assert.match(body,/HelloWorld!/m); |
| 15 | +}else{ |
| 16 | +assert.match(headers,/Connection:keep-alive\r\n/m); |
| 17 | +assert.match(headers,/Keep-Alive:timeout=5\r\n/m); |
| 18 | +assert.match(body,/HelloWorld!/m); |
| 19 | +} |
| 20 | +} |
| 21 | + |
| 22 | +functionwriteRequest(socket,withBody){ |
| 23 | +if(withBody){ |
| 24 | +socket.write('POST / HTTP/1.1\r\n'); |
| 25 | +socket.write('Connection: keep-alive\r\n'); |
| 26 | +socket.write('Content-Type: text/plain\r\n'); |
| 27 | +socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`); |
| 28 | +socket.write(`${bodySent}\r\n`); |
| 29 | +socket.write('\r\n\r\n'); |
| 30 | +}else{ |
| 31 | +socket.write('GET / HTTP/1.1\r\n'); |
| 32 | +socket.write('Connection: keep-alive\r\n'); |
| 33 | +socket.write('\r\n\r\n'); |
| 34 | +} |
| 35 | +} |
| 36 | + |
| 37 | +constserver=http.createServer((req,res)=>{ |
| 38 | +letbody=''; |
| 39 | +req.on('data',(data)=>{ |
| 40 | +body+=data; |
| 41 | +}); |
| 42 | + |
| 43 | +req.on('end',()=>{ |
| 44 | +if(req.method==='POST'){ |
| 45 | +assert.strictEqual(bodySent,body); |
| 46 | +} |
| 47 | +res.writeHead(200,{'Content-Type': 'text/plain'}); |
| 48 | +res.write('Hello World!'); |
| 49 | +res.end(); |
| 50 | +}); |
| 51 | +}); |
| 52 | + |
| 53 | +functioninitialRequests(socket,numberOfRequests,cb){ |
| 54 | +letbuffer=''; |
| 55 | + |
| 56 | +writeRequest(socket); |
| 57 | + |
| 58 | +socket.on('data',(data)=>{ |
| 59 | +buffer+=data; |
| 60 | + |
| 61 | +if(buffer.endsWith('\r\n\r\n')){ |
| 62 | +if(--numberOfRequests===0){ |
| 63 | +socket.removeAllListeners('data'); |
| 64 | +cb(); |
| 65 | +}else{ |
| 66 | +const[headers,body]=buffer.trim().split('\r\n\r\n'); |
| 67 | +assertResponse(headers,body); |
| 68 | +buffer=''; |
| 69 | +writeRequest(socket,true); |
| 70 | +} |
| 71 | +} |
| 72 | +}); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +server.maxRequestsPerSocket=3; |
| 77 | +server.listen(0,common.mustCall((res)=>{ |
| 78 | +constsocket=newnet.Socket(); |
| 79 | +constanotherSocket=newnet.Socket(); |
| 80 | + |
| 81 | +socket.on('end',common.mustCall(()=>{ |
| 82 | +server.close(); |
| 83 | +})); |
| 84 | + |
| 85 | +socket.on('ready',common.mustCall(()=>{ |
| 86 | +// Do 2 of 3 allowed requests and ensure they still alive |
| 87 | +initialRequests(socket,2,common.mustCall(()=>{ |
| 88 | +anotherSocket.connect({port: server.address().port}); |
| 89 | +})); |
| 90 | +})); |
| 91 | + |
| 92 | +anotherSocket.on('ready',common.mustCall(()=>{ |
| 93 | +// Do another 2 requests with another socket |
| 94 | +// enusre that this will not affect the first socket |
| 95 | +initialRequests(anotherSocket,2,common.mustCall(()=>{ |
| 96 | +letbuffer=''; |
| 97 | + |
| 98 | +// Send the rest of the calls to the first socket |
| 99 | +// and see connection is closed |
| 100 | +socket.on('data',common.mustCall((data)=>{ |
| 101 | +buffer+=data; |
| 102 | + |
| 103 | +if(buffer.endsWith('\r\n\r\n')){ |
| 104 | +const[headers,body]=buffer.trim().split('\r\n\r\n'); |
| 105 | +assertResponse(headers,body,true); |
| 106 | +anotherSocket.end(); |
| 107 | +socket.end(); |
| 108 | +} |
| 109 | +})); |
| 110 | + |
| 111 | +writeRequest(socket,true); |
| 112 | +})); |
| 113 | +})); |
| 114 | + |
| 115 | +socket.connect({port: server.address().port}); |
| 116 | +})); |
0 commit comments