diff --git a/README.md b/README.md index 1e43c7a..f27cc1b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/wwwdude/index.js b/lib/wwwdude/index.js index 1820df5..fa00cab 100644 --- a/lib/wwwdude/index.js +++ b/lib/wwwdude/index.js @@ -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 @@ -199,7 +199,6 @@ var createClient = exports.createClient = function createClient(clientOptions) { return emitter; } - /* return the actual API */ return { diff --git a/lib/wwwdude/node-versions/v0.4.x.js b/lib/wwwdude/node-versions/v0.4.x.js index 589b058..b8591b2 100644 --- a/lib/wwwdude/node-versions/v0.4.x.js +++ b/lib/wwwdude/node-versions/v0.4.x.js @@ -8,6 +8,7 @@ */ var Http = require('http'); +var Https = require('https'); /** * Make the actual request for Node v0.4.x @@ -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, @@ -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); }); diff --git a/lib/wwwdude/util.js b/lib/wwwdude/util.js index c557e64..5cf5916 100644 --- a/lib/wwwdude/util.js +++ b/lib/wwwdude/util.js @@ -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; }; diff --git a/package.json b/package.json index 750c899..23e486d 100644 --- a/package.json +++ b/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 ", "directories" : { "lib" : "lib" }, "repository" : { diff --git a/test/fixtures/server.pem b/test/fixtures/server.pem new file mode 100644 index 0000000..0d62243 --- /dev/null +++ b/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+MxoY9azM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oqoA26xtMdqJU1RlXsaDB/aPXveax8q/B +hMkI29GUJoPqiIZc1d2VijzRAa72xXWKr+cxN2RUaeq/jJgNA3LWTlZKBzbdyg67 +gSi/Rx/mlxaizihvCuTHcYOmqR31XSg= +-----END CERTIFICATE----- diff --git a/test/test_helper.js b/test/test_helper.js index 52e80aa..859cb4d 100644 --- a/test/test_helper.js +++ b/test/test_helper.js @@ -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) { @@ -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 }; }; diff --git a/test/wwwdude-https.js b/test/wwwdude-https.js new file mode 100644 index 0000000..5d083f5 --- /dev/null +++ b/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); + }; + }); diff --git a/test/wwwdude-util.js b/test/wwwdude-util.js index 0846441..98a5e40 100644 --- a/test/wwwdude-util.js +++ b/test/wwwdude-util.js @@ -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' }; @@ -58,6 +59,7 @@ var exUrl2 = { hash: '', host: 'foo.bar.baz:2342', hostname: 'foo.bar.baz', + protocol: 'http:', path: '/lala' }; @@ -67,6 +69,7 @@ var exUrl3 = { hash: '', host: 'foo.bar.baz', hostname: 'foo.bar.baz', + protocol: 'https:', path: '/asdf' };