Skip to content

Commit

Permalink
[test] Got simple tests for complex requests (i.e. requests as object…
Browse files Browse the repository at this point in the history
…s) working
  • Loading branch information
indexzero committed Jul 25, 2010
1 parent dd1b7db commit 40fefc7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/http-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ httpAgent.HttpAgent = function () {
});
};

httpAgent.create = function(host, urls) {
httpAgent.create = function (host, urls) {
return new httpAgent.HttpAgent(host, urls);
};

Expand Down Expand Up @@ -116,15 +116,15 @@ httpAgent.HttpAgent.prototype = {
var request = this._createRequest(client, url);

var self = this;
request.addListener('response', function(response) {
request.addListener('response', function (response) {
response.setEncoding(self.encoding);
self.response = response;

response.addListener("data", function (chunk) {
self.body += chunk;
});

response.addListener("end", function(err, obj) {
response.addListener("end", function (err, obj) {
self._visited.unshift(url);
self.emit('next', null, self);
});
Expand Down
54 changes: 54 additions & 0 deletions test/complex-request-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* complex-request-test.js: Tests for complex requests using HttpAgent
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/

var path = require('path'),
sys = require('sys'),
http = require('http'),
events = require('events'),
assert = require('assert'),
eyes = require('eyes'),
net = require('net'),
vows = require('vows');

require.paths.unshift(path.join(__dirname, '..', 'lib'));

var httpAgent = require('http-agent'),
helpers = require('./helpers');

var complexUrls = [
{
method: 'GET',
url: 'barackobama'
},
{
method: 'GET',
url: 'facebook'
},
{
method: 'GET',
url: 'google'
}
];

vows.describe('httpAgent').addBatch({
"When using an httpAgent": {
"to browse a path of complex urls": {
"the next event": {
topic: function () {
var agent = helpers.createAgent({ urls: complexUrls });
agent.addListener('next', this.callback);
agent.start();
},
"should be raised after start": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
assert.isNotNull(agent.response);
}
}
}
}
}).export(module);
20 changes: 10 additions & 10 deletions test/http-agent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ vows.describe('httpAgent').addBatch({
"When using an httpAgent": {
"to browse a path of urls": {
"the next event": {
topic: function() {
topic: function () {
var agent = helpers.createAgent();
agent.addListener('next', this.callback);
agent.start();
},
"should be raised after start": function(e, agent) {
"should be raised after start": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
assert.isNotNull(agent.response);
}
Expand All @@ -38,7 +38,7 @@ vows.describe('httpAgent').addBatch({
agent.addListener('next', this.callback);
agent.start();
},
"should emit the next event": function(e, agent) {
"should emit the next event": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
}
}
Expand All @@ -49,14 +49,14 @@ vows.describe('httpAgent').addBatch({
"simple usage of": {
"the create() method": {
topic: helpers.createAgent(),
"should return a valid httpAgent": function(agent) {
"should return a valid httpAgent": function (agent) {
assert.instanceOf(agent, httpAgent.HttpAgent)
assert.equal(agent.nextUrls.length, 3);
assert.equal(agent.nextUrls[0], 'graph.facebook.com/barackobama');
assert.equal(agent.prevUrls.length, 0);
assert.equal(agent.host, 'graph.facebook.com');
},
"should return a valid event emitter": function(agent) {
"should return a valid event emitter": function (agent) {
assert.isFunction(agent.addListener);
assert.isFunction(agent.removeListener);
assert.isFunction(agent.removeAllListener);
Expand All @@ -71,7 +71,7 @@ vows.describe('httpAgent').addBatch({
agent.start();
agent.stop();
},
"should emit the stopped event when previously started": function(e, agent) {
"should emit the stopped event when previously started": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
}
},
Expand All @@ -81,7 +81,7 @@ vows.describe('httpAgent').addBatch({
agent.addListener('start', this.callback);
agent.start();
},
"should emit the started event": function(e, agent) {
"should emit the started event": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
}
},
Expand All @@ -91,7 +91,7 @@ vows.describe('httpAgent').addBatch({
agent.addListener('next', this.callback);
agent.start();
},
"should emit the next event": function(e, agent) {
"should emit the next event": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
assert.equal(agent.nextUrls.length, 2);
assert.equal(agent.nextUrls[0], 'graph.facebook.com/facebook');
Expand All @@ -113,7 +113,7 @@ vows.describe('httpAgent').addBatch({
agent.addListener('next', nextCallback);
agent.start();
},
"should emit the next event": function(e, agent) {
"should emit the next event": function (e, agent) {
assert.instanceOf(agent, httpAgent.HttpAgent);
assert.equal(agent.nextUrls.length, 2);
assert.equal(agent.prevUrls.length, 2);
Expand All @@ -127,7 +127,7 @@ vows.describe('httpAgent').addBatch({
"When using an httpAgent": {
"the back() method": {
"when called before start": {
topic: function() {
topic: function () {
var agent = helpers.createAgent();
agent.addListener('next', this.callback);

Expand Down

0 comments on commit 40fefc7

Please sign in to comment.