@@ -25,18 +25,36 @@ async def app(scope, receive, send):
2525
2626class TestCSPMiddleware (TestCase ):
2727 def test_headers (self ):
28+ """
29+ Make sure the headers are added.
30+ """
2831 wrapped_app = CSPMiddleware (app )
2932
3033 client = TestClient (wrapped_app )
3134 response = client .request ("GET" , "/" )
3235
33- header_names = response .headers .keys ()
34-
3536 # Make sure the headers got added:
36- self .assertIn ("content-security-policy" , header_names )
37+ self .assertEqual (
38+ response .headers ["content-security-policy" ],
39+ "default-src: 'self'" ,
40+ )
3741
3842 # Make sure the original headers are still intact:
39- self .assertIn ("content-type" , header_names )
43+ self .assertEqual (response .headers ["content-type" ], "text/plain" )
44+
45+ def test_default_src (self ):
46+ """
47+ Make sure the `default-src` value can be set.
48+ """
49+ wrapped_app = CSPMiddleware (app , config = CSPConfig (default_src = "none" ))
50+
51+ client = TestClient (wrapped_app )
52+ response = client .request ("GET" , "/" )
53+
54+ self .assertEqual (
55+ response .headers .get ("content-security-policy" ),
56+ "default-src: 'none'" ,
57+ )
4058
4159 def test_report_uri (self ):
4260 wrapped_app = CSPMiddleware (
@@ -46,5 +64,7 @@ def test_report_uri(self):
4664 client = TestClient (wrapped_app )
4765 response = client .request ("GET" , "/" )
4866
49- header = response .headers ["content-security-policy" ]
50- self .assertIn ("report-uri" , header )
67+ self .assertEqual (
68+ response .headers ["content-security-policy" ],
69+ "default-src: 'self'; report-uri foo.com" ,
70+ )
0 commit comments