Skip to content

Commit

Permalink
decided to use vows for request tests (echo server needs to be run be…
Browse files Browse the repository at this point in the history
…fore testing still)
  • Loading branch information
icodeforlove committed Feb 6, 2013
1 parent e84457c commit a9e0e9e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 91 deletions.
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "requester",
"version": "0.1.11",
"version": "0.1.12",
"description": "swiss army knife for requests",
"main": "index.js",
"directories": {
Expand All @@ -21,7 +21,8 @@
"author": "Chad Scira",
"license": "MIT",
"devDependencies": {
"async": "0.1.22"
"async": "0.1.22",
"vows": "1.2.7"
},
"dependencies": {
"async": "0.1.22",
Expand Down
27 changes: 27 additions & 0 deletions test/echo-server.js
@@ -0,0 +1,27 @@
/*jshint node:true, strict:false*/
var colors = require('colors'),
http = require('http'),
port = 1338,
host = '127.0.0.1';

http
.createServer(function (request, response) {
var data = '';

request.setEncoding('utf8');
request.on('data', function(chunk) {
data += chunk;
});

request.on('end', function() {
response.end(JSON.stringify({
headers: request.headers,
url: request.url,
method: request.method,
body: data
}));
});
})
.listen(port, host, function () {
//process.send({port: port, host: host});
});
92 changes: 92 additions & 0 deletions test/request-test.js
@@ -0,0 +1,92 @@
var vows = require('vows'),
assert = require('assert'),
Requester = require('requester'),
requester = new Requester({
headers: {'content-type': 'custom-default-content-type'},
debug: 0
}),
basePath = 'http://127.0.0.1:1338';

var request = function () {
var params = Array.prototype.slice.call(arguments),
method = params.shift();

return function () {
var self = this,
callback = function (details) {
self.callback(null, {request: this, details: JSON.parse(details)});
};

params.push(callback);

requester[method].apply(requester, params);
};
};

exports.getRequests = vows.describe('Get Requests').addBatch({
'Standard Request': {
topic: request('get', basePath + '?something=something'),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.equal(JSON.stringify(topic.details), '{"headers":{"content-type":"custom-default-content-type","host":"127.0.0.1:1338","connection":"keep-alive"},"url":"/?something=something","method":"GET","body":""}');
}
},

'Mixed Request': {
topic: request('get', basePath + '?something=something', {data: {somethingElse: 'somethingElse'}}),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.equal(JSON.stringify(topic.details), '{"headers":{"content-type":"custom-default-content-type","host":"127.0.0.1:1338","connection":"keep-alive"},"url":"/?something=something&somethingElse=somethingElse","method":"GET","body":""}');
}
}
});

exports.postRequests = vows.describe('Post Requests').addBatch({
'Standard Request': {
topic: request('post', basePath, {data: {something: 'something'}}),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.equal(JSON.stringify(topic.details), '{"headers":{"content-type":"custom-default-content-type","content-length":"19","host":"127.0.0.1:1338","connection":"keep-alive"},"url":"/","method":"POST","body":"something=something"}');
}
},
'Mixed Request': {
topic: request('post', basePath + '?something=something', {data: {something: 'something'}}),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.equal(JSON.stringify(topic.details), '{"headers":{"content-type":"custom-default-content-type","content-length":"19","host":"127.0.0.1:1338","connection":"keep-alive"},"url":"/?something=something","method":"POST","body":"something=something"}');
}
},
'Headers': {
topic: request('post', basePath, {headers: {'user-agent': 'something'}}),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.equal(topic.details.headers['user-agent'], 'something');
}
},

'Custom Content': {
topic: request('post', basePath, {data: {something: 'something'}, headers: {'content-type': 'something'}}),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.equal(topic.details.body, 'something=something');
assert.equal(topic.details.headers['content-type'], 'something');
}
}
});

exports.multipartRequests = vows.describe('Multipart Requests').addBatch({
'Standard Request': {
topic: request('multipart', basePath, {data: {something: 'something'}}),

'is response correct': function (topic) {
assert.equal(topic.request.statusCode, 200);
assert.match(topic.details.body, /name="something"/);
}
}
});
26 changes: 0 additions & 26 deletions tests/echo-server.js

This file was deleted.

63 changes: 0 additions & 63 deletions tests/request-types.js

This file was deleted.

0 comments on commit a9e0e9e

Please sign in to comment.