|
21 | 21 |
|
22 | 22 | const { test } = require('tap')
|
23 | 23 | const { URL } = require('url')
|
24 |
| -const { Client, ConnectionPool, Transport, errors } = require('../../index') |
| 24 | +const buffer = require('buffer') |
| 25 | +const intoStream = require('into-stream') |
| 26 | +const { Client, ConnectionPool, Transport, Connection, errors } = require('../../index') |
25 | 27 | const { CloudConnectionPool } = require('../../lib/pool')
|
26 | 28 | const { buildServer } = require('../utils')
|
27 | 29 |
|
@@ -1243,3 +1245,58 @@ test('Socket destryed while reading the body', t => {
|
1243 | 1245 | })
|
1244 | 1246 | })
|
1245 | 1247 | })
|
| 1248 | + |
| 1249 | +test('Content length too big (buffer)', t => { |
| 1250 | + t.plan(4) |
| 1251 | + |
| 1252 | + class MockConnection extends Connection { |
| 1253 | + request (params, callback) { |
| 1254 | + const stream = intoStream(JSON.stringify({ hello: 'world' })) |
| 1255 | + stream.statusCode = 200 |
| 1256 | + stream.headers = { |
| 1257 | + 'content-type': 'application/json;utf=8', |
| 1258 | + 'content-encoding': 'gzip', |
| 1259 | + 'content-length': buffer.constants.MAX_LENGTH + 10, |
| 1260 | + connection: 'keep-alive', |
| 1261 | + date: new Date().toISOString() |
| 1262 | + } |
| 1263 | + stream.on('close', () => t.pass('Stream destroyed')) |
| 1264 | + process.nextTick(callback, null, stream) |
| 1265 | + return { abort () {} } |
| 1266 | + } |
| 1267 | + } |
| 1268 | + |
| 1269 | + const client = new Client({ node: 'http://localhost:9200', Connection: MockConnection }) |
| 1270 | + client.info((err, result) => { |
| 1271 | + t.ok(err instanceof errors.RequestAbortedError) |
| 1272 | + t.is(err.message, `The content length (${buffer.constants.MAX_LENGTH + 10}) is bigger than the maximum allowed buffer (${buffer.constants.MAX_LENGTH})`) |
| 1273 | + t.strictEqual(result.meta.attempts, 0) |
| 1274 | + }) |
| 1275 | +}) |
| 1276 | + |
| 1277 | +test('Content length too big (string)', t => { |
| 1278 | + t.plan(4) |
| 1279 | + |
| 1280 | + class MockConnection extends Connection { |
| 1281 | + request (params, callback) { |
| 1282 | + const stream = intoStream(JSON.stringify({ hello: 'world' })) |
| 1283 | + stream.statusCode = 200 |
| 1284 | + stream.headers = { |
| 1285 | + 'content-type': 'application/json;utf=8', |
| 1286 | + 'content-length': buffer.constants.MAX_STRING_LENGTH + 10, |
| 1287 | + connection: 'keep-alive', |
| 1288 | + date: new Date().toISOString() |
| 1289 | + } |
| 1290 | + stream.on('close', () => t.pass('Stream destroyed')) |
| 1291 | + process.nextTick(callback, null, stream) |
| 1292 | + return { abort () {} } |
| 1293 | + } |
| 1294 | + } |
| 1295 | + |
| 1296 | + const client = new Client({ node: 'http://localhost:9200', Connection: MockConnection }) |
| 1297 | + client.info((err, result) => { |
| 1298 | + t.ok(err instanceof errors.RequestAbortedError) |
| 1299 | + 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})`) |
| 1300 | + t.strictEqual(result.meta.attempts, 0) |
| 1301 | + }) |
| 1302 | +}) |
0 commit comments