11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciif (!common.hasCrypto) 71cb0ef41Sopenharmony_ci common.skip('missing crypto'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ciconst http2 = require('http2'); 111cb0ef41Sopenharmony_ciconst fs = require('fs'); 121cb0ef41Sopenharmony_ciconst tls = require('tls'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst ajs_data = fixtures.readSync('a.js', 'utf8'); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciconst { 171cb0ef41Sopenharmony_ci HTTP2_HEADER_PATH, 181cb0ef41Sopenharmony_ci HTTP2_HEADER_STATUS 191cb0ef41Sopenharmony_ci} = http2.constants; 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ciconst key = fixtures.readKey('agent8-key.pem', 'binary'); 221cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('agent8-cert.pem', 'binary'); 231cb0ef41Sopenharmony_ciconst ca = fixtures.readKey('fake-startcom-root-cert.pem', 'binary'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciconst server = http2.createSecureServer({ key, cert }); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => { 281cb0ef41Sopenharmony_ci const name = headers[HTTP2_HEADER_PATH].slice(1); 291cb0ef41Sopenharmony_ci const file = fixtures.path(name); 301cb0ef41Sopenharmony_ci fs.stat(file, (err, stat) => { 311cb0ef41Sopenharmony_ci if (err != null || stat.isDirectory()) { 321cb0ef41Sopenharmony_ci stream.respond({ [HTTP2_HEADER_STATUS]: 404 }); 331cb0ef41Sopenharmony_ci stream.end(); 341cb0ef41Sopenharmony_ci } else { 351cb0ef41Sopenharmony_ci stream.respond({ [HTTP2_HEADER_STATUS]: 200 }); 361cb0ef41Sopenharmony_ci const str = fs.createReadStream(file); 371cb0ef41Sopenharmony_ci str.pipe(stream); 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci}); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciserver.listen(0, () => { 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci const secureContext = tls.createSecureContext({ ca }); 451cb0ef41Sopenharmony_ci const client = http2.connect(`https://localhost:${server.address().port}`, 461cb0ef41Sopenharmony_ci { secureContext }); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci let remaining = 2; 491cb0ef41Sopenharmony_ci function maybeClose() { 501cb0ef41Sopenharmony_ci if (--remaining === 0) { 511cb0ef41Sopenharmony_ci client.close(); 521cb0ef41Sopenharmony_ci server.close(); 531cb0ef41Sopenharmony_ci } 541cb0ef41Sopenharmony_ci } 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci // Request for a file that does exist, response is 200 571cb0ef41Sopenharmony_ci const req1 = client.request({ [HTTP2_HEADER_PATH]: '/a.js' }, 581cb0ef41Sopenharmony_ci { endStream: true }); 591cb0ef41Sopenharmony_ci req1.on('response', common.mustCall((headers) => { 601cb0ef41Sopenharmony_ci assert.strictEqual(headers[HTTP2_HEADER_STATUS], 200); 611cb0ef41Sopenharmony_ci })); 621cb0ef41Sopenharmony_ci let req1_data = ''; 631cb0ef41Sopenharmony_ci req1.setEncoding('utf8'); 641cb0ef41Sopenharmony_ci req1.on('data', (chunk) => req1_data += chunk); 651cb0ef41Sopenharmony_ci req1.on('end', common.mustCall(() => { 661cb0ef41Sopenharmony_ci assert.strictEqual(req1_data, ajs_data); 671cb0ef41Sopenharmony_ci maybeClose(); 681cb0ef41Sopenharmony_ci })); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci // Request for a file that does not exist, response is 404 711cb0ef41Sopenharmony_ci const req2 = client.request({ [HTTP2_HEADER_PATH]: '/does_not_exist' }, 721cb0ef41Sopenharmony_ci { endStream: true }); 731cb0ef41Sopenharmony_ci req2.on('response', common.mustCall((headers) => { 741cb0ef41Sopenharmony_ci assert.strictEqual(headers[HTTP2_HEADER_STATUS], 404); 751cb0ef41Sopenharmony_ci })); 761cb0ef41Sopenharmony_ci req2.on('data', common.mustNotCall()); 771cb0ef41Sopenharmony_ci req2.on('end', common.mustCall(() => maybeClose())); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci}); 80