|
| 1 | +/* eslint-disable node-core/crypto-check */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const common = require('../common'); |
| 6 | +const assert = require('assert'); |
| 7 | +const http = require('http'); |
| 8 | +const net = require('net'); |
| 9 | + |
| 10 | +function runRoundTrip(transport, serverOptions, requestOptions = {}) { |
| 11 | + return new Promise((resolve, reject) => { |
| 12 | + const server = serverOptions === undefined ? |
| 13 | + transport.createServer(onRequest) : |
| 14 | + transport.createServer(serverOptions, onRequest); |
| 15 | + |
| 16 | + function onRequest(req, res) { |
| 17 | + let body = ''; |
| 18 | + req.setEncoding('utf8'); |
| 19 | + req.on('data', (chunk) => body += chunk); |
| 20 | + req.on('end', common.mustCall(() => { |
| 21 | + assert.strictEqual(body, 'ABCD'); |
| 22 | + |
| 23 | + const callbacks = []; |
| 24 | + res.setHeader('Trailer', 'x-test'); |
| 25 | + res.flushHeaders(); |
| 26 | + |
| 27 | + // Exercise an explicit flush while the socket remains corked. |
| 28 | + res.cork(); |
| 29 | + res.cork(); |
| 30 | + res.write('E', common.mustCall(() => callbacks.push('E'))); |
| 31 | + res.write('F', common.mustCall(() => callbacks.push('F'))); |
| 32 | + |
| 33 | + const originalSend = res._send; |
| 34 | + res._send = common.mustCall(function(...args) { |
| 35 | + assert.notStrictEqual(this.socket.writableCorked, 0); |
| 36 | + return originalSend.apply(this, args); |
| 37 | + }, 5); |
| 38 | + try { |
| 39 | + res.uncork(); |
| 40 | + res.uncork(); |
| 41 | + } finally { |
| 42 | + res._send = originalSend; |
| 43 | + } |
| 44 | + |
| 45 | + // end() must flush this buffer before the terminating chunk. |
| 46 | + res.cork(); |
| 47 | + res.cork(); |
| 48 | + res.write('G', common.mustCall(() => callbacks.push('G'))); |
| 49 | + res.addTrailers({ 'x-test': 'yes' }); |
| 50 | + res.once('finish', common.mustCall(() => callbacks.push('finish'))); |
| 51 | + res.end('H', common.mustCall(() => { |
| 52 | + callbacks.push('end'); |
| 53 | + assert.deepStrictEqual(callbacks, ['E', 'F', 'G', 'finish', 'end']); |
| 54 | + })); |
| 55 | + assert.strictEqual(res.writableCorked, 0); |
| 56 | + })); |
| 57 | + } |
| 58 | + |
| 59 | + server.on('error', reject); |
| 60 | + server.listen(0, common.localhostIPv4, common.mustCall(() => { |
| 61 | + const callbacks = []; |
| 62 | + const req = transport.request({ |
| 63 | + host: common.localhostIPv4, |
| 64 | + port: server.address().port, |
| 65 | + method: 'POST', |
| 66 | + ...requestOptions, |
| 67 | + }, common.mustCall((res) => { |
| 68 | + let body = ''; |
| 69 | + res.setEncoding('utf8'); |
| 70 | + res.on('data', (chunk) => body += chunk); |
| 71 | + res.on('end', common.mustCall(() => { |
| 72 | + assert.strictEqual(body, 'EFGH'); |
| 73 | + assert.strictEqual(res.trailers['x-test'], 'yes'); |
| 74 | + server.close(common.mustCall(resolve)); |
| 75 | + })); |
| 76 | + })); |
| 77 | + |
| 78 | + req.on('error', reject); |
| 79 | + req.cork(); |
| 80 | + req.cork(); |
| 81 | + req.write('A', common.mustCall(() => callbacks.push('A'))); |
| 82 | + req.write('B', common.mustCall(() => callbacks.push('B'))); |
| 83 | + req.uncork(); |
| 84 | + req.uncork(); |
| 85 | + req.cork(); |
| 86 | + req.cork(); |
| 87 | + req.write('C', common.mustCall(() => callbacks.push('C'))); |
| 88 | + req.once('finish', common.mustCall(() => callbacks.push('finish'))); |
| 89 | + req.end('D', common.mustCall(() => { |
| 90 | + callbacks.push('end'); |
| 91 | + assert.deepStrictEqual(callbacks, ['A', 'B', 'C', 'finish', 'end']); |
| 92 | + })); |
| 93 | + assert.strictEqual(req.writableCorked, 0); |
| 94 | + })); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +function runPipelined() { |
| 99 | + return new Promise((resolve, reject) => { |
| 100 | + let firstResponse; |
| 101 | + const server = http.createServer(common.mustCall((req, res) => { |
| 102 | + if (req.url === '/first') { |
| 103 | + firstResponse = res; |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + assert.strictEqual(req.url, '/second'); |
| 108 | + assert.strictEqual(res.socket, null); |
| 109 | + res.cork(); |
| 110 | + res.cork(); |
| 111 | + res.write('B'); |
| 112 | + res.write('C'); |
| 113 | + res.end(); |
| 114 | + assert.strictEqual(res.writableCorked, 0); |
| 115 | + firstResponse.end('A'); |
| 116 | + }, 2)); |
| 117 | + |
| 118 | + server.on('error', reject); |
| 119 | + server.listen(0, common.localhostIPv4, common.mustCall(() => { |
| 120 | + const socket = net.createConnection({ |
| 121 | + host: common.localhostIPv4, |
| 122 | + port: server.address().port, |
| 123 | + }); |
| 124 | + let response = ''; |
| 125 | + |
| 126 | + socket.setEncoding('latin1'); |
| 127 | + socket.on('error', reject); |
| 128 | + socket.on('data', (chunk) => response += chunk); |
| 129 | + socket.on('end', common.mustCall(() => { |
| 130 | + assert.match(response, /\r\n\r\nAHTTP\/1\.1 200 OK\r\n/); |
| 131 | + assert.match(response, /\r\n\r\n1\r\nB\r\n1\r\nC\r\n0\r\n\r\n$/); |
| 132 | + server.close(common.mustCall(resolve)); |
| 133 | + })); |
| 134 | + socket.on('connect', common.mustCall(() => { |
| 135 | + socket.end( |
| 136 | + 'GET /first HTTP/1.1\r\nHost: localhost\r\n\r\n' + |
| 137 | + 'GET /second HTTP/1.1\r\nHost: localhost\r\n' + |
| 138 | + 'Connection: close\r\n\r\n', |
| 139 | + ); |
| 140 | + })); |
| 141 | + })); |
| 142 | + }); |
| 143 | +} |
| 144 | + |
| 145 | +function runDrainOnEnd() { |
| 146 | + return new Promise((resolve, reject) => { |
| 147 | + const server = http.createServer(common.mustCall((req, res) => { |
| 148 | + res.cork(); |
| 149 | + assert.strictEqual(res.write('1'.repeat(10)), true); |
| 150 | + assert.strictEqual(res.write('2'.repeat(1000)), false); |
| 151 | + assert.strictEqual(res.writableNeedDrain, true); |
| 152 | + |
| 153 | + res.once('drain', common.mustCall(() => { |
| 154 | + assert.strictEqual(res.finished, true); |
| 155 | + assert.strictEqual(res.writableNeedDrain, false); |
| 156 | + assert.strictEqual(res.writableLength, 0); |
| 157 | + })); |
| 158 | + res.end(); |
| 159 | + })); |
| 160 | + |
| 161 | + server.on('connection', common.mustCall((socket) => { |
| 162 | + socket._writableState.highWaterMark = 1000; |
| 163 | + })); |
| 164 | + server.on('error', reject); |
| 165 | + server.listen(0, common.localhostIPv4, common.mustCall(() => { |
| 166 | + const req = http.get({ |
| 167 | + host: common.localhostIPv4, |
| 168 | + port: server.address().port, |
| 169 | + }, common.mustCall((res) => { |
| 170 | + let body = ''; |
| 171 | + res.setEncoding('utf8'); |
| 172 | + res.on('data', (chunk) => body += chunk); |
| 173 | + res.on('end', common.mustCall(() => { |
| 174 | + assert.strictEqual(body, '1'.repeat(10) + '2'.repeat(1000)); |
| 175 | + server.close(common.mustCall(resolve)); |
| 176 | + })); |
| 177 | + })); |
| 178 | + req.on('error', reject); |
| 179 | + })); |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +async function main() { |
| 184 | + await runRoundTrip(http); |
| 185 | + |
| 186 | + if (common.hasCrypto) { |
| 187 | + const fixtures = require('../common/fixtures'); |
| 188 | + const https = require('https'); |
| 189 | + await runRoundTrip(https, { |
| 190 | + key: fixtures.readKey('agent1-key.pem'), |
| 191 | + cert: fixtures.readKey('agent1-cert.pem'), |
| 192 | + }, { rejectUnauthorized: false }); |
| 193 | + } |
| 194 | + |
| 195 | + await runPipelined(); |
| 196 | + await runDrainOnEnd(); |
| 197 | +} |
| 198 | + |
| 199 | +main().then(common.mustCall()); |
0 commit comments