test: replace deprecated util.debug() calls
[platform/upstream/nodejs.git] / test / parallel / test-http-legacy.js
1 'use strict';
2 var common = require('../common');
3 var assert = require('assert');
4 var http = require('http');
5 var url = require('url');
6
7 function p(x) {
8   common.error(common.inspect(x));
9 }
10
11 var responses_sent = 0;
12 var responses_recvd = 0;
13 var body0 = '';
14 var body1 = '';
15
16 var server = http.createServer(function(req, res) {
17   if (responses_sent == 0) {
18     assert.equal('GET', req.method);
19     assert.equal('/hello', url.parse(req.url).pathname);
20
21     console.dir(req.headers);
22     assert.equal(true, 'accept' in req.headers);
23     assert.equal('*/*', req.headers['accept']);
24
25     assert.equal(true, 'foo' in req.headers);
26     assert.equal('bar', req.headers['foo']);
27   }
28
29   if (responses_sent == 1) {
30     assert.equal('POST', req.method);
31     assert.equal('/world', url.parse(req.url).pathname);
32     this.close();
33   }
34
35   req.on('end', function() {
36     res.writeHead(200, {'Content-Type': 'text/plain'});
37     res.write('The path was ' + url.parse(req.url).pathname);
38     res.end();
39     responses_sent += 1;
40   });
41   req.resume();
42
43   //assert.equal('127.0.0.1', res.connection.remoteAddress);
44 });
45
46 server.listen(common.PORT, function() {
47   var client = http.createClient(common.PORT);
48   var req = client.request('/hello', {'Accept': '*/*', 'Foo': 'bar'});
49   setTimeout(function() {
50     req.end();
51   }, 100);
52   req.on('response', function(res) {
53     assert.equal(200, res.statusCode);
54     responses_recvd += 1;
55     res.setEncoding('utf8');
56     res.on('data', function(chunk) { body0 += chunk; });
57     console.error('Got /hello response');
58   });
59
60   setTimeout(function() {
61     var req = client.request('POST', '/world');
62     req.end();
63     req.on('response', function(res) {
64       assert.equal(200, res.statusCode);
65       responses_recvd += 1;
66       res.setEncoding('utf8');
67       res.on('data', function(chunk) { body1 += chunk; });
68       console.error('Got /world response');
69     });
70   }, 1);
71 });
72
73 process.on('exit', function() {
74   console.error('responses_recvd: ' + responses_recvd);
75   assert.equal(2, responses_recvd);
76
77   console.error('responses_sent: ' + responses_sent);
78   assert.equal(2, responses_sent);
79
80   assert.equal('The path was /hello', body0);
81   assert.equal('The path was /world', body1);
82 });