Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
test: refactored http test.
Many http tests had used legacy http.Client.
This refactored it to use modern API.

Fixes #1528.
  • Loading branch information
koichik committed Aug 16, 2011
1 parent c05936c commit 8293bb8
Show file tree
Hide file tree
Showing 26 changed files with 159 additions and 146 deletions.
28 changes: 17 additions & 11 deletions test/disabled/test-http-big-proxy-responses.js
Expand Up @@ -43,21 +43,24 @@ chargen.listen(9000, ready);


// Proxy to the chargen server. // Proxy to the chargen server.
var proxy = http.createServer(function(req, res) { var proxy = http.createServer(function(req, res) {
var c = http.createClient(9000, 'localhost');

var len = parseInt(req.headers['x-len'], 10); var len = parseInt(req.headers['x-len'], 10);
assert.ok(len > 0); assert.ok(len > 0);


var sent = 0; var sent = 0;




c.addListener('error', function(e) { function onError(e) {
console.log('proxy client error. sent ' + sent); console.log('proxy client error. sent ' + sent);
throw e; throw e;
}); }


var proxy_req = c.request(req.method, req.url, req.headers); var proxy_req = http.request({
proxy_req.addListener('response', function(proxy_res) { host: 'localhost',
port: 9000,
method: req.method,
path: req.url,
headers: req.headers
}, function(proxy_res) {
res.writeHead(proxy_res.statusCode, proxy_res.headers); res.writeHead(proxy_res.statusCode, proxy_res.headers);


var count = 0; var count = 0;
Expand All @@ -74,7 +77,7 @@ var proxy = http.createServer(function(req, res) {
}); });


}); });

proxy_req.on('error', onError);
proxy_req.end(); proxy_req.end();
}); });
proxy.listen(9001, ready); proxy.listen(9001, ready);
Expand All @@ -89,9 +92,12 @@ function call_chargen(list) {


var recved = 0; var recved = 0;


var req = http.createClient(9001, 'localhost').request('/', {'x-len': len}); var req = http.request({

port: 9001,
req.addListener('response', function(res) { host: 'localhost',
path: '/',
headers: {'x-len': len}
}, function(res) {


res.addListener('data', function(d) { res.addListener('data', function(d) {
recved += d.length; recved += d.length;
Expand All @@ -115,7 +121,7 @@ function call_chargen(list) {
} }
} }


serversRunning = 0; var serversRunning = 0;
function ready() { function ready() {
if (++serversRunning < 2) return; if (++serversRunning < 2) return;
call_chargen([100, 1000, 10000, 100000, 1000000]); call_chargen([100, 1000, 10000, 100000, 1000000]);
Expand Down
8 changes: 5 additions & 3 deletions test/disabled/test-http-head-request.js
Expand Up @@ -42,9 +42,11 @@ server.listen(common.PORT);
var gotEnd = false; var gotEnd = false;


server.addListener('listening', function() { server.addListener('listening', function() {
var client = http.createClient(common.PORT); var request = http.request({
var request = client.request('HEAD', '/'); port: common.PORT,
request.addListener('response', function(response) { method: 'HEAD',
path: '/'
}, function(response) {
console.log('got response'); console.log('got response');
response.addListener('data', function() { response.addListener('data', function() {
process.exit(2); process.exit(2);
Expand Down
7 changes: 5 additions & 2 deletions test/pummel/test-http-upload-timeout.js
Expand Up @@ -48,8 +48,11 @@ server.listen(common.PORT, '127.0.0.1', function() {
connections++; connections++;


setTimeout(function() { setTimeout(function() {
var client = http.createClient(common.PORT), var request = http.request({
request = client.request('POST', '/'); port: common.PORT,
method: 'POST',
path: '/'
});


function ping() { function ping() {
var nextPing = (Math.random() * 900).toFixed(); var nextPing = (Math.random() * 900).toFixed();
Expand Down
9 changes: 5 additions & 4 deletions test/simple/test-http-allow-req-after-204-res.js
Expand Up @@ -38,14 +38,15 @@ var server = http.createServer(function(req, res) {
res.end(); res.end();
}); });


var client = http.createClient(common.PORT);

function nextRequest() { function nextRequest() {
var method = methods.shift(); var method = methods.shift();
console.error('writing request: %s', method); console.error('writing request: %s', method);


var request = client.request(method, '/'); var request = http.request({
request.on('response', function(response) { port: common.PORT,
method: method,
path: '/'
}, function(response) {
response.on('end', function() { response.on('end', function() {
if (methods.length == 0) { if (methods.length == 0) {
console.error('close server'); console.error('close server');
Expand Down
12 changes: 7 additions & 5 deletions test/simple/test-http-buffer-sanity.js
Expand Up @@ -68,18 +68,20 @@ var gotThanks = false;
web.listen(common.PORT, function() { web.listen(common.PORT, function() {
console.log('Making request'); console.log('Making request');


var client = http.createClient(common.PORT); var req = http.request({
var req = client.request('GET', '/', { 'content-length': buffer.length }); port: common.PORT,
req.end(buffer); method: 'GET',

path: '/',
req.on('response', function(res) { headers: { 'content-length': buffer.length }
}, function(res) {
console.log('Got response'); console.log('Got response');
res.setEncoding('utf8'); res.setEncoding('utf8');
res.on('data', function(string) { res.on('data', function(string) {
assert.equal('thanks', string); assert.equal('thanks', string);
gotThanks = true; gotThanks = true;
}); });
}); });
req.end(buffer);
}); });




Expand Down
12 changes: 8 additions & 4 deletions test/simple/test-http-client-parse-error.js
Expand Up @@ -37,10 +37,14 @@ var srv = net.createServer(function(c) {
var parseError = false; var parseError = false;


srv.listen(common.PORT, '127.0.0.1', function() { srv.listen(common.PORT, '127.0.0.1', function() {
var hc = http.createClient(common.PORT, '127.0.0.1'); var req = http.request({
hc.request('GET', '/').end(); host: '127.0.0.1',

port: common.PORT,
hc.on('error', function(e) { method: 'GET',
path: '/'});
req.end();

req.on('error', function(e) {
console.log('got error from client'); console.log('got error from client');
srv.close(); srv.close();
assert.ok(e.message.indexOf('Parse Error') >= 0); assert.ok(e.message.indexOf('Parse Error') >= 0);
Expand Down
6 changes: 2 additions & 4 deletions test/simple/test-http-client-race.js
Expand Up @@ -39,9 +39,7 @@ var body1 = '';
var body2 = ''; var body2 = '';


server.addListener('listening', function() { server.addListener('listening', function() {
var client = http.createClient(common.PORT); var req1 = http.request({ port: common.PORT, path: '/1' });

var req1 = client.request('/1');
req1.end(); req1.end();
req1.addListener('response', function(res1) { req1.addListener('response', function(res1) {
res1.setEncoding('utf8'); res1.setEncoding('utf8');
Expand All @@ -51,7 +49,7 @@ server.addListener('listening', function() {
}); });


res1.addListener('end', function() { res1.addListener('end', function() {
var req2 = client.request('/2'); var req2 = http.request({ port: common.PORT, path: '/2' });
req2.end(); req2.end();
req2.addListener('response', function(res2) { req2.addListener('response', function(res2) {
res2.setEncoding('utf8'); res2.setEncoding('utf8');
Expand Down
19 changes: 10 additions & 9 deletions test/simple/test-http-client-upload-buf.js
Expand Up @@ -46,15 +46,11 @@ var server = http.createServer(function(req, res) {
server.listen(common.PORT); server.listen(common.PORT);


server.addListener('listening', function() { server.addListener('listening', function() {
var client = http.createClient(common.PORT); var req = http.request({
var req = client.request('POST', '/'); port: common.PORT,

method: 'POST',
req.write(new Buffer(N)); path: '/'
req.end(); }, function(res) {

common.error('client finished sending request');

req.addListener('response', function(res) {
res.setEncoding('utf8'); res.setEncoding('utf8');
res.addListener('data', function(chunk) { res.addListener('data', function(chunk) {
console.log(chunk); console.log(chunk);
Expand All @@ -64,6 +60,11 @@ server.addListener('listening', function() {
server.close(); server.close();
}); });
}); });

req.write(new Buffer(N));
req.end();

common.error('client finished sending request');
}); });


process.addListener('exit', function() { process.addListener('exit', function() {
Expand Down
22 changes: 12 additions & 10 deletions test/simple/test-http-client-upload.js
Expand Up @@ -47,16 +47,11 @@ var server = http.createServer(function(req, res) {
server.listen(common.PORT); server.listen(common.PORT);


server.addListener('listening', function() { server.addListener('listening', function() {
var client = http.createClient(common.PORT); var req = http.request({
var req = client.request('POST', '/'); port: common.PORT,
req.write('1\n'); method: 'POST',
req.write('2\n'); path: '/'
req.write('3\n'); }, function(res) {
req.end();

common.error('client finished sending request');

req.addListener('response', function(res) {
res.setEncoding('utf8'); res.setEncoding('utf8');
res.addListener('data', function(chunk) { res.addListener('data', function(chunk) {
console.log(chunk); console.log(chunk);
Expand All @@ -66,6 +61,13 @@ server.addListener('listening', function() {
server.close(); server.close();
}); });
}); });

req.write('1\n');
req.write('2\n');
req.write('3\n');
req.end();

common.error('client finished sending request');
}); });


process.addListener('exit', function() { process.addListener('exit', function() {
Expand Down
5 changes: 1 addition & 4 deletions test/simple/test-http-contentLength0.js
Expand Up @@ -33,10 +33,7 @@ var s = http.createServer(function(req, res) {
}); });
s.listen(common.PORT, function() { s.listen(common.PORT, function() {


var r = http.createClient(common.PORT); var request = http.request({ port: common.PORT }, function(response) {
var request = r.request('GET', '/');

request.on('response', function(response) {
console.log('STATUS: ' + response.statusCode); console.log('STATUS: ' + response.statusCode);
s.close(); s.close();
}); });
Expand Down
3 changes: 1 addition & 2 deletions test/simple/test-http-exceptions.js
Expand Up @@ -33,8 +33,7 @@ var server = http.createServer(function(req, res) {
server.listen(common.PORT, function() { server.listen(common.PORT, function() {
var req; var req;
for (var i = 0; i < 4; i += 1) { for (var i = 0; i < 4; i += 1) {
req = http.createClient(common.PORT).request('GET', '/busy/' + i); req = http.get({ port: common.PORT, path: '/busy/' + i });
req.end();
} }
}); });


Expand Down
8 changes: 5 additions & 3 deletions test/simple/test-http-expect-continue.js
Expand Up @@ -51,9 +51,11 @@ server.listen(common.PORT);




server.addListener('listening', function() { server.addListener('listening', function() {
var client = http.createClient(common.PORT); var req = http.request({
var req = client.request('POST', '/world', { port: common.PORT,
'Expect': '100-continue' method: 'POST',
path: '/world',
headers: { 'Expect': '100-continue' }
}); });
common.debug('Client sending request...'); common.debug('Client sending request...');
outstanding_reqs++; outstanding_reqs++;
Expand Down
10 changes: 6 additions & 4 deletions test/simple/test-http-head-request.js
Expand Up @@ -37,16 +37,18 @@ var server = http.createServer(function(req, res) {
var gotEnd = false; var gotEnd = false;


server.listen(common.PORT, function() { server.listen(common.PORT, function() {
var client = http.createClient(common.PORT); var request = http.request({
var request = client.request('HEAD', '/'); port: common.PORT,
request.end(); method: 'HEAD',
request.addListener('response', function(response) { path: '/'
}, function(response) {
common.error('response start'); common.error('response start');
response.addListener('end', function() { response.addListener('end', function() {
common.error('response end'); common.error('response end');
gotEnd = true; gotEnd = true;
}); });
}); });
request.end();
}); });


process.addListener('exit', function() { process.addListener('exit', function() {
Expand Down
11 changes: 7 additions & 4 deletions test/simple/test-http-head-response-has-no-body-end.js
Expand Up @@ -37,17 +37,20 @@ server.listen(common.PORT);
var responseComplete = false; var responseComplete = false;


server.addListener('listening', function() { server.addListener('listening', function() {
var req = http.createClient(common.PORT).request('HEAD', '/'); var req = http.request({
common.error('req'); port: common.PORT,
req.end(); method: 'HEAD',
req.addListener('response', function(res) { path: '/'
}, function(res) {
common.error('response'); common.error('response');
res.addListener('end', function() { res.addListener('end', function() {
common.error('response end'); common.error('response end');
server.close(); server.close();
responseComplete = true; responseComplete = true;
}); });
}); });
common.error('req');
req.end();
}); });


process.addListener('exit', function() { process.addListener('exit', function() {
Expand Down
11 changes: 7 additions & 4 deletions test/simple/test-http-head-response-has-no-body.js
Expand Up @@ -37,17 +37,20 @@ server.listen(common.PORT);
var responseComplete = false; var responseComplete = false;


server.addListener('listening', function() { server.addListener('listening', function() {
var req = http.createClient(common.PORT).request('HEAD', '/'); var req = http.request({
common.error('req'); port: common.PORT,
req.end(); method: 'HEAD',
req.addListener('response', function(res) { path: '/'
}, function(res) {
common.error('response'); common.error('response');
res.addListener('end', function() { res.addListener('end', function() {
common.error('response end'); common.error('response end');
server.close(); server.close();
responseComplete = true; responseComplete = true;
}); });
}); });
common.error('req');
req.end();
}); });


process.addListener('exit', function() { process.addListener('exit', function() {
Expand Down
18 changes: 9 additions & 9 deletions test/simple/test-http-proxy.js
Expand Up @@ -43,12 +43,12 @@ var backend = http.createServer(function(req, res) {
res.end(); res.end();
}); });


var proxy_client = http.createClient(BACKEND_PORT);
var proxy = http.createServer(function(req, res) { var proxy = http.createServer(function(req, res) {
common.debug('proxy req headers: ' + JSON.stringify(req.headers)); common.debug('proxy req headers: ' + JSON.stringify(req.headers));
var proxy_req = proxy_client.request(url.parse(req.url).pathname); var proxy_req = http.get({
proxy_req.end(); port: BACKEND_PORT,
proxy_req.addListener('response', function(proxy_res) { path: url.parse(req.url).pathname
}, function(proxy_res) {


common.debug('proxy res headers: ' + JSON.stringify(proxy_res.headers)); common.debug('proxy res headers: ' + JSON.stringify(proxy_res.headers));


Expand Down Expand Up @@ -76,10 +76,10 @@ function startReq() {
nlistening++; nlistening++;
if (nlistening < 2) return; if (nlistening < 2) return;


var client = http.createClient(PROXY_PORT); var client = http.get({
var req = client.request('/test'); port: PROXY_PORT,
common.debug('client req'); path: '/test'
req.addListener('response', function(res) { }, function(res) {
common.debug('got res'); common.debug('got res');
assert.equal(200, res.statusCode); assert.equal(200, res.statusCode);


Expand All @@ -95,7 +95,7 @@ function startReq() {
common.debug('closed both'); common.debug('closed both');
}); });
}); });
req.end(); common.debug('client req');
} }


common.debug('listen proxy'); common.debug('listen proxy');
Expand Down

0 comments on commit 8293bb8

Please sign in to comment.