From 74e5eb22913c7fcd6080a3c455ae01fde48e2bb9 Mon Sep 17 00:00:00 2001 From: Malte-Thorben Bruns Date: Fri, 24 Apr 2015 00:37:17 +0200 Subject: [PATCH] test: extend retry, timeout & call tests --- test/test-1-https.js | 3 +- test/test-http-retry.js | 15 ++++++++++ test/test-http-timeout.js | 36 ++++++++++++++++++++-- test/test-public-api.js | 63 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 4 deletions(-) diff --git a/test/test-1-https.js b/test/test-1-https.js index 91cdd40..478b66b 100644 --- a/test/test-1-https.js +++ b/test/test-1-https.js @@ -40,7 +40,8 @@ suite('https', function() { rail.call({ proto: 'https', - port: common.port + port: common.port, + agent: null }, function(response) { response.on('readable', function() { response.read(); diff --git a/test/test-http-retry.js b/test/test-http-retry.js index d39a104..559ecac 100644 --- a/test/test-http-retry.js +++ b/test/test-http-retry.js @@ -30,6 +30,21 @@ suite('http:retry', function() { }); + test('configure', function() { + var options = { + retry: {} + }; + rail.plugins.retry._configure(options); + assert.deepEqual(options, {retry: {interval: 20, limit: 3}}); + + options = { + retry: false + }; + rail.plugins.retry._configure(options); + assert.deepEqual(options, {retry: {limit: 0}}); + }); + + test('call', function(done) { var retries = 0; diff --git a/test/test-http-timeout.js b/test/test-http-timeout.js index a231840..4b8d9c1 100644 --- a/test/test-http-timeout.js +++ b/test/test-http-timeout.js @@ -21,7 +21,8 @@ suite('http:timeout', function() { suiteSetup(function(done) { rail = new RAIL(); rail.use('timeout', { - response: 50 + response: 20, + socket: 20 }); server = http.createServer(listener); @@ -29,7 +30,7 @@ suite('http:timeout', function() { }); - test('call', function(done) { + test('repsonse timeout', function(done) { onrequest = function(request, response) { response.end('pong'); }; @@ -37,11 +38,40 @@ suite('http:timeout', function() { var call = rail.call({ proto: 'http', host: 'github.com', - port: 55555 + port: 55555, + timeout: { + socket: 0 + } }).on('timeout', function(type) { + assert.strictEqual(type, 'response'); call.abort(); }).on('error', function(err) { assert(err); + assert.strictEqual(err.message, 'socket hang up'); + assert.strictEqual(err.reason, 'user'); + done(); + }).end(); + }); + + + test('socket timeout', function(done) { + onrequest = function(request, response) { + response.end('pong'); + }; + + var call = rail.call({ + proto: 'http', + host: 'github.com', + port: 55555, + timeout: { + response: 0 + } + }).on('timeout', function(type) { + assert.strictEqual(type, 'socket'); + call.abort(); + }).on('error', function(err) { + assert(err); + assert.strictEqual(err.message, 'socket hang up'); assert.strictEqual(err.reason, 'user'); done(); }).end(); diff --git a/test/test-public-api.js b/test/test-public-api.js index 6a1beb8..dda9289 100644 --- a/test/test-public-api.js +++ b/test/test-public-api.js @@ -42,4 +42,67 @@ suite('public-api', function() { } }, function() {}); }); + + + test('call.__intercept "interceptor should be a function"', function() { + var client = rail(); + var call = client.call(); + + assert.throws(function() { + call.__intercept('response'); + }, TypeError, 'interceptor should be a function'); + }); + + + test('call.abort "Not connected"', function(done) { + var client = rail(); + var call = client.call(); + assert(call.__request()); + + call.once('error', function(err) { + assert(err); + assert(err.message, 'Not connected'); + done(); + }); + + call.abort(); + call.abort(); + call.end(); + }); + + + test('call.__request "No configuration available"', function(done) { + var client = rail(); + var call = client.call(); + + call.once('error', function(err) { + assert(err); + assert(err.message, 'No configuration available'); + done(); + }); + + ++call._pointer; + call.__request(); + }); + + test('call._urlToOptions', function() { + var client = rail(); + var call = client.call(); + + var options = call._urlToOptions({ + request: { + method: 'POST' + } + }, 'http://github.com'); + + assert.deepEqual(options, { + proto: 'http', + request: { + method: 'POST', + host: 'github.com', + path: '/', + port: null + } + }); + }); });