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