Updated test

This commit is contained in:
delvedor
2018-10-26 19:06:04 +02:00
parent 51e6c76511
commit 961b8224ef
4 changed files with 127 additions and 3 deletions

View File

@ -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)

View File

@ -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()
})

View File

@ -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()

View File

@ -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)
})
})
})