11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst test_res_body = 'other stuff!\n';
81cb0ef41Sopenharmony_ciconst countdown = new Countdown(2, () => server.close());
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst server = http.createServer((req, res) => {
111cb0ef41Sopenharmony_ci  console.error('Server sending informational message #1...');
121cb0ef41Sopenharmony_ci  // These function calls may rewritten as necessary
131cb0ef41Sopenharmony_ci  // to call res.writeHead instead
141cb0ef41Sopenharmony_ci  res._writeRaw('HTTP/1.1 102 Processing\r\n');
151cb0ef41Sopenharmony_ci  res._writeRaw('Foo: Bar\r\n');
161cb0ef41Sopenharmony_ci  res._writeRaw('\r\n', common.mustCall());
171cb0ef41Sopenharmony_ci  console.error('Server sending full response...');
181cb0ef41Sopenharmony_ci  res.writeHead(200, {
191cb0ef41Sopenharmony_ci    'Content-Type': 'text/plain',
201cb0ef41Sopenharmony_ci    'ABCD': '1'
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci  res.end(test_res_body);
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciserver.listen(0, function() {
261cb0ef41Sopenharmony_ci  const req = http.request({
271cb0ef41Sopenharmony_ci    port: this.address().port,
281cb0ef41Sopenharmony_ci    path: '/world'
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci  req.end();
311cb0ef41Sopenharmony_ci  console.error('Client sending request...');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  let body = '';
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  req.on('information', function(res) {
361cb0ef41Sopenharmony_ci    assert.strictEqual(res.httpVersion, '1.1');
371cb0ef41Sopenharmony_ci    assert.strictEqual(res.httpVersionMajor, 1);
381cb0ef41Sopenharmony_ci    assert.strictEqual(res.httpVersionMinor, 1);
391cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusCode, 102,
401cb0ef41Sopenharmony_ci                       `Received ${res.statusCode}, not 102.`);
411cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusMessage, 'Processing',
421cb0ef41Sopenharmony_ci                       `Received ${res.statusMessage}, not "Processing".`);
431cb0ef41Sopenharmony_ci    assert.strictEqual(res.headers.foo, 'Bar');
441cb0ef41Sopenharmony_ci    assert.strictEqual(res.rawHeaders[0], 'Foo');
451cb0ef41Sopenharmony_ci    assert.strictEqual(res.rawHeaders[1], 'Bar');
461cb0ef41Sopenharmony_ci    console.error('Client got 102 Processing...');
471cb0ef41Sopenharmony_ci    countdown.dec();
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  req.on('response', function(res) {
511cb0ef41Sopenharmony_ci    // Check that all 102 Processing received before full response received.
521cb0ef41Sopenharmony_ci    assert.strictEqual(countdown.remaining, 1);
531cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusCode, 200,
541cb0ef41Sopenharmony_ci                       `Final status code was ${res.statusCode}, not 200.`);
551cb0ef41Sopenharmony_ci    res.setEncoding('utf8');
561cb0ef41Sopenharmony_ci    res.on('data', function(chunk) { body += chunk; });
571cb0ef41Sopenharmony_ci    res.on('end', function() {
581cb0ef41Sopenharmony_ci      console.error('Got full response.');
591cb0ef41Sopenharmony_ci      assert.strictEqual(body, test_res_body);
601cb0ef41Sopenharmony_ci      assert.ok('abcd' in res.headers);
611cb0ef41Sopenharmony_ci      countdown.dec();
621cb0ef41Sopenharmony_ci    });
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci});
65