11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci'use strict'; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst common = require('../common'); 61cb0ef41Sopenharmony_ciif (!common.hasCrypto) 71cb0ef41Sopenharmony_ci common.skip('missing crypto'); 81cb0ef41Sopenharmony_ciconst assert = require('assert'); 91cb0ef41Sopenharmony_ciconst h2 = require('http2'); 101cb0ef41Sopenharmony_ciconst net = require('net'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciconst { kTimeout } = require('internal/timers'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// Tests behavior of the proxied socket in Http2ServerRequest 151cb0ef41Sopenharmony_ci// & Http2ServerResponse - this proxy socket should mimic the 161cb0ef41Sopenharmony_ci// behavior of http1 but against the http2 api & model 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciconst errMsg = { 191cb0ef41Sopenharmony_ci code: 'ERR_HTTP2_NO_SOCKET_MANIPULATION', 201cb0ef41Sopenharmony_ci name: 'Error', 211cb0ef41Sopenharmony_ci message: 'HTTP/2 sockets should not be directly manipulated ' + 221cb0ef41Sopenharmony_ci '(e.g. read and written)' 231cb0ef41Sopenharmony_ci}; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciconst server = h2.createServer(); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciserver.on('request', common.mustCall(function(request, response) { 281cb0ef41Sopenharmony_ci assert.ok(request.socket instanceof net.Socket); 291cb0ef41Sopenharmony_ci assert.ok(response.socket instanceof net.Socket); 301cb0ef41Sopenharmony_ci assert.strictEqual(request.socket, response.socket); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci assert.ok(request.socket.readable); 331cb0ef41Sopenharmony_ci request.resume(); 341cb0ef41Sopenharmony_ci assert.ok(request.socket.writable); 351cb0ef41Sopenharmony_ci assert.strictEqual(request.socket.destroyed, false); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci request.socket.setTimeout(987); 381cb0ef41Sopenharmony_ci assert.strictEqual(request.stream.session[kTimeout]._idleTimeout, 987); 391cb0ef41Sopenharmony_ci request.socket.setTimeout(0); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci assert.throws(() => request.socket.read(), errMsg); 421cb0ef41Sopenharmony_ci assert.throws(() => request.socket.write(), errMsg); 431cb0ef41Sopenharmony_ci assert.throws(() => request.socket.pause(), errMsg); 441cb0ef41Sopenharmony_ci assert.throws(() => request.socket.resume(), errMsg); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci // Should have correct this context for socket methods & getters 471cb0ef41Sopenharmony_ci assert.ok(request.socket.address() != null); 481cb0ef41Sopenharmony_ci assert.ok(request.socket.remotePort); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci request.on('end', common.mustCall(() => { 511cb0ef41Sopenharmony_ci assert.strictEqual(request.socket.readable, false); 521cb0ef41Sopenharmony_ci response.socket.destroy(); 531cb0ef41Sopenharmony_ci })); 541cb0ef41Sopenharmony_ci response.on('finish', common.mustCall(() => { 551cb0ef41Sopenharmony_ci assert.ok(request.socket); 561cb0ef41Sopenharmony_ci assert.strictEqual(response.socket, undefined); 571cb0ef41Sopenharmony_ci assert.ok(request.socket.destroyed); 581cb0ef41Sopenharmony_ci assert.strictEqual(request.socket.readable, false); 591cb0ef41Sopenharmony_ci process.nextTick(() => { 601cb0ef41Sopenharmony_ci assert.strictEqual(request.socket.writable, false); 611cb0ef41Sopenharmony_ci server.close(); 621cb0ef41Sopenharmony_ci }); 631cb0ef41Sopenharmony_ci })); 641cb0ef41Sopenharmony_ci 651cb0ef41Sopenharmony_ci // Properties that do not exist on the proxy are retrieved from the socket 661cb0ef41Sopenharmony_ci assert.ok(request.socket._server); 671cb0ef41Sopenharmony_ci assert.strictEqual(request.socket.connecting, false); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci // Socket events are bound and emitted on Http2Stream 701cb0ef41Sopenharmony_ci request.socket.on('close', common.mustCall()); 711cb0ef41Sopenharmony_ci request.socket.once('close', common.mustCall()); 721cb0ef41Sopenharmony_ci request.socket.on('testEvent', common.mustCall()); 731cb0ef41Sopenharmony_ci request.socket.emit('testEvent'); 741cb0ef41Sopenharmony_ci})); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() { 771cb0ef41Sopenharmony_ci const port = server.address().port; 781cb0ef41Sopenharmony_ci const url = `http://localhost:${port}`; 791cb0ef41Sopenharmony_ci const client = h2.connect(url, common.mustCall(() => { 801cb0ef41Sopenharmony_ci const headers = { 811cb0ef41Sopenharmony_ci ':path': '/', 821cb0ef41Sopenharmony_ci ':method': 'GET', 831cb0ef41Sopenharmony_ci ':scheme': 'http', 841cb0ef41Sopenharmony_ci ':authority': `localhost:${port}` 851cb0ef41Sopenharmony_ci }; 861cb0ef41Sopenharmony_ci const request = client.request(headers); 871cb0ef41Sopenharmony_ci request.on('end', common.mustCall(() => { 881cb0ef41Sopenharmony_ci client.close(); 891cb0ef41Sopenharmony_ci })); 901cb0ef41Sopenharmony_ci request.end(); 911cb0ef41Sopenharmony_ci request.resume(); 921cb0ef41Sopenharmony_ci })); 931cb0ef41Sopenharmony_ci})); 94