@@ -25,7 +25,7 @@ class AssetTagHelperTest < ActionView::TestCase
25
25
attr_reader :request , :response
26
26
27
27
class FakeRequest
28
- attr_accessor :script_name
28
+ attr_accessor :script_name , :content_security_policy_nonce_directives
29
29
def protocol ( ) "http://" end
30
30
def ssl? ( ) false end
31
31
def host_with_port ( ) "localhost" end
@@ -724,6 +724,27 @@ def test_should_set_preload_links_with_integrity_hashes
724
724
end
725
725
end
726
726
727
+ def test_should_set_preload_links_with_nonce
728
+ @request . content_security_policy_nonce_directives = %w( script-src )
729
+ with_preload_links_header do
730
+ preload_link_tag ( "http://example.com/preload.js" )
731
+ stylesheet_link_tag ( "http://example.com/style.css" , nonce : true )
732
+ javascript_include_tag ( "http://example.com/all.js" , nonce : true )
733
+ expected = "<http://example.com/preload.js>; rel=preload; as=script; type=text/javascript; nonce=iyhD0Yc0W+c=,<http://example.com/style.css>; rel=preload; as=style; nonce=iyhD0Yc0W+c=; nopush,<http://example.com/all.js>; rel=preload; as=script; nonce=iyhD0Yc0W+c=; nopush"
734
+ assert_equal expected , @response . headers [ "link" ]
735
+ end
736
+ end
737
+
738
+ def test_should_set_preload_link_tag_nonce_if_listed_in_csp_directives
739
+ @request . content_security_policy_nonce_directives = %w( script-src )
740
+ assert_equal %(<link rel="preload" href="/application.js" as="script" type="text/javascript" nonce="iyhD0Yc0W+c=">) , preload_link_tag ( "/application.js" )
741
+ assert_equal %(<link rel="preload" href="/style.css" as="style" type="text/css">) , preload_link_tag ( "/style.css" )
742
+
743
+ @request . content_security_policy_nonce_directives = %w( style-src )
744
+ assert_equal %(<link rel="preload" href="/application.js" as="script" type="text/javascript">) , preload_link_tag ( "/application.js" )
745
+ assert_equal %(<link rel="preload" href="/style.css" as="style" type="text/css" nonce="iyhD0Yc0W+c=">) , preload_link_tag ( "/style.css" )
746
+ end
747
+
727
748
def test_should_not_preload_links_when_disabled
728
749
with_preload_links_header ( false ) do
729
750
stylesheet_link_tag ( "http://example.com/style.css" )
0 commit comments