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 http2 = require('http2');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst testResBody = 'other stuff!\n';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// Checks the full 100-continue flow from client sending 'expect: 100-continue'
121cb0ef41Sopenharmony_ci// through server receiving it, triggering 'checkContinue' custom handler,
131cb0ef41Sopenharmony_ci// writing the rest of the request to finally the client receiving to.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst server = http2.createServer(
161cb0ef41Sopenharmony_ci  common.mustNotCall('Full request received before 100 Continue')
171cb0ef41Sopenharmony_ci);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciserver.on('checkContinue', common.mustCall((req, res) => {
201cb0ef41Sopenharmony_ci  res.writeContinue();
211cb0ef41Sopenharmony_ci  res.writeHead(200, {});
221cb0ef41Sopenharmony_ci  res.end(testResBody);
231cb0ef41Sopenharmony_ci  // Should simply return false if already too late to write
241cb0ef41Sopenharmony_ci  assert.strictEqual(res.writeContinue(), false);
251cb0ef41Sopenharmony_ci  res.on('finish', common.mustCall(
261cb0ef41Sopenharmony_ci    () => process.nextTick(() => assert.strictEqual(res.writeContinue(), false))
271cb0ef41Sopenharmony_ci  ));
281cb0ef41Sopenharmony_ci}));
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
311cb0ef41Sopenharmony_ci  let body = '';
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
341cb0ef41Sopenharmony_ci  const req = client.request({
351cb0ef41Sopenharmony_ci    ':method': 'POST',
361cb0ef41Sopenharmony_ci    'expect': '100-continue'
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  let gotContinue = false;
401cb0ef41Sopenharmony_ci  req.on('continue', common.mustCall(() => {
411cb0ef41Sopenharmony_ci    gotContinue = true;
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  req.on('response', common.mustCall((headers) => {
451cb0ef41Sopenharmony_ci    assert.strictEqual(gotContinue, true);
461cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':status'], 200);
471cb0ef41Sopenharmony_ci    req.end();
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  req.setEncoding('utf-8');
511cb0ef41Sopenharmony_ci  req.on('data', common.mustCall((chunk) => { body += chunk; }));
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  req.on('end', common.mustCall(() => {
541cb0ef41Sopenharmony_ci    assert.strictEqual(body, testResBody);
551cb0ef41Sopenharmony_ci    client.close();
561cb0ef41Sopenharmony_ci    server.close();
571cb0ef41Sopenharmony_ci  }));
581cb0ef41Sopenharmony_ci}));
59