11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  mustCall,
51cb0ef41Sopenharmony_ci  mustNotCall,
61cb0ef41Sopenharmony_ci  hasCrypto,
71cb0ef41Sopenharmony_ci  skip
81cb0ef41Sopenharmony_ci} = require('../common');
91cb0ef41Sopenharmony_ciif (!hasCrypto)
101cb0ef41Sopenharmony_ci  skip('missing crypto');
111cb0ef41Sopenharmony_ciconst { createServer, connect } = require('http2');
121cb0ef41Sopenharmony_ciconst assert = require('assert');
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  const server = createServer();
151cb0ef41Sopenharmony_ci  server.listen(0, mustCall(() => {
161cb0ef41Sopenharmony_ci    const port = server.address().port;
171cb0ef41Sopenharmony_ci    const url = `http://localhost:${port}`;
181cb0ef41Sopenharmony_ci    const client = connect(url, mustCall(() => {
191cb0ef41Sopenharmony_ci      const request = client.request();
201cb0ef41Sopenharmony_ci      request.resume();
211cb0ef41Sopenharmony_ci      request.on('end', mustCall());
221cb0ef41Sopenharmony_ci      request.on('close', mustCall(() => {
231cb0ef41Sopenharmony_ci        client.close();
241cb0ef41Sopenharmony_ci      }));
251cb0ef41Sopenharmony_ci    }));
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    server.once('request', mustCall((request, response) => {
281cb0ef41Sopenharmony_ci      // response.write() returns true
291cb0ef41Sopenharmony_ci      assert(response.write('muahaha', 'utf8', mustCall()));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci      response.stream.close(0, mustCall(() => {
321cb0ef41Sopenharmony_ci        response.on('error', mustNotCall());
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci        // response.write() without cb returns error
351cb0ef41Sopenharmony_ci        response.write('muahaha', mustCall((err) => {
361cb0ef41Sopenharmony_ci          assert.strictEqual(err.code, 'ERR_HTTP2_INVALID_STREAM');
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci          // response.write() with cb returns falsy value
391cb0ef41Sopenharmony_ci          assert(!response.write('muahaha', mustCall()));
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci          client.destroy();
421cb0ef41Sopenharmony_ci          server.close();
431cb0ef41Sopenharmony_ci        }));
441cb0ef41Sopenharmony_ci      }));
451cb0ef41Sopenharmony_ci    }));
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  // Http2ServerResponse.write ERR_STREAM_WRITE_AFTER_END
511cb0ef41Sopenharmony_ci  const server = createServer();
521cb0ef41Sopenharmony_ci  server.listen(0, mustCall(() => {
531cb0ef41Sopenharmony_ci    const port = server.address().port;
541cb0ef41Sopenharmony_ci    const url = `http://localhost:${port}`;
551cb0ef41Sopenharmony_ci    const client = connect(url, mustCall(() => {
561cb0ef41Sopenharmony_ci      const request = client.request();
571cb0ef41Sopenharmony_ci      request.resume();
581cb0ef41Sopenharmony_ci      request.on('end', mustCall());
591cb0ef41Sopenharmony_ci      request.on('close', mustCall(() => {
601cb0ef41Sopenharmony_ci        client.close();
611cb0ef41Sopenharmony_ci      }));
621cb0ef41Sopenharmony_ci    }));
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    server.once('request', mustCall((request, response) => {
651cb0ef41Sopenharmony_ci      response.end();
661cb0ef41Sopenharmony_ci      response.write('asd', mustCall((err) => {
671cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
681cb0ef41Sopenharmony_ci        client.destroy();
691cb0ef41Sopenharmony_ci        server.close();
701cb0ef41Sopenharmony_ci      }));
711cb0ef41Sopenharmony_ci    }));
721cb0ef41Sopenharmony_ci  }));
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  const server = createServer();
771cb0ef41Sopenharmony_ci  server.listen(0, mustCall(() => {
781cb0ef41Sopenharmony_ci    const port = server.address().port;
791cb0ef41Sopenharmony_ci    const url = `http://localhost:${port}`;
801cb0ef41Sopenharmony_ci    const client = connect(url, mustCall(() => {
811cb0ef41Sopenharmony_ci      client.request();
821cb0ef41Sopenharmony_ci    }));
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    server.once('request', mustCall((request, response) => {
851cb0ef41Sopenharmony_ci      response.destroy();
861cb0ef41Sopenharmony_ci      assert.strictEqual(response.write('asd', mustNotCall()), false);
871cb0ef41Sopenharmony_ci      client.destroy();
881cb0ef41Sopenharmony_ci      server.close();
891cb0ef41Sopenharmony_ci    }));
901cb0ef41Sopenharmony_ci  }));
911cb0ef41Sopenharmony_ci}
92