Developer-Dashboard
view release on metacpan or search on metacpan
t/web_app_static_files.t view on Meta::CPAN
# Test: _get_content_type for PNG
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'image.png');
is($ct, 'image/png', 'PNG content type correct');
}
# Test: _get_content_type for JPEG
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'photo.jpg');
is($ct, 'image/jpeg', 'JPEG content type correct');
}
# Test: _get_content_type for GIF
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'animation.gif');
is($ct, 'image/gif', 'GIF content type correct');
}
# Test: _get_content_type for WebP
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'image.webp');
is($ct, 'image/webp', 'WebP content type correct');
}
# Test: _get_content_type for SVG
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'icon.svg');
is($ct, 'image/svg+xml', 'SVG content type correct');
}
# Test: _get_content_type for ICO
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'favicon.ico');
is($ct, 'image/x-icon', 'ICO content type correct');
}
# Test: _get_content_type for HTML
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'index.html');
is($ct, 'text/html; charset=utf-8', 'HTML content type correct');
}
# Test: _get_content_type for unknown
{
my $app = create_mock_app();
my $ct = $app->_get_content_type('others', 'unknown.xyz');
is($ct, 'application/octet-stream', 'Unknown content type is octet-stream');
}
# Test: _serve_static_file with directory traversal (security)
{
my $app = create_mock_app();
my $response = $app->_serve_static_file('js', '../../../etc/passwd');
is($response->[0], 400, 'Directory traversal blocked');
}
# Test: _serve_static_file with nonexistent file
{
my $app = create_mock_app();
my $response = $app->_serve_static_file('js', 'nonexistent.js');
is($response->[0], 404, 'Nonexistent file returns 404');
}
# Test: built-in jquery shim route exists for bookmark compatibility
{
local $ENV{HOME} = tempdir(CLEANUP => 1);
my $paths = Developer::Dashboard::PathRegistry->new(home => $ENV{HOME});
my $store = Developer::Dashboard::PageStore->new(paths => $paths);
my $app = create_mock_app( pages => $store );
my $response = $app->jquery_js_response();
is($response->[0], 200, 'built-in jquery shim returns 200');
is($response->[1], 'application/javascript; charset=utf-8', 'built-in jquery shim returns javascript content type');
like($response->[2], qr/window\.jQuery = \$;/, 'built-in jquery shim exposes window.jQuery');
like($response->[2], qr/\$\.ajax = function/, 'built-in jquery shim exposes ajax support');
}
# Test: static_file_response routes the jquery compatibility alias through the built-in shim
{
local $ENV{HOME} = tempdir(CLEANUP => 1);
my $paths = Developer::Dashboard::PathRegistry->new(home => $ENV{HOME});
my $store = Developer::Dashboard::PageStore->new(paths => $paths);
my $app = create_mock_app( pages => $store );
my $response = $app->static_file_response( type => 'js', file => 'jquery-4.0.0.min.js' );
is($response->[0], 200, 'jquery compatibility alias returns 200');
like($response->[1], qr/application\/javascript/, 'jquery compatibility alias returns javascript content');
like($response->[2], qr/window\.jQuery = \$;/, 'jquery compatibility alias reuses the built-in jquery shim');
}
# Test: _serve_static_file resolves runtime-root dashboard/public assets
{
local $ENV{HOME} = tempdir(CLEANUP => 1);
my $paths = Developer::Dashboard::PathRegistry->new(home => $ENV{HOME});
my $store = Developer::Dashboard::PageStore->new(paths => $paths);
my $public_dir = File::Spec->catdir( $paths->runtime_root, 'dashboard', 'public', 'js' );
mkdir File::Spec->catdir( $paths->runtime_root, 'dashboard' ) or die $! if !-d File::Spec->catdir( $paths->runtime_root, 'dashboard' );
mkdir File::Spec->catdir( $paths->runtime_root, 'dashboard', 'public' ) or die $! if !-d File::Spec->catdir( $paths->runtime_root, 'dashboard', 'public' );
mkdir $public_dir or die $! if !-d $public_dir;
my $test_file = File::Spec->catfile($public_dir, 'test.js');
open my $fh, '>', $test_file or die "Cannot create test file: $!";
print {$fh} 'console.log("runtime");';
close $fh;
my $app = create_mock_app( pages => $store );
my $response = $app->_serve_static_file('js', 'test.js');
is($response->[0], 200, 'runtime public file returns 200');
is($response->[1], 'application/javascript; charset=utf-8', 'runtime public file keeps javascript content type');
is($response->[2], 'console.log("runtime");', 'runtime public file content is served');
}
# Test: _serve_static_file resolves dashboards/public assets
{
local $ENV{HOME} = tempdir(CLEANUP => 1);
my $paths = Developer::Dashboard::PathRegistry->new(home => $ENV{HOME});
( run in 1.258 second using v1.01-cache-2.11-cpan-39bf76dae61 )