summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-legacy.js
blob: e2fed3135d40f11e68be72646786a493568b2ffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const url = require('url');

let responses_sent = 0;
let body0 = '';
let body1 = '';

const server = http.createServer(function(req, res) {
  if (responses_sent === 0) {
    assert.strictEqual('GET', req.method);
    assert.strictEqual('/hello', url.parse(req.url).pathname);

    console.dir(req.headers);
    assert.strictEqual(true, 'accept' in req.headers);
    assert.strictEqual('*/*', req.headers['accept']);

    assert.strictEqual(true, 'foo' in req.headers);
    assert.strictEqual('bar', req.headers['foo']);
  }

  if (responses_sent === 1) {
    assert.strictEqual('POST', req.method);
    assert.strictEqual('/world', url.parse(req.url).pathname);
    this.close();
  }

  req.on('end', function() {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write('The path was ' + url.parse(req.url).pathname);
    res.end();
    responses_sent += 1;
  });
  req.resume();
});

server.listen(0, common.mustCall(function() {
  const client = http.createClient(this.address().port);
  const req = client.request('/hello', {'Accept': '*/*', 'Foo': 'bar'});
  setTimeout(function() {
    req.end();
  }, 100);
  req.on('response', common.mustCall(function(res) {
    assert.strictEqual(200, res.statusCode);
    res.setEncoding('utf8');
    res.on('data', function(chunk) { body0 += chunk; });
    console.error('Got /hello response');
  }));

  setTimeout(common.mustCall(function() {
    const req = client.request('POST', '/world');
    req.end();
    req.on('response', common.mustCall(function(res) {
      assert.strictEqual(200, res.statusCode);
      res.setEncoding('utf8');
      res.on('data', function(chunk) { body1 += chunk; });
      console.error('Got /world response');
    }));
  }), 1);
}));

process.on('exit', function() {
  console.error('responses_sent: ' + responses_sent);
  assert.strictEqual(2, responses_sent);

  assert.strictEqual('The path was /hello', body0);
  assert.strictEqual('The path was /world', body1);
});