Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
lib: add globalClient
Browse files Browse the repository at this point in the history
  • Loading branch information
skenqbx committed Apr 21, 2015
1 parent 2ded40a commit 09d9df6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -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;
86 changes: 86 additions & 0 deletions test/test-global.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 09d9df6

Please sign in to comment.