11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) 51cb0ef41Sopenharmony_ci common.skip('missing crypto'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst h2 = require('http2'); 81cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst body = 111cb0ef41Sopenharmony_ci '<html><head></head><body><h1>this is some data</h2></body></html>'; 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst server = h2.createServer(); 141cb0ef41Sopenharmony_ciconst count = 100; 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci// We use the lower-level API here 171cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall(onStream, count)); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cifunction onStream(stream, headers, flags) { 201cb0ef41Sopenharmony_ci assert.strictEqual(headers[':scheme'], 'http'); 211cb0ef41Sopenharmony_ci assert.ok(headers[':authority']); 221cb0ef41Sopenharmony_ci assert.strictEqual(headers[':method'], 'GET'); 231cb0ef41Sopenharmony_ci assert.strictEqual(flags, 5); 241cb0ef41Sopenharmony_ci stream.respond({ 251cb0ef41Sopenharmony_ci 'content-type': 'text/html', 261cb0ef41Sopenharmony_ci ':status': 200 271cb0ef41Sopenharmony_ci }); 281cb0ef41Sopenharmony_ci stream.write(body.slice(0, 20)); 291cb0ef41Sopenharmony_ci stream.end(body.slice(20)); 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciserver.on('close', common.mustCall()); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciserver.listen(0); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciserver.on('listening', common.mustCall(() => { 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci const client = h2.connect(`http://localhost:${server.address().port}`); 391cb0ef41Sopenharmony_ci client.setMaxListeners(101); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci client.on('goaway', console.log); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci client.on('connect', common.mustCall(() => { 441cb0ef41Sopenharmony_ci assert(!client.encrypted); 451cb0ef41Sopenharmony_ci assert(!client.originSet); 461cb0ef41Sopenharmony_ci assert.strictEqual(client.alpnProtocol, 'h2c'); 471cb0ef41Sopenharmony_ci })); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci const countdown = new Countdown(count, () => { 501cb0ef41Sopenharmony_ci client.close(); 511cb0ef41Sopenharmony_ci server.close(common.mustCall()); 521cb0ef41Sopenharmony_ci }); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci for (let n = 0; n < count; n++) { 551cb0ef41Sopenharmony_ci const req = client.request(); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci req.on('response', common.mustCall(function(headers) { 581cb0ef41Sopenharmony_ci assert.strictEqual(headers[':status'], 200); 591cb0ef41Sopenharmony_ci assert.strictEqual(headers['content-type'], 'text/html'); 601cb0ef41Sopenharmony_ci assert(headers.date); 611cb0ef41Sopenharmony_ci })); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci let data = ''; 641cb0ef41Sopenharmony_ci req.setEncoding('utf8'); 651cb0ef41Sopenharmony_ci req.on('data', (d) => data += d); 661cb0ef41Sopenharmony_ci req.on('end', common.mustCall(() => { 671cb0ef41Sopenharmony_ci assert.strictEqual(body, data); 681cb0ef41Sopenharmony_ci })); 691cb0ef41Sopenharmony_ci req.on('close', common.mustCall(() => countdown.dec())); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci})); 72