1
1
require "test_helper"
2
2
3
3
class Importmap ::ImportmapTagsHelperTest < ActionView ::TestCase
4
+ attr_reader :request
5
+
6
+ class FakeRequest
7
+ def initialize ( nonce = nil )
8
+ @nonce = nonce
9
+ end
10
+
11
+ def send_early_hints ( links ) ; end
12
+
13
+ def content_security_policy
14
+ Object . new if @nonce
15
+ end
16
+
17
+ def content_security_policy_nonce
18
+ @nonce
19
+ end
20
+ end
21
+
4
22
test "javascript_importmap_tags with and without shim" do
5
23
assert_match /shim/ , javascript_importmap_tags ( "application" )
6
24
assert_no_match /shim/ , javascript_importmap_tags ( "application" , shim : false )
@@ -17,4 +35,24 @@ class Importmap::ImportmapTagsHelperTest < ActionView::TestCase
17
35
%(<link rel="modulepreload" href="https://cdn.skypack.dev/md5">) ,
18
36
javascript_importmap_module_preload_tags
19
37
end
38
+
39
+ test "tags have no nonce if CSP is not configured" do
40
+ @request = FakeRequest . new
41
+
42
+ assert_no_match /nonce/ , javascript_importmap_tags ( "application" )
43
+ ensure
44
+ @request = nil
45
+ end
46
+
47
+ test "tags have nonce if CSP is configured" do
48
+ @request = FakeRequest . new ( "iyhD0Yc0W+c=" )
49
+
50
+ assert_match /nonce="iyhD0Yc0W\+ c="/ , javascript_inline_importmap_tag
51
+ assert_match /nonce="iyhD0Yc0W\+ c="/ , javascript_importmap_shim_nonce_configuration_tag
52
+ assert_match /nonce="iyhD0Yc0W\+ c="/ , javascript_importmap_shim_tag
53
+ assert_match /nonce="iyhD0Yc0W\+ c="/ , javascript_import_module_tag ( "application" )
54
+ assert_match /nonce="iyhD0Yc0W\+ c="/ , javascript_importmap_module_preload_tags
55
+ ensure
56
+ @request = nil
57
+ end
20
58
end
0 commit comments