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, true); 30 request.socket.readable = false; 31 assert.strictEqual(request.stream.readable, false); 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 response.stream.destroy(); 77})); 78 79server.listen(0, common.mustCall(function() { 80 const port = server.address().port; 81 const url = `http://localhost:${port}`; 82 const client = h2.connect(url, common.mustCall(function() { 83 const headers = { 84 ':path': '/', 85 ':method': 'GET', 86 ':scheme': 'http', 87 ':authority': `localhost:${port}` 88 }; 89 const request = client.request(headers); 90 request.on('end', common.mustCall(() => { 91 client.close(); 92 server.close(); 93 })); 94 request.end(); 95 request.resume(); 96 })); 97})); 98