From 961b8224efdeab0033e85337c5f81ee5470d0b09 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 26 Oct 2018 19:06:04 +0200 Subject: [PATCH] Updated test --- test/unit/connection-pool.test.js | 2 +- test/unit/connection.test.js | 54 +++++++++++++++++++++++++++++++ test/unit/serializer.test.js | 29 +++++++++++++++++ test/unit/transport.test.js | 45 ++++++++++++++++++++++++-- 4 files changed, 127 insertions(+), 3 deletions(-) diff --git a/test/unit/connection-pool.test.js b/test/unit/connection-pool.test.js index 7bc435f48..9bd300edf 100644 --- a/test/unit/connection-pool.test.js +++ b/test/unit/connection-pool.test.js @@ -135,7 +135,7 @@ test('API', t => { var connection = pool.addConnection(href) pool.markDead(connection) pool.resurrect(Date.now() + 1000 * 60 * 3, (isAlive, connection) => { - t.ok(isAlive === null) + t.true(isAlive) connection = pool.connections.get(connection.id) t.strictEqual(connection.deadCount, 1) t.true(connection.resurrectTimeout > 0) diff --git a/test/unit/connection.test.js b/test/unit/connection.test.js index 8ec4eba15..f08a1b2fe 100644 --- a/test/unit/connection.test.js +++ b/test/unit/connection.test.js @@ -192,3 +192,57 @@ test('Timeout support', t => { }) }) }) + +test('querystring', t => { + t.test('Should concatenate the querystring', t => { + t.plan(2) + + function handler (req, res) { + t.strictEqual(req.url, '/hello?hello=world&you_know=for%20search') + res.end('ok') + } + + buildServer(handler, ({ port }, server) => { + const connection = new Connection({ + host: { + href: `http://localhost:${port}`, + protocol: 'http:' + } + }) + connection.request({ + path: '/hello', + method: 'GET', + querystring: 'hello=world&you_know=for%20search' + }, (err, res) => { + t.error(err) + }) + }) + }) + + t.test('If the querystring is null should not do anything', t => { + t.plan(2) + + function handler (req, res) { + t.strictEqual(req.url, '/hello') + res.end('ok') + } + + buildServer(handler, ({ port }, server) => { + const connection = new Connection({ + host: { + href: `http://localhost:${port}`, + protocol: 'http:' + } + }) + connection.request({ + path: '/hello', + method: 'GET', + querystring: null + }, (err, res) => { + t.error(err) + }) + }) + }) + + t.end() +}) diff --git a/test/unit/serializer.test.js b/test/unit/serializer.test.js index 5f44820e2..3c587be8a 100644 --- a/test/unit/serializer.test.js +++ b/test/unit/serializer.test.js @@ -1,6 +1,7 @@ 'use strict' const { test } = require('tap') +const { stringify } = require('querystring') const Serializer = require('../../lib/Serializer') const { SerializationError, DeserializationError } = require('../../lib/errors') @@ -29,6 +30,34 @@ test('ndserialize', t => { ) }) +test('qserialize', t => { + t.plan(1) + const s = new Serializer() + const obj = { + hello: 'world', + you_know: 'for search' + } + + t.strictEqual( + s.qserialize(obj), + stringify(obj) + ) +}) + +test('qserialize (array)', t => { + t.plan(1) + const s = new Serializer() + const obj = { + hello: 'world', + arr: ['foo', 'bar'] + } + + t.strictEqual( + s.qserialize(obj), + 'hello=world&arr=foo%2Cbar' + ) +}) + test('SerializationError', t => { t.plan(1) const s = new Serializer() diff --git a/test/unit/transport.test.js b/test/unit/transport.test.js index 245a1f2f3..34adc8a8a 100644 --- a/test/unit/transport.test.js +++ b/test/unit/transport.test.js @@ -359,13 +359,14 @@ test('Retry mechanism', t => { var count = 0 function handler (req, res) { res.setHeader('Content-Type', 'application/json;utf=8') - if (count++ === 1) { + if (count > 0) { res.end(JSON.stringify({ hello: 'world' })) } else { setTimeout(() => { res.end(JSON.stringify({ hello: 'world' })) }, 1000) } + count++ } buildServer(handler, ({ port }, server) => { @@ -378,6 +379,9 @@ test('Retry mechanism', t => { }, { host: new URL(`http://localhost:${port}`), id: 'node2' + }, { + host: new URL(`http://localhost:${port}`), + id: 'node3' }]) const transport = new Transport({ @@ -385,7 +389,7 @@ test('Retry mechanism', t => { connectionPool: pool, serializer: new Serializer(), maxRetries: 1, - requestTimeout: 500, + requestTimeout: 250, sniffInterval: false, sniffOnStart: false }) @@ -959,3 +963,40 @@ test('Ignore status code', t => { }) }) }) + +test('Should serialize the querystring', t => { + t.plan(2) + + function handler (req, res) { + t.strictEqual(req.url, '/hello?hello=world&you_know=for%20search') + res.end('ok') + } + + buildServer(handler, ({ port }, server) => { + const pool = new ConnectionPool({ + selector: new RoundRobinSelector() + }) + pool.addConnection(`http://localhost:${port}`) + + const transport = new Transport({ + emit: () => {}, + connectionPool: pool, + serializer: new Serializer(), + maxRetries: 3, + requestTimeout: 30000, + sniffInterval: false, + sniffOnStart: false + }) + + transport.request({ + method: 'GET', + path: '/hello', + querystring: { + hello: 'world', + you_know: 'for search' + } + }, (err, body) => { + t.error(err) + }) + }) +})