Skip to content

Commit

Permalink
Add https unit test
Browse files Browse the repository at this point in the history
Add HTTPS support
  • Loading branch information
pfleidi committed Mar 22, 2011
1 parent 0279237 commit 5b200f5
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -19,6 +19,7 @@ Features
* Very customizable (custom headers on client/request basis ...)
* Automatic redirect following
* Automatic gzip decode support
* Automatic support for HTTPS (node 0.4.x only)
* Flexible handling of responses with event emitters

Installation
Expand Down
5 changes: 2 additions & 3 deletions lib/wwwdude/index.js
Expand Up @@ -23,10 +23,10 @@ var defaultHeaders = {
'User-Agent': 'node-wwwdude'
};

exports.version = '0.0.6';
exports.version = '0.0.7';

/**
* Factory method to initalize a client
* Factory method to create a client
*
* @return {Object}
* @api public
Expand Down Expand Up @@ -199,7 +199,6 @@ var createClient = exports.createClient = function createClient(clientOptions) {
return emitter;
}


/* return the actual API */
return {

Expand Down
9 changes: 8 additions & 1 deletion lib/wwwdude/node-versions/v0.4.x.js
Expand Up @@ -8,6 +8,7 @@
*/

var Http = require('http');
var Https = require('https');

/**
* Make the actual request for Node v0.4.x
Expand All @@ -20,6 +21,7 @@ exports.request = function (context) {
var url = context.url;
var headers = context.options.headers;
var payload = context.options.payload;
var request;

var options = {
host: url.hostname,
Expand All @@ -36,7 +38,12 @@ exports.request = function (context) {
headers['Content-Length'] = payload.length;
}

var request = Http.request(options, context.handler);
if (url.protocol === 'https:') {
request = Https.request(options, context.handler);
} else {
request = Http.request(options, context.handler);
}

request.on('error', function (err) {
context.respond('error', err);
});
Expand Down
1 change: 1 addition & 0 deletions lib/wwwdude/util.js
Expand Up @@ -119,6 +119,7 @@ exports.parseUrl = function parseUrl(url) {
container.path = (parsedUrl.pathname || '/') + container.queryparms + container.hash;
container.host = parsedUrl.host;
container.hostname = parsedUrl.hostname;
container.protocol = parsedUrl.protocol;

return container;
};
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{ "name" : "wwwdude",
"description" : "Simple to use HTTP library on top of the built in libs of node.js",
"tags" : ["http", "simple", "util", "rest", "utility"],
"version" : "0.0.6",
"version" : "0.0.7",
"author" : "Sven Pfleiderer <sven@roothausen.de>",
"directories" : { "lib" : "lib" },
"repository" : {
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/server.pem
@@ -0,0 +1,35 @@
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQClc51goqPAj7zV0Za04iz7bEGJpCYSXrwgoieHu+//6Kj54n/D
0p5GmNpwCJcEGucJAeGehmjPrU+aE0p3C9uxb5qIhjO6gaWiv7JPIDUKEO401Ew0
V63gietJRSrIXQINu5CdFwlBs28vnu8DJZD61dxoQhqmmrRZOKfM4ByaywIDAQAB
AoGAZ2xhGVIxeKKhUSuFwKaNpdDjGu5AWkVb5i5W9ZM2kErYrfO3GCrlg43zJgtI
k2lZuJizd4vfospcsxyR9N61wjAT7dNON33N+KxE6+dX3lr78/XpOzgn2de90DWz
ELgvUMNEjhyc4dI3cY1ukHz0R4CVKFvFyBIUn2quCEBp8GkCQQDal+FXWAH2X6kj
xWzhqKsBF7wMsk2U8CADeVoL685iPnz8vxnjEyOZOA/xLEWXPI/pDRVdBA6YMnfP
j3/LWqjfAkEAwcO1Sqpv0zh8opuj7A0lHDVv6c5/c3Eb4YAZ4cJdKTl6EhPC5bTA
RJAMKGn8xFWgiq9cYy9AyCTsroSLp7TPlQJBANCWR4A6Sb0YsWSh+nEmqu8i/925
sxBhRpsqDEP94vWanVfs3z3wwHJvrb68OTfEoaqIQo981JRk7DESxKZ+v08CQQCo
jQb6EXK/CUcZ6EvavcCHXlCg0Rl7IWxAZ7+b7AhAIRh3+yqxyN7rhWaeDTxsyZOi
sFKskOFIeR5N3UzisN1VAkEAswzfyhdigjRJOPdjGn0kTM5OSA79FLtHJfBL59sD
CLZmPmTG1RvpOXOkAyeazb+YBavab+MxoY9azMPlmh+K6Q==
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFIC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Zc1d2VijzRAa72xXWKr+cxN2RUaeq/jJgNA3LWTlZKBzbdyg67
gSi/Rx/mlxaizihvCuTHcYOmqR31XSg=
-----END CERTIFICATE-----
31 changes: 22 additions & 9 deletions test/test_helper.js
Expand Up @@ -19,8 +19,11 @@ function _routeall(app, path, handler) {
});
}

exports.echoServer = function () {
exports.echoServer = function (options) {
var port = exports.port;
var protocol = options ? 'https' : 'http';
var server;

exports.port += 1;

function handler(request, response, next) {
Expand All @@ -43,18 +46,28 @@ exports.echoServer = function () {
server.close();
}

var server = Connect.createServer(
Connect.bodyParser(),
Gzip(),
Connect.router(function (app) {
_routeall(app, /foo(.*)/, handler);
})
);
if (options) {
server = Connect.createServer(
options,
Connect.bodyParser(),
Connect.router(function (app) {
_routeall(app, /foo(.*)/, handler);
})
);
} else {
server = Connect.createServer(
Connect.bodyParser(),
Gzip(),
Connect.router(function (app) {
_routeall(app, /foo(.*)/, handler);
})
);
}

server.listen(port, 'localhost');

return {
url: 'http://localhost:' + port,
url: protocol + '://localhost:' + port,
serv: server
};
};
Expand Down
46 changes: 46 additions & 0 deletions test/wwwdude-https.js
@@ -0,0 +1,46 @@
/*!
* unit tests to ensure simple use cases work as expected
*
* @author pfleidi
*/

var Fs = require('fs');
var Helper = require('./test_helper');
var HttpClient = require('../index');
var assert = require('assert');
var client = HttpClient.createClient();

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');
});

}

['get', 'put', 'post', 'del'].forEach(function (verb) {
exports[verb] = function (beforeExit) {
_simpleHttps(beforeExit, verb);
};
});
3 changes: 3 additions & 0 deletions test/wwwdude-util.js
Expand Up @@ -49,6 +49,7 @@ var exUrl1 = {
hash: '#asdf',
host: 'foo.bar.baz',
hostname: 'foo.bar.baz',
protocol: 'http:',
path: '/hellotest.aa?foo=bar&23=42#asdf'
};

Expand All @@ -58,6 +59,7 @@ var exUrl2 = {
hash: '',
host: 'foo.bar.baz:2342',
hostname: 'foo.bar.baz',
protocol: 'http:',
path: '/lala'
};

Expand All @@ -67,6 +69,7 @@ var exUrl3 = {
hash: '',
host: 'foo.bar.baz',
hostname: 'foo.bar.baz',
protocol: 'https:',
path: '/asdf'
};

Expand Down

0 comments on commit 5b200f5

Please sign in to comment.