Permalink
Browse files

Refactor unit tests

  • Loading branch information...
1 parent e06edf8 commit 80b444b641e7b90edccef7f43942275a9fe81a26 @pfleidi committed May 1, 2011
View
@@ -0,0 +1,50 @@
+/*!
+ * Client unittest helpers
+ *
+ * @autor pfleidi
+ */
+
+var Helper = require('./test_helper');
+var assert = require('assert');
+
+exports.simpleRequest = function simpleRequest(options) {
+ var callbacks = 0;
+ var echoServer = options.server || Helper.echoServer();
+ var upCase = options.verb.replace(/del/, 'delete').toUpperCase();
+
+ function _check(data, resp, cb) {
+ callbacks += 1;
+ assert.ok(resp, 'Response must be provided');
+ if (options.verb !== 'head') {
+ assert.ok(data, 'Data must be provided');
+ assert.strictEqual(data.method, upCase);
+ assert.strictEqual(data.url, options.path);
+ if (cb) {
+ cb(data, resp);
+ }
+ }
+ }
+
+ options.client[options.verb](echoServer.url + options.path, options.reqopts)
+ .on('error', function (err) {
+ callbacks += 1;
+ console.log('caught error: ' + err.stack);
+ })
+ .on('2XX', function (data, resp) {
+ _check(data, resp, options.callback2XX);
+ })
+ .on('success', function (data, resp) {
+ _check(data, resp, options.callbackSuccess);
+ })
+ .on('complete', function (data, resp) {
+ assert.ok(resp, 'Response must be provided');
+ callbacks += 1;
+ });
+
+ options.beforeExit(function () {
+ assert.strictEqual(callbacks, 3, 'Ensure all callbacks are called');
+ });
+
+};
+
+
@@ -10,16 +10,14 @@ var assert = require('assert');
var Helper = require('./test_helper');
var HttpClient = require('../index');
-var client = HttpClient.createClient({
- parseContent: JSON.parse
- });
+var client = HttpClient.createClient({ contentParser: HttpClient.parsers.json});
function _assertWithPayload(beforeExit, verb, payload) {
var callbacks = 0;
var echoServer = Helper.echoServer();
var upCase = verb.toUpperCase();
- client[verb](echoServer.url + '/foo', payload)
+ client[verb](echoServer.url + '/foo', { payload: payload })
.on('success', function (data, resp) {
callbacks += 1;
assert.ok(data, 'Data must be provided');
@@ -7,48 +7,40 @@
var assert = require('assert');
var Helper = require('./test_helper');
+var ClientHelper = require('./client_helper');
var HttpClient = require('../index');
-var client1 = HttpClient.createClient();
-var client2 = HttpClient.createClient({ gzip: true });
+var client1 = HttpClient.createClient({ contentParser: HttpClient.parsers.json });
+var client2 = HttpClient.createClient({
+ contentParser: HttpClient.parsers.json,
+ gzip: true
+ });
function _simple(beforeExit, verb, payload, mimetype, gzip) {
- var callbacks = 0;
- var echoServer = Helper.echoServer();
- var upCase = verb.toUpperCase();
var client = gzip ? client2 : client1;
- client[verb](echoServer.url + '/foo', payload, {
- 'Content-Type': mimetype
- })
- .on('2XX', function (data, resp) {
- callbacks += 1;
- var req = JSON.parse(data);
- assert.ok(data, 'Data must be provided');
- assert.ok(resp, 'Response must be provided');
- assert.strictEqual(req.method, upCase);
- assert.strictEqual(req.payload, payload);
- assert.strictEqual(req.headers['content-type'], mimetype || 'application/x-www-form-urlencoded');
- assert.strictEqual(req.url, '/foo');
- assert.strictEqual(req.headers['user-agent'], 'node-wwwdude');
- if (gzip) {
- assert.strictEqual(req.headers['accept-encoding'], 'gzip');
- assert.strictEqual(resp.headers['content-encoding'], 'gzip');
- } else {
- assert.strictEqual(req.headers['accept-encoding'], undefined);
- assert.strictEqual(resp.headers['content-encoding'], undefined);
- }
- })
- .on('success', function (data, resp) {
- callbacks += 1;
- var req = JSON.parse(data);
- assert.ok(data, 'Data must be provided');
- })
- .on('complete', function (data, resp) {
- callbacks += 1;
- });
+ ClientHelper.simpleRequest({
+ client: client,
+ beforeExit: beforeExit,
+ verb: verb,
+ path: '/foo',
+ reqopts: {
+ headers: { 'Content-Type': mimetype },
+ payload: payload
+ },
+ callback2XX: function (data, resp) {
+ assert.strictEqual(data.headers['user-agent'], 'node-wwwdude');
+ assert.strictEqual(data.payload, payload);
+ assert.strictEqual(data.headers['content-type'], mimetype || 'application/x-www-form-urlencoded');
+ assert.strictEqual(data.headers['user-agent'], 'node-wwwdude');
+ if (gzip) {
+ assert.strictEqual(data.headers['accept-encoding'], 'gzip');
+ assert.strictEqual(resp.headers['content-encoding'], 'gzip');
+ } else {
+ assert.strictEqual(data.headers['accept-encoding'], undefined);
+ assert.strictEqual(resp.headers['content-encoding'], undefined);
+ }
- beforeExit(function () {
- assert.strictEqual(callbacks, 3, 'Ensure all callbacks are called');
+ }
});
}
@@ -21,7 +21,7 @@ function _testServerError(beforeExit, verb, payload) {
var echoServer = Helper.echoServer();
var upCase = verb.replace(/del/, 'delete').toUpperCase();
- client500[verb](echoServer.url + '/foo', payload)
+ client500[verb](echoServer.url + '/foo', { payload: payload })
.on('http-server-error', function (data, response) {
callbacks += 1;
var req = JSON.parse(data);
@@ -64,7 +64,7 @@ function _testClientError(beforeExit, verb, payload) {
var echoServer = Helper.echoServer();
var upCase = verb.replace(/del/, 'delete').toUpperCase();
- client400[verb](echoServer.url + '/foo', payload)
+ client400[verb](echoServer.url + '/foo', { payload: payload })
.on('http-client-error', function (data, response) {
callbacks += 1;
var req = JSON.parse(data);
@@ -134,3 +134,25 @@ exports.clientErrorPost = function (test) {
exports.clientErrorDel = function (test) {
_testClientError(test, 'del');
};
+
+exports.parseError = function (beforeExit) {
+ var callbacks = 0;
+ var client = HttpClient.createClient({
+ contentParser: function (content, callback) {
+ callback(new Error('test'));
+ }
+ });
+ var echoServer = Helper.echoServer();
+ client.get(echoServer.url + '/foo')
+ .on('error', function () {
+ callbacks += 1;
+ })
+ .on('complete', function () {
+ throw new Error('this should not happen!');
+ });
+
+ beforeExit(function () {
+ assert.strictEqual(callbacks, 1, 'Ensure only error is called');
+ });
+
+};
View
@@ -4,39 +4,27 @@
* @author pfleidi
*/
-var Fs = require('fs');
+var ClientHelper = require('./client_helper');
var Helper = require('./test_helper');
+var Fs = require('fs');
var HttpClient = require('../index');
var assert = require('assert');
-var client = HttpClient.createClient();
+var client = HttpClient.createClient({ contentParser: HttpClient.parsers.json });
function _simpleHttps(beforeExit, verb) {
- var callbacks = 0;
- var echoServer = Helper.echoServer({
- key: Fs.readFileSync(__dirname + '/fixtures/server.pem'),
- cert: Fs.readFileSync(__dirname + '/fixtures/server.pem')
- });
-
- var upCase = verb.replace(/del/, 'delete').toUpperCase();
-
- client[verb](echoServer.url + '/foo')
- .on('success', function (data, resp) {
- callbacks += 1;
- var req = JSON.parse(data);
- assert.ok(data, 'Data must be provided');
- assert.ok(resp, 'Response must be provided');
- assert.strictEqual(req.method, upCase);
- assert.strictEqual(req.url, '/foo');
- assert.strictEqual(req.headers['user-agent'], 'node-wwwdude');
- })
- .on('complete', function (data, resp) {
- callbacks += 1;
- });
-
- beforeExit(function () {
- assert.strictEqual(callbacks, 2, 'Ensure all callbacks are called');
+ ClientHelper.simpleRequest({
+ client: client,
+ beforeExit: beforeExit,
+ verb: verb,
+ path: '/foo',
+ server: Helper.echoServer({
+ key: Fs.readFileSync(__dirname + '/fixtures/server.pem'),
+ cert: Fs.readFileSync(__dirname + '/fixtures/server.pem')
+ }),
+ callback2XX: function (data, resp) {
+ assert.strictEqual(data.headers['user-agent'], 'node-wwwdude');
+ }
});
-
}
['get', 'put', 'post', 'del'].forEach(function (verb) {
Oops, something went wrong.

0 comments on commit 80b444b

Please sign in to comment.