From 00a82e58f487db8a4342cfcb0429109801db5f6c Mon Sep 17 00:00:00 2001 From: kartikk221 Date: Sat, 6 Apr 2024 12:29:45 -0400 Subject: [PATCH] Added test for transfer encoding body limitation test --- src/components/http/Request.js | 4 ++-- tests/components/http/Request.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/components/http/Request.js b/src/components/http/Request.js index 013e5a0..a02b4e4 100644 --- a/src/components/http/Request.js +++ b/src/components/http/Request.js @@ -309,8 +309,8 @@ class Request { case 1: // Pass through the uWS volatile ArrayBuffer chunk to the passthrough callback as a volatile Uint8Array chunk this._body_parser_passthrough( - // If this is a chunked transfer, we need to copy the chunk as any passthrough consumer - // will have no immediate way of processing hence this chunk needs to stick around in memory across multiple cycles without being deallocated by uWS + // If this is a chunked transfer, we need to COPY the chunk as any passthrough consumer will have no immediate way of processing + // hence this chunk needs to stick around across multiple cycles without being deallocated by uWS this._body_chunked_transfer ? copy_array_buffer_to_uint8array(chunk) : new Uint8Array(chunk), diff --git a/tests/components/http/Request.js b/tests/components/http/Request.js index c2ebc4d..08c6958 100644 --- a/tests/components/http/Request.js +++ b/tests/components/http/Request.js @@ -7,6 +7,8 @@ const { test_request_body_echo_test } = require('./scenarios/request_body_echo_t const { test_request_uncaught_rejections } = require('./scenarios/request_uncaught_rejections.js'); const { test_request_router_paths_test } = require('./scenarios/request_router_paths_test.js'); const { test_request_chunked_json } = require('./scenarios/request_chunked_json.js'); +const fs = require('fs'); +const _path = require('path'); const crypto = require('crypto'); const router = new HyperExpress.Router(); const endpoint = '/tests/request/:param1/:param2'; @@ -189,6 +191,32 @@ async function test_request_object() { // Assert rejection status code as 413 Too Large Payload assert_log(group, 'Too Large Body 413 HTTP Code Reject', () => too_large_response.status === 413); + // Perform a too large body test with transfer-encoding: chunked + const temp_file_path = _path.resolve(_path.join(__dirname, '../../../tests/content/too-large-file.temp')); + fs.writeFileSync(temp_file_path, too_large_body_value); + try { + const too_large_chunked_response = await fetch(base + url, { + method: test_method, + body: fs.createReadStream(temp_file_path), + headers: { + 'transfer-encoding': 'chunked', + }, + }); + + // Cleanup the temp file + fs.unlinkSync(temp_file_path); + + // Assert rejection status code as 413 Too Large Payload + assert_log( + group, + 'Too Large Body 413 HTTP Code Reject (Chunked)', + () => too_large_chunked_response.status === 413 + ); + } catch (error) { + // Cleanup the temp file + fs.unlinkSync(temp_file_path); + } + // Perform a request with a urlencoded body to test .urlencoded() method const urlencoded_string = `url1=${param1}&url2=${param2}`; const urlencoded_response = await fetch(base + url, {