blob: 9ba13ddf96b6ecfe8eb5c85eb6885de20b852fd1 [file] [log] [blame]
Roman Reissf29762f2015-05-19 11:00:061'use strict';
Ryan Dahla0159b42010-12-04 23:20:342var common = require('../common');
3var assert = require('assert');
4var net = require('net');
5var http = require('http');
Oleg Efimov093dfaf2010-12-05 22:33:526var url = require('url');
7var qs = require('querystring');
Ryan536ecea2009-05-19 12:49:288
Ryan8b49cef2009-06-08 17:10:239var request_number = 0;
10var requests_sent = 0;
Oleg Efimov093dfaf2010-12-05 22:33:5211var server_response = '';
Ryan8b49cef2009-06-08 17:10:2312var client_got_eof = false;
Ryan536ecea2009-05-19 12:49:2813
Oleg Efimov093dfaf2010-12-05 22:33:5214var server = http.createServer(function(req, res) {
Ryan31265be2009-08-26 16:22:0015 res.id = request_number;
16 req.id = request_number++;
Ryan536ecea2009-05-19 12:49:2817
Ryan31265be2009-08-26 16:22:0018 if (req.id == 0) {
Oleg Efimov093dfaf2010-12-05 22:33:5219 assert.equal('GET', req.method);
20 assert.equal('/hello', url.parse(req.url).pathname);
21 assert.equal('world', qs.parse(url.parse(req.url).query).hello);
22 assert.equal('b==ar', qs.parse(url.parse(req.url).query).foo);
Ryan31265be2009-08-26 16:22:0023 }
Ryan536ecea2009-05-19 12:49:2824
Ryan31265be2009-08-26 16:22:0025 if (req.id == 1) {
Oleg Efimov093dfaf2010-12-05 22:33:5226 common.error('req 1');
27 assert.equal('POST', req.method);
28 assert.equal('/quit', url.parse(req.url).pathname);
Michaeljohn Clement485823f2009-12-02 05:38:4229 }
30
31 if (req.id == 2) {
Oleg Efimov093dfaf2010-12-05 22:33:5232 common.error('req 2');
33 assert.equal('foo', req.headers['x-x']);
Michaeljohn Clement485823f2009-12-02 05:38:4234 }
35
36 if (req.id == 3) {
Oleg Efimov093dfaf2010-12-05 22:33:5237 common.error('req 3');
38 assert.equal('bar', req.headers['x-x']);
Ryan31265be2009-08-26 16:22:0039 this.close();
Oleg Efimov093dfaf2010-12-05 22:33:5240 common.error('server closed');
Ryan31265be2009-08-26 16:22:0041 }
Ryan536ecea2009-05-19 12:49:2842
Oleg Efimov093dfaf2010-12-05 22:33:5243 setTimeout(function() {
44 res.writeHead(200, {'Content-Type': 'text/plain'});
Ryan Dahlae85d9a2010-02-17 06:16:2945 res.write(url.parse(req.url).pathname);
Ryan Dahl50c70ac2010-04-08 17:44:2246 res.end();
Ryan31265be2009-08-26 16:22:0047 }, 1);
Ryan536ecea2009-05-19 12:49:2848
Ryan31265be2009-08-26 16:22:0049});
Bert Belder7059be12010-08-11 23:38:4250server.listen(common.PORT);
Ryan8047b912009-06-30 11:56:5251
Ryan Dahl4733d0b2011-02-04 23:14:5852server.httpAllowHalfOpen = true;
53
Ben Noordhuis018e1102011-10-14 23:08:3654server.on('listening', function() {
Bert Belder7059be12010-08-11 23:38:4255 var c = net.createConnection(common.PORT);
Ryan31265be2009-08-26 16:22:0056
Oleg Efimov093dfaf2010-12-05 22:33:5257 c.setEncoding('utf8');
Bert Belder7059be12010-08-11 23:38:4258
Ben Noordhuis018e1102011-10-14 23:08:3659 c.on('connect', function() {
Oleg Efimov093dfaf2010-12-05 22:33:5260 c.write('GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n');
Ryan8b49cef2009-06-08 17:10:2361 requests_sent += 1;
Bert Belder7059be12010-08-11 23:38:4262 });
Michaeljohn Clement485823f2009-12-02 05:38:4263
Ben Noordhuis018e1102011-10-14 23:08:3664 c.on('data', function(chunk) {
Bert Belder7059be12010-08-11 23:38:4265 server_response += chunk;
Michaeljohn Clement485823f2009-12-02 05:38:4266
Bert Belder7059be12010-08-11 23:38:4267 if (requests_sent == 1) {
Oleg Efimov093dfaf2010-12-05 22:33:5268 c.write('POST /quit HTTP/1.1\r\n\r\n');
Bert Belder7059be12010-08-11 23:38:4269 requests_sent += 1;
70 }
Ryan536ecea2009-05-19 12:49:2871
Bert Belder7059be12010-08-11 23:38:4272 if (requests_sent == 2) {
Oleg Efimov093dfaf2010-12-05 22:33:5273 c.write('GET / HTTP/1.1\r\nX-X: foo\r\n\r\n' +
74 'GET / HTTP/1.1\r\nX-X: bar\r\n\r\n');
Ryan Dahl4733d0b2011-02-04 23:14:5875 // Note: we are making the connection half-closed here
76 // before we've gotten the response from the server. This
77 // is a pretty bad thing to do and not really supported
78 // by many http servers. Node supports it optionally if
79 // you set server.httpAllowHalfOpen=true, which we've done
80 // above.
Bert Belder7059be12010-08-11 23:38:4281 c.end();
Oleg Efimov093dfaf2010-12-05 22:33:5282 assert.equal(c.readyState, 'readOnly');
Bert Belder7059be12010-08-11 23:38:4283 requests_sent += 2;
84 }
Ryan536ecea2009-05-19 12:49:2885
Bert Belder7059be12010-08-11 23:38:4286 });
87
Ben Noordhuis018e1102011-10-14 23:08:3688 c.on('end', function() {
Bert Belder7059be12010-08-11 23:38:4289 client_got_eof = true;
90 });
91
Ben Noordhuis018e1102011-10-14 23:08:3692 c.on('close', function() {
Oleg Efimov093dfaf2010-12-05 22:33:5293 assert.equal(c.readyState, 'closed');
Bert Belder7059be12010-08-11 23:38:4294 });
Ryan31265be2009-08-26 16:22:0095});
Ryan8b49cef2009-06-08 17:10:2396
Ben Noordhuis018e1102011-10-14 23:08:3697process.on('exit', function() {
Felix Geisendörfer530328f2009-11-28 17:26:5998 assert.equal(4, request_number);
99 assert.equal(4, requests_sent);
Ryan8b49cef2009-06-08 17:10:23100
Oleg Efimov093dfaf2010-12-05 22:33:52101 var hello = new RegExp('/hello');
Felix Geisendörfer530328f2009-11-28 17:26:59102 assert.equal(true, hello.exec(server_response) != null);
Ryan8b49cef2009-06-08 17:10:23103
Oleg Efimov093dfaf2010-12-05 22:33:52104 var quit = new RegExp('/quit');
Felix Geisendörfer530328f2009-11-28 17:26:59105 assert.equal(true, quit.exec(server_response) != null);
Ryan8b49cef2009-06-08 17:10:23106
Felix Geisendörfer530328f2009-11-28 17:26:59107 assert.equal(true, client_got_eof);
Ryan723c7d92009-08-26 16:51:04108});