Skip to content

Commit

Permalink
Added test for transfer encoding body limitation test
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikk221 committed Apr 6, 2024
1 parent 7940d15 commit 00a82e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/http/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
28 changes: 28 additions & 0 deletions tests/components/http/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 00a82e5

Please sign in to comment.