Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

decided to use vows for request tests (echo server needs to be run be…

…fore testing still)
  • Loading branch information...
commit a9e0e9e578385ffb24f803faebe73b0b4498b59a 1 parent e84457c
@icodeforlove authored
View
5 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": {
@@ -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",
View
27 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});
+ });
View
92 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"/);
+ }
+ }
+});
View
26 tests/echo-server.js
@@ -1,26 +0,0 @@
-/*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() {
- console.log(JSON.stringify({
- headers: request.headers,
- url: request.url,
- method: request.method,
- body: data
- }, null, '\t').grey);
- response.end('{status: "OK"}');
- });
-}).listen(port, host);
-
-console.log(('echo server started at ' + host + ':' + port).cyan);
View
63 tests/request-types.js
@@ -1,63 +0,0 @@
-/*jshint node:true, strict:false*/
-var async = require('async'),
- echoServer = require('child_process').fork('echo-server'),
- Requester = require('../lib/requester'),
- colors = require('colors');
-
-var requester = new Requester({
- headers: {'content-type': 'custom-default-content-type'}
-});
-
-function testRequest (name, method, args, callback) {
- console.log('\n' + name.yellow, method, JSON.stringify(args));
- args.push(function () {
- callback();
- });
- requester[method].apply(requester, args);
-}
-
-function testGet (callback) {
- testRequest('testGet', 'get', ['http://localhost:1338?something=something'], callback);
-}
-
-function testGetMixed (callback) {
- testRequest('testGetMixed', 'get', ['http://localhost:1338?something=something', {data: {somethingElse: 'somethingElse'}}], callback);
-}
-
-function testPost (callback) {
- testRequest('testPost', 'post', ['http://localhost:1338', {data: {something: 'something'}}], callback);
-}
-
-function testPostMixed (callback) {
- testRequest('testMixedPost', 'post', ['http://localhost:1338?something=something', {data: {somethingElse: 'somethingElse'}}], callback);
-}
-
-function testPostHeaders (callback) {
- testRequest('testPostHeaders', 'post', ['http://localhost:1338', {headers: {'user-agent': 'something'}}], callback);
-}
-
-function testPostCustomContent (callback) {
- testRequest('testPostCustomContent', 'post', ['http://localhost:1338', {data: {something: 'something'}, headers: {'content-type': 'something'}}], callback);
-}
-
-function testMultipart (callback) {
- testRequest('testMultipart', 'multipart', ['http://localhost:1338', {data: {something: 'something'}}], callback);
-}
-
-
-setTimeout(function () {
- async.series(
- [
- testGet,
- testGetMixed,
- testPost,
- testPostMixed,
- testPostHeaders,
- testPostCustomContent,
- testMultipart
- ],
- function () {
- echoServer.kill();
- }
- );
-}, 100);
Please sign in to comment.
Something went wrong with that request. Please try again.