blob: 6e15a615cc0cf711f574acc7287a81b9b1a6174d [file] [log] [blame]
Akron354f4532021-01-21 17:29:44 +01001use Mojolicious::Lite;
2use Test::Mojo;
3use Test::More;
4
5my $t = Test::Mojo->new;
6
7plugin 'Kalamar::Plugin::CSP' => {
8 'style-src' => ['self','unsafe-inline'],
9 'script-src' => '*',
10 'img-src' => ['self', 'data:']
11};
12
13get '/' => sub {
14 shift->render(text => 'hello world');
15};
16
17my $csp = 'Content-Security-Policy';
18
19$t->get_ok('/')
20 ->status_is(200)
21 ->content_is('hello world')
22 ->header_is($csp, "img-src 'self' data:;script-src *;style-src 'self' 'unsafe-inline';")
23 ;
24
25$t->app->csp->add('img-src' => 'stats.ids-mannheim.de');
26
27$t->get_ok('/')
28 ->status_is(200)
29 ->content_is('hello world')
30 ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';")
31 ;
32
33$t->get_ok('/')
34 ->status_is(200)
35 ->content_is('hello world')
36 ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';")
37 ;
38
39$t->app->csp->add('img-src' => 'stats.ids-mannheim.de');
40
41$t->get_ok('/')
42 ->status_is(200)
43 ->content_is('hello world')
44 ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';")
45 ;
46
47$t->app->csp->add('script-src' => '*');
48
49$t->get_ok('/')
50 ->status_is(200)
51 ->content_is('hello world')
52 ->header_is($csp, "img-src 'self' data: stats.ids-mannheim.de;script-src *;style-src 'self' 'unsafe-inline';")
53 ;
54
55
56# New
57$t = Test::Mojo->new;
58$t->app->config(
59 CSP => {
60 'style-src' => ['self','unsafe-inline'],
61 'img-src' => ['self', 'data:']
62 }
63);
64
65$t->app->plugin('Kalamar::Plugin::CSP' => {
66 'script-src' => '*',
67 'img-src' => 'self'
68});
69
70$t->app->routes->get('/n')->to(
71 cb => sub {
72 shift->render(text => 'hello world');
73 }
74);
75
76$t->get_ok('/n')
77 ->status_is(200)
78 ->content_is('hello world')
79 ->header_is($csp, "img-src 'self' data:;script-src *;style-src 'self' 'unsafe-inline';")
80 ;
81
82
83done_testing;
84__END__