Skip to content

Commit

Permalink
[api test] Got http-agent responding to post requests
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 25, 2010
1 parent 40fefc7 commit 7eff8aa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
42 changes: 36 additions & 6 deletions lib/http-agent.js
Expand Up @@ -113,8 +113,17 @@ httpAgent.HttpAgent.prototype = {

_makeRequest: function (url) {
var client = http.createClient(80, this.host);
var request = this._createRequest(client, url);


// Try to create the request or dispatch the error
try {
var request = this._createRequest(client, url);
}
catch (e) {
this.emit('next', e, self);
this.emit('stop', e, self);
return;
}

var self = this;
request.addListener('response', function (response) {
response.setEncoding(self.encoding);
Expand Down Expand Up @@ -144,15 +153,36 @@ httpAgent.HttpAgent.prototype = {
},

_createComplexRequest: function (client, options) {
// TODO: Add post data to request, and other
// options in the request options hash
return client.request(options.method, "/" + options.url, { "host": this.host });
// Check if we're doing a POST or a PUT and have data to send
var sendData = options.method.match(/^P/) && options.body && options.body.length > 0;

options.headers = options.header || {};
options.headers.Host = this.host;

if(sendData) {
options.headers['Content-Length'] = options.body.length;
}

var request = client.request(options.method, "/" + options.url, options.headers);
if(sendData) {
request.write(options.body, 'utf-8');
}

return request;
},

_delegate: function (method, property) {
var self = this;
this[method] = function () {
return self[property][method].apply(self[property], arguments);
};
}
},

/*_extend: function (target, source) {
var prop;
Object.keys(source).forEach(function(prop) {
target[prop] = source[prop];
})
return target;
}*/
};
11 changes: 11 additions & 0 deletions test/complex-request-test.js
Expand Up @@ -37,6 +37,16 @@ var complexUrls = [

vows.describe('httpAgent').addBatch({
"When using an httpAgent": {
"to browse an undefined url": {
topic: function () {
var agent = helpers.createAgent({ urls: [undefined] });
agent.addListener('next', this.callback);
agent.start();
},
"should throw an error": function (err, agent) {
assert.isNotNull(err);
}
},
"to browse a path of complex urls": {
"the next event": {
topic: function () {
Expand All @@ -45,6 +55,7 @@ vows.describe('httpAgent').addBatch({
agent.start();
},
"should be raised after start": function (e, agent) {
eyes.inspect(e);
assert.instanceOf(agent, httpAgent.HttpAgent);
assert.isNotNull(agent.response);
}
Expand Down
25 changes: 25 additions & 0 deletions test/functional-request-test.js
@@ -0,0 +1,25 @@
/*
* functional-request-test.js: Tests for functional requests (i.e. requests that generate
* their own ClientRequest) 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');

vows.describe('httpAgent').addBatch({

}).export(module);

0 comments on commit 7eff8aa

Please sign in to comment.