11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst http = require('http');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cilet onPause = null;
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst server = http.createServer((req, res) => {
81cb0ef41Sopenharmony_ci  if (req.method === 'GET')
91cb0ef41Sopenharmony_ci    return res.end();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci  res.writeHead(200);
121cb0ef41Sopenharmony_ci  res.flushHeaders();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => {
151cb0ef41Sopenharmony_ci    req.on('end', common.mustNotCall());
161cb0ef41Sopenharmony_ci  }));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  req.connection.on('pause', () => {
191cb0ef41Sopenharmony_ci    res.end();
201cb0ef41Sopenharmony_ci    onPause();
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci}).listen(0, common.mustCall(() => {
231cb0ef41Sopenharmony_ci  const agent = new http.Agent({
241cb0ef41Sopenharmony_ci    maxSockets: 1,
251cb0ef41Sopenharmony_ci    keepAlive: true
261cb0ef41Sopenharmony_ci  });
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  const port = server.address().port;
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  const post = http.request({
311cb0ef41Sopenharmony_ci    agent,
321cb0ef41Sopenharmony_ci    method: 'POST',
331cb0ef41Sopenharmony_ci    port,
341cb0ef41Sopenharmony_ci  }, common.mustCall((res) => {
351cb0ef41Sopenharmony_ci    res.resume();
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    post.write(Buffer.alloc(16 * 1024).fill('X'));
381cb0ef41Sopenharmony_ci    onPause = () => {
391cb0ef41Sopenharmony_ci      post.end('something');
401cb0ef41Sopenharmony_ci    };
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  // What happens here is that the server `end`s the response before we send
441cb0ef41Sopenharmony_ci  // `something`, and the client thought that this is a green light for sending
451cb0ef41Sopenharmony_ci  // next GET request
461cb0ef41Sopenharmony_ci  post.write('initial');
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  http.request({
491cb0ef41Sopenharmony_ci    agent,
501cb0ef41Sopenharmony_ci    method: 'GET',
511cb0ef41Sopenharmony_ci    port,
521cb0ef41Sopenharmony_ci  }, common.mustCall((res) => {
531cb0ef41Sopenharmony_ci    server.close();
541cb0ef41Sopenharmony_ci    res.connection.end();
551cb0ef41Sopenharmony_ci  })).end();
561cb0ef41Sopenharmony_ci}));
57