From 09d9df6b087c09a56c7c5b026543267bcaaa64cb Mon Sep 17 00:00:00 2001 From: Malte-Thorben Bruns Date: Tue, 21 Apr 2015 17:41:09 +0200 Subject: [PATCH] lib: add globalClient --- lib/index.js | 17 ++++++++- test/test-global.js | 86 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 test/test-global.js diff --git a/lib/index.js b/lib/index.js index 9955366..9ce97b5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,21 @@ 'use strict'; +var RAIL = require('./rail'); +var globalClient = new RAIL(); -module.exports = exports = require('./rail'); +module.exports = exports = RAIL; exports.Call = require('./call'); exports.plugins = require('./plugins'); + +globalClient.use('buffer'); +globalClient.use('json'); +globalClient.use('redirect'); +globalClient.use('cookies'); + +exports.globalClient = globalClient; + + +function call(urlOrOptions, responseListener) { + return globalClient.call(urlOrOptions, responseListener); +} +exports.call = call; diff --git a/test/test-global.js b/test/test-global.js new file mode 100644 index 0000000..1df17a0 --- /dev/null +++ b/test/test-global.js @@ -0,0 +1,86 @@ +'use strict'; +/* global suite: false, setup: false, test: false, + teardown: false, suiteSetup: false, suiteTeardown: false */ +var assert = require('assert'); +var common = require('./common'); +var https = require('https'); +var RAIL = require('../'); + + +suite('global', function() { + var server; + var onrequest; + + var listener = function(request, response) { + if (typeof onrequest === 'function') { + onrequest(request, response); + } + }; + + + suiteSetup(function(done) { + process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; + + var options = { + key: common.serverKey, + cert: common.serverCert + }; + + server = https.createServer(options, listener); + server.listen(common.port, done); + }); + + + test('call(url)', function(done) { + var path = '/some/path/on/the/service'; + var url = 'https://localhost:' + common.port + path; + + onrequest = function(request, response) { + assert.strictEqual(request.url, path); + response.end('pong'); + }; + + RAIL.call(url, function(response) { + var body = []; + + response.on('readable', function() { + var data = response.read(); + body.push(data); + }); + + response.on('end', function() { + assert.strictEqual(response.statusCode, 200); + assert.strictEqual(Buffer.concat(body).toString(), 'pong'); + done(); + }); + }).end(); + }); + + + test('call(options-with-url)', function(done) { + var path = '/some/other/path/on/the/service'; + var url = 'https://localhost:' + common.port + path; + + onrequest = function(request, response) { + assert.strictEqual(request.url, path); + response.end('pong'); + }; + + RAIL.call({ + url: url, + buffer: true + }, function(response) { + assert.strictEqual(response.statusCode, 200); + assert(response.buffer); + assert.strictEqual(response.buffer.toString(), 'pong'); + done(); + }).end(); + }); + + + suiteTeardown(function(done) { + delete process.env.NODE_TLS_REJECT_UNAUTHORIZED; + + server.close(done); + }); +});