Permalink
Browse files

test: refactored http test.

Many http tests had used legacy http.Client.
This refactored it to use modern API.
  • Loading branch information...
koichik committed Aug 14, 2011
1 parent 16788f4 commit fcee5d3c659b4e82d2c7597599008fef43fe6069
@@ -43,21 +43,24 @@ chargen.listen(9000, ready);
// Proxy to the chargen server.
var proxy = http.createServer(function(req, res) {
- var c = http.createClient(9000, 'localhost');
-
var len = parseInt(req.headers['x-len'], 10);
assert.ok(len > 0);
var sent = 0;
- c.addListener('error', function(e) {
+ function onError(e) {
console.log('proxy client error. sent ' + sent);
throw e;
- });
+ }
- var proxy_req = c.request(req.method, req.url, req.headers);
- proxy_req.addListener('response', function(proxy_res) {
+ var proxy_req = http.request({
+ host: 'localhost',
+ port: 9000,
+ method: req.method,
+ path: req.url,
+ headers: req.headers
+ }, function(proxy_res) {
res.writeHead(proxy_res.statusCode, proxy_res.headers);
var count = 0;
@@ -74,7 +77,7 @@ var proxy = http.createServer(function(req, res) {
});
});
-
+ proxy_req.on('error', onError);
proxy_req.end();
});
proxy.listen(9001, ready);
@@ -89,9 +92,12 @@ function call_chargen(list) {
var recved = 0;
- var req = http.createClient(9001, 'localhost').request('/', {'x-len': len});
-
- req.addListener('response', function(res) {
+ var req = http.request({
+ port: 9001,
+ host: 'localhost',
+ path: '/',
+ headers: {'x-len': len}
+ }, function(res) {
res.addListener('data', function(d) {
recved += d.length;
@@ -115,7 +121,7 @@ function call_chargen(list) {
}
}
-serversRunning = 0;
+var serversRunning = 0;
function ready() {
if (++serversRunning < 2) return;
call_chargen([100, 1000, 10000, 100000, 1000000]);
@@ -124,3 +130,4 @@ function ready() {
process.addListener('exit', function() {
assert.ok(done);
});
+
@@ -42,9 +42,11 @@ server.listen(common.PORT);
var gotEnd = false;
server.addListener('listening', function() {
- var client = http.createClient(common.PORT);
- var request = client.request('HEAD', '/');
- request.addListener('response', function(response) {
+ var request = http.request({
+ port: common.PORT,
+ method: 'HEAD',
+ path: '/'
+ }, function(response) {
console.log('got response');
response.addListener('data', function() {
process.exit(2);
@@ -60,3 +62,4 @@ server.addListener('listening', function() {
setTimeout(function() {
process.exit(1);
}, 2000);
+
@@ -48,8 +48,11 @@ server.listen(common.PORT, '127.0.0.1', function() {
connections++;
setTimeout(function() {
- var client = http.createClient(common.PORT),
- request = client.request('POST', '/');
+ var request = http.request({
+ port: common.PORT,
+ method: 'POST',
+ path: '/'
+ });
function ping() {
var nextPing = (Math.random() * 900).toFixed();
@@ -38,14 +38,15 @@ var server = http.createServer(function(req, res) {
res.end();
});
-var client = http.createClient(common.PORT);
-
function nextRequest() {
var method = methods.shift();
console.error('writing request: %s', method);
- var request = client.request(method, '/');
- request.on('response', function(response) {
+ var request = http.request({
+ port: common.PORT,
+ method: method,
+ path: '/'
+ }, function(response) {
response.on('end', function() {
if (methods.length == 0) {
console.error('close server');
@@ -62,3 +63,4 @@ function nextRequest() {
}
server.listen(common.PORT, nextRequest);
+
@@ -68,22 +68,25 @@ var gotThanks = false;
web.listen(common.PORT, function() {
console.log('Making request');
- var client = http.createClient(common.PORT);
- var req = client.request('GET', '/', { 'content-length': buffer.length });
- req.end(buffer);
-
- req.on('response', function(res) {
+ var req = http.request({
+ port: common.PORT,
+ method: 'GET',
+ path: '/',
+ headers: { 'content-length': buffer.length }
+ }, function(res) {
console.log('Got response');
res.setEncoding('utf8');
res.on('data', function(string) {
assert.equal('thanks', string);
gotThanks = true;
});
});
+ req.end(buffer);
});
process.on('exit', function() {
assert.equal(bufferSize, measuredSize);
assert.ok(gotThanks);
});
+
@@ -37,10 +37,14 @@ var srv = net.createServer(function(c) {
var parseError = false;
srv.listen(common.PORT, '127.0.0.1', function() {
- var hc = http.createClient(common.PORT, '127.0.0.1');
- hc.request('GET', '/').end();
-
- hc.on('error', function(e) {
+ var req = http.request({
+ host: '127.0.0.1',
+ port: common.PORT,
+ method: 'GET',
+ path: '/'});
+ req.end();
+
+ req.on('error', function(e) {
console.log('got error from client');
srv.close();
assert.ok(e.message.indexOf('Parse Error') >= 0);
@@ -53,4 +57,3 @@ process.addListener('exit', function() {
assert.ok(parseError);
});
-
@@ -39,9 +39,7 @@ var body1 = '';
var body2 = '';
server.addListener('listening', function() {
- var client = http.createClient(common.PORT);
-
- var req1 = client.request('/1');
+ var req1 = http.request({ port: common.PORT, path: '/1' });
req1.end();
req1.addListener('response', function(res1) {
res1.setEncoding('utf8');
@@ -51,7 +49,7 @@ server.addListener('listening', function() {
});
res1.addListener('end', function() {
- var req2 = client.request('/2');
+ var req2 = http.request({ port: common.PORT, path: '/2' });
req2.end();
req2.addListener('response', function(res2) {
res2.setEncoding('utf8');
@@ -66,3 +64,4 @@ process.addListener('exit', function() {
assert.equal(body1_s, body1);
assert.equal(body2_s, body2);
});
+
@@ -46,15 +46,11 @@ var server = http.createServer(function(req, res) {
server.listen(common.PORT);
server.addListener('listening', function() {
- var client = http.createClient(common.PORT);
- var req = client.request('POST', '/');
-
- req.write(new Buffer(N));
- req.end();
-
- common.error('client finished sending request');
-
- req.addListener('response', function(res) {
+ var req = http.request({
+ port: common.PORT,
+ method: 'POST',
+ path: '/'
+ }, function(res) {
res.setEncoding('utf8');
res.addListener('data', function(chunk) {
console.log(chunk);
@@ -64,10 +60,16 @@ server.addListener('listening', function() {
server.close();
});
});
+
+ req.write(new Buffer(N));
+ req.end();
+
+ common.error('client finished sending request');
});
process.addListener('exit', function() {
assert.equal(N, bytesRecieved);
assert.equal(true, server_req_complete);
assert.equal(true, client_res_complete);
});
+
@@ -47,16 +47,11 @@ var server = http.createServer(function(req, res) {
server.listen(common.PORT);
server.addListener('listening', function() {
- var client = http.createClient(common.PORT);
- var req = client.request('POST', '/');
- req.write('1\n');
- req.write('2\n');
- req.write('3\n');
- req.end();
-
- common.error('client finished sending request');
-
- req.addListener('response', function(res) {
+ var req = http.request({
+ port: common.PORT,
+ method: 'POST',
+ path: '/'
+ }, function(res) {
res.setEncoding('utf8');
res.addListener('data', function(chunk) {
console.log(chunk);
@@ -66,10 +61,18 @@ server.addListener('listening', function() {
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() {
assert.equal('1\n2\n3\n', sent_body);
assert.equal(true, server_req_complete);
assert.equal(true, client_res_complete);
});
+
@@ -33,13 +33,11 @@ var s = http.createServer(function(req, res) {
});
s.listen(common.PORT, function() {
- var r = http.createClient(common.PORT);
- var request = r.request('GET', '/');
-
- request.on('response', function(response) {
+ var request = http.request({ port: common.PORT }, function(response) {
console.log('STATUS: ' + response.statusCode);
s.close();
});
request.end();
});
+
@@ -33,8 +33,7 @@ var server = http.createServer(function(req, res) {
server.listen(common.PORT, function() {
var req;
for (var i = 0; i < 4; i += 1) {
- req = http.createClient(common.PORT).request('GET', '/busy/' + i);
- req.end();
+ req = http.get({ port: common.PORT, path: '/busy/' + i });
}
});
@@ -51,9 +51,11 @@ server.listen(common.PORT);
server.addListener('listening', function() {
- var client = http.createClient(common.PORT);
- var req = client.request('POST', '/world', {
- 'Expect': '100-continue'
+ var req = http.request({
+ port: common.PORT,
+ method: 'POST',
+ path: '/world',
+ headers: { 'Expect': '100-continue' }
});
common.debug('Client sending request...');
outstanding_reqs++;
@@ -82,3 +84,4 @@ server.addListener('listening', function() {
});
});
});
+
@@ -37,18 +37,21 @@ var server = http.createServer(function(req, res) {
var gotEnd = false;
server.listen(common.PORT, function() {
- var client = http.createClient(common.PORT);
- var request = client.request('HEAD', '/');
- request.end();
- request.addListener('response', function(response) {
+ var request = http.request({
+ port: common.PORT,
+ method: 'HEAD',
+ path: '/'
+ }, function(response) {
common.error('response start');
response.addListener('end', function() {
common.error('response end');
gotEnd = true;
});
});
+ request.end();
});
process.addListener('exit', function() {
assert.ok(gotEnd);
});
+
@@ -37,19 +37,23 @@ server.listen(common.PORT);
var responseComplete = false;
server.addListener('listening', function() {
- var req = http.createClient(common.PORT).request('HEAD', '/');
- common.error('req');
- req.end();
- req.addListener('response', function(res) {
+ var req = http.request({
+ port: common.PORT,
+ method: 'HEAD',
+ path: '/'
+ }, function(res) {
common.error('response');
res.addListener('end', function() {
common.error('response end');
server.close();
responseComplete = true;
});
});
+ common.error('req');
+ req.end();
});
process.addListener('exit', function() {
assert.ok(responseComplete);
});
+
Oops, something went wrong.

0 comments on commit fcee5d3

Please sign in to comment.