test: ensure PRECHECKS are working for static file handling
This commit is contained in:
parent
c8d2e19f4a
commit
7ccb9d7b69
2 changed files with 119 additions and 1 deletions
|
@ -477,6 +477,124 @@ Deno.test({
|
|||
}
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: 'allow overriding prechecks',
|
||||
permissions: {
|
||||
env: true,
|
||||
read: true,
|
||||
write: true,
|
||||
net: true
|
||||
},
|
||||
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
|
||||
fn: async () => {
|
||||
let test_server_info: EPHEMERAL_SERVER | null = null;
|
||||
const cwd = Deno.cwd();
|
||||
|
||||
const static_file_handler = await import('../handlers/static.ts');
|
||||
const PREVIOUS_PRECHECKS = static_file_handler.PRECHECKS.PUT?.slice(0);
|
||||
try {
|
||||
Deno.chdir('./tests/www');
|
||||
test_server_info = await get_ephemeral_listen_server();
|
||||
|
||||
const PRECHECKS = static_file_handler.PRECHECKS.PUT = PREVIOUS_PRECHECKS ? [...PREVIOUS_PRECHECKS] : [];
|
||||
PRECHECKS.push((request: Request): Response | undefined => {
|
||||
const has_required_header = request.headers.get('x-required-header');
|
||||
|
||||
if (!has_required_header) {
|
||||
return Response.json({
|
||||
error: {
|
||||
message: 'Missing required header.',
|
||||
cause: 'missing_required_header'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const failed_put_body = new FormData();
|
||||
failed_put_body.append(
|
||||
'file',
|
||||
new File(['this is the test content for precheck overrides'], 'test_put_upload_that_should_fail.txt')
|
||||
);
|
||||
const failed_put_response = await fetch(
|
||||
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_fail.txt`,
|
||||
{
|
||||
method: 'PUT',
|
||||
body: failed_put_body
|
||||
}
|
||||
);
|
||||
|
||||
asserts.assert(!failed_put_response.ok);
|
||||
|
||||
const failed_body = await failed_put_response.json();
|
||||
asserts.assert(failed_body);
|
||||
asserts.assert(failed_body.error);
|
||||
asserts.assertEquals(failed_body.error.cause, 'missing_required_header');
|
||||
|
||||
const successful_put_body = new FormData();
|
||||
successful_put_body.append(
|
||||
'file',
|
||||
new File(['this is the test content for precheck overrides'], 'test_put_upload_that_should_not_fail.txt')
|
||||
);
|
||||
const successful_put_response = await fetch(
|
||||
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_not_fail.txt`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'x-required-header': 'true'
|
||||
},
|
||||
body: successful_put_body
|
||||
}
|
||||
);
|
||||
|
||||
asserts.assert(successful_put_response.ok);
|
||||
|
||||
const successful_body = await successful_put_response.json();
|
||||
asserts.assert(successful_body);
|
||||
asserts.assert(successful_body.written);
|
||||
asserts.assertEquals(
|
||||
successful_body.written[0],
|
||||
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_not_fail.txt`
|
||||
);
|
||||
|
||||
const get_response = await fetch(
|
||||
`http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_not_fail.txt`,
|
||||
{
|
||||
method: 'GET'
|
||||
}
|
||||
);
|
||||
|
||||
asserts.assert(get_response.ok);
|
||||
asserts.assert(get_response.body);
|
||||
|
||||
const local_download_path = path.join(Deno.cwd(), 'files', 'test_put_upload_that_should_not_fail.txt-downloaded');
|
||||
await ensureFile(local_download_path);
|
||||
|
||||
const file = await Deno.open(local_download_path, { truncate: true, write: true });
|
||||
await get_response.body.pipeTo(file.writable);
|
||||
|
||||
const download_content = await Deno.readTextFile(local_download_path);
|
||||
asserts.assert(download_content);
|
||||
asserts.assertEquals(download_content, 'this is the test content for precheck overrides');
|
||||
|
||||
await Deno.remove(local_download_path);
|
||||
asserts.assert(!fs.existsSync(local_download_path));
|
||||
} finally {
|
||||
if (PREVIOUS_PRECHECKS) {
|
||||
static_file_handler.PRECHECKS.PUT = PREVIOUS_PRECHECKS;
|
||||
}
|
||||
Deno.chdir(cwd);
|
||||
if (test_server_info) {
|
||||
await test_server_info?.server?.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: 'these methods should not work on static files',
|
||||
permissions: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue