Throw if the content length is too big (#1355)

This commit is contained in:
Tomas Della Vedova
2020-11-24 09:48:17 +01:00
committed by GitHub
parent 959aee5903
commit 502509cabc
2 changed files with 77 additions and 1 deletions

View File

@ -21,7 +21,9 @@
const { test } = require('tap')
const { URL } = require('url')
const { Client, ConnectionPool, Transport, errors } = require('../../index')
const buffer = require('buffer')
const intoStream = require('into-stream')
const { Client, ConnectionPool, Transport, Connection, errors } = require('../../index')
const { CloudConnectionPool } = require('../../lib/pool')
const { buildServer } = require('../utils')
@ -1243,3 +1245,58 @@ test('Socket destryed while reading the body', t => {
})
})
})
test('Content length too big (buffer)', t => {
t.plan(4)
class MockConnection extends Connection {
request (params, callback) {
const stream = intoStream(JSON.stringify({ hello: 'world' }))
stream.statusCode = 200
stream.headers = {
'content-type': 'application/json;utf=8',
'content-encoding': 'gzip',
'content-length': buffer.constants.MAX_LENGTH + 10,
connection: 'keep-alive',
date: new Date().toISOString()
}
stream.on('close', () => t.pass('Stream destroyed'))
process.nextTick(callback, null, stream)
return { abort () {} }
}
}
const client = new Client({ node: 'http://localhost:9200', Connection: MockConnection })
client.info((err, result) => {
t.ok(err instanceof errors.RequestAbortedError)
t.is(err.message, `The content length (${buffer.constants.MAX_LENGTH + 10}) is bigger than the maximum allowed buffer (${buffer.constants.MAX_LENGTH})`)
t.strictEqual(result.meta.attempts, 0)
})
})
test('Content length too big (string)', t => {
t.plan(4)
class MockConnection extends Connection {
request (params, callback) {
const stream = intoStream(JSON.stringify({ hello: 'world' }))
stream.statusCode = 200
stream.headers = {
'content-type': 'application/json;utf=8',
'content-length': buffer.constants.MAX_STRING_LENGTH + 10,
connection: 'keep-alive',
date: new Date().toISOString()
}
stream.on('close', () => t.pass('Stream destroyed'))
process.nextTick(callback, null, stream)
return { abort () {} }
}
}
const client = new Client({ node: 'http://localhost:9200', Connection: MockConnection })
client.info((err, result) => {
t.ok(err instanceof errors.RequestAbortedError)
t.is(err.message, `The content length (${buffer.constants.MAX_STRING_LENGTH + 10}) is bigger than the maximum allowed string (${buffer.constants.MAX_STRING_LENGTH})`)
t.strictEqual(result.meta.attempts, 0)
})
})