1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6const assert = require('assert'); 7const h2 = require('http2'); 8 9// Tests behavior of the proxied socket in Http2ServerRequest 10// & Http2ServerResponse - specifically property setters 11 12const errMsg = { 13 code: 'ERR_HTTP2_NO_SOCKET_MANIPULATION', 14 name: 'Error', 15 message: 'HTTP/2 sockets should not be directly manipulated ' + 16 '(e.g. read and written)' 17}; 18 19const server = h2.createServer(); 20 21server.on('request', common.mustCall(function(request, response) { 22 const noop = () => {}; 23 24 assert.strictEqual(request.stream.destroyed, false); 25 request.socket.destroyed = true; 26 assert.strictEqual(request.stream.destroyed, true); 27 request.socket.destroyed = false; 28 29 assert.strictEqual(request.stream.readable, false); 30 request.socket.readable = true; 31 assert.strictEqual(request.stream.readable, true); 32 33 assert.strictEqual(request.stream.writable, true); 34 request.socket.writable = false; 35 assert.strictEqual(request.stream.writable, false); 36 37 const realOn = request.stream.on; 38 request.socket.on = noop; 39 assert.strictEqual(request.stream.on, noop); 40 request.stream.on = realOn; 41 42 const realOnce = request.stream.once; 43 request.socket.once = noop; 44 assert.strictEqual(request.stream.once, noop); 45 request.stream.once = realOnce; 46 47 const realEnd = request.stream.end; 48 request.socket.end = noop; 49 assert.strictEqual(request.stream.end, noop); 50 request.socket.end = common.mustCall(); 51 request.socket.end(); 52 request.stream.end = realEnd; 53 54 const realEmit = request.stream.emit; 55 request.socket.emit = noop; 56 assert.strictEqual(request.stream.emit, noop); 57 request.stream.emit = realEmit; 58 59 const realDestroy = request.stream.destroy; 60 request.socket.destroy = noop; 61 assert.strictEqual(request.stream.destroy, noop); 62 request.stream.destroy = realDestroy; 63 64 request.socket.setTimeout = noop; 65 assert.strictEqual(request.stream.session.setTimeout, noop); 66 67 assert.strictEqual(request.stream.session.socket._isProcessing, undefined); 68 request.socket._isProcessing = true; 69 assert.strictEqual(request.stream.session.socket._isProcessing, true); 70 71 assert.throws(() => request.socket.read = noop, errMsg); 72 assert.throws(() => request.socket.write = noop, errMsg); 73 assert.throws(() => request.socket.pause = noop, errMsg); 74 assert.throws(() => request.socket.resume = noop, errMsg); 75 76 request.stream.on('finish', common.mustCall(() => { 77 setImmediate(() => { 78 request.socket.setTimeout = noop; 79 assert.strictEqual(request.stream.setTimeout, noop); 80 81 assert.strictEqual(request.stream._isProcessing, undefined); 82 request.socket._isProcessing = true; 83 assert.strictEqual(request.stream._isProcessing, true); 84 }); 85 })); 86 response.stream.destroy(); 87})); 88 89server.listen(0, common.mustCall(function() { 90 const port = server.address().port; 91 const url = `http://localhost:${port}`; 92 const client = h2.connect(url, common.mustCall(function() { 93 const headers = { 94 ':path': '/', 95 ':method': 'GET', 96 ':scheme': 'http', 97 ':authority': `localhost:${port}` 98 }; 99 const request = client.request(headers); 100 request.on('end', common.mustCall(() => { 101 client.close(); 102 server.close(); 103 })); 104 request.end(); 105 request.resume(); 106 })); 107})); 108