Add ability to disable the http agent (#1251)

This commit is contained in:
Tomas Della Vedova
2020-07-13 15:07:51 +02:00
committed by GitHub
parent 39cf023426
commit 1592c4d575
8 changed files with 117 additions and 13 deletions

View File

@ -51,12 +51,14 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 42
})
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: {
url: 'http://localhost:9200',
@ -76,6 +78,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
maxRetries: 'five'
@ -93,6 +96,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
requestTimeout: 'five'
@ -110,6 +114,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
pingTimeout: 'five'
@ -134,6 +139,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffInterval: 'five'
@ -151,6 +157,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffOnStart: 'no'
@ -168,6 +175,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffEndpoint: false
@ -185,6 +193,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
sniffOnConnectionFault: 'yes'
@ -216,6 +225,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
resurrectStrategy: 'custom'
@ -233,6 +243,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
suggestCompression: 'no'
@ -250,6 +261,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
compression: 'deflate'
@ -267,6 +279,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
headers: 'foo=bar'
@ -284,6 +297,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
opaqueIdPrefix: 42
@ -308,6 +322,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
name: 42
@ -349,6 +364,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
auth: 'password'
@ -367,6 +383,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
cloud: {
id: 42
@ -389,12 +406,22 @@ expectType<Client>(
})
)
expectType<Client>(
new Client({
node: 'http://localhost:9200',
agent: false
})
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
agent: {
// @ts-expect-error
keepAlive: 'yes',
// @ts-expect-error
keepAliveMsecs: true,
// @ts-expect-error
maxSockets: 'all',
maxFreeSockets: null
}
@ -418,7 +445,9 @@ expectError<errors.ConfigurationError>(
new Client({
node: 'http://localhost:9200',
ssl: {
// @ts-expect-error
ca: 42,
// @ts-expect-error
rejectUnauthorized: 'yes'
}
})
@ -437,6 +466,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
generateRequestId: 'id'
@ -463,6 +493,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
nodeSelector (connections) {
@ -484,6 +515,7 @@ expectType<Client>(
)
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
nodeFilter (connection) {
@ -518,6 +550,7 @@ expectError<errors.ConfigurationError>(
}
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
Serializer: CustomSerializer
@ -553,6 +586,7 @@ expectError<errors.ConfigurationError>(
expectError<errors.ConfigurationError>(
new Client({
node: 'http://localhost:9200',
// @ts-expect-error
Connection: CustomConnection
})
)
@ -584,6 +618,7 @@ expectError<errors.ConfigurationError>(
}
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
ConnectionPool: CustomConnectionPool
@ -617,6 +652,7 @@ expectError<errors.ConfigurationError>(
}
expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
node: 'http://localhost:9200',
Transport: CustomTransport

View File

@ -90,15 +90,10 @@ expectError(
body: 42
}).then(console.log)
)
/*
* We cannot test yet the absence of a property because tsd is still shipping
* TypeScript v3.7, as soon as it will be 3.9, we'll uncomment the following test.
// @ts-expect-error
client.async_search.get()
*/
// callback api is not supported
expectError(client.cat.count({ index: 'test' }, {}, (err: any, result: any) => {}))

View File

@ -1093,6 +1093,32 @@ test('Random selector', t => {
})
})
test('Disable keep alive agent', t => {
t.plan(3)
function handler (req, res) {
t.strictEqual(req.headers.connection, 'close')
res.setHeader('Content-Type', 'application/json;utf=8')
res.end(JSON.stringify({ hello: 'world' }))
}
buildServer(handler, ({ port }, server) => {
const client = new Client({
node: `http://localhost:${port}`,
agent: false
})
client.search({
index: 'test',
q: 'foo:bar'
}, (err, { body }) => {
t.error(err)
t.deepEqual(body, { hello: 'world' })
server.stop()
})
})
})
test('name property as string', t => {
t.plan(1)

View File

@ -198,7 +198,7 @@ test('Disable keep alive', t => {
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`),
agent: { keepAlive: false }
agent: false
})
connection.request({
path: '/hello',
@ -499,6 +499,43 @@ test('Should not close a connection if there are open requests', t => {
})
})
test('Should not close a connection if there are open requests (with agent disabled)', t => {
t.plan(4)
function handler (req, res) {
setTimeout(() => res.end('ok'), 1000)
}
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`),
agent: false
})
setTimeout(() => {
t.strictEqual(connection._openRequests, 1)
connection.close()
}, 500)
connection.request({
path: '/hello',
method: 'GET'
}, (err, res) => {
t.error(err)
t.strictEqual(connection._openRequests, 0)
var payload = ''
res.setEncoding('utf8')
res.on('data', chunk => { payload += chunk })
res.on('error', err => t.fail(err))
res.on('end', () => {
t.strictEqual(payload, 'ok')
server.stop()
})
})
})
})
test('Url with auth', t => {
t.plan(2)