Skip to content

Commit

Permalink
mozillaGH-72 Add tests for FormattedPolicyCSPMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanYoung committed May 25, 2022
1 parent 71bb768 commit aa1fa13
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions csp/tests/test_contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
from django.test import RequestFactory
from django.test.utils import override_settings

from csp.contrib.formatted_policy import FormattedPolicyCSPMiddleware
from csp.contrib.rate_limiting import RateLimitedCSPMiddleware
from csp.tests.utils import response


HEADER = 'Content-Security-Policy'
mw = RateLimitedCSPMiddleware(response())
rate_limited_mw = RateLimitedCSPMiddleware(response())
formatted_mw = FormattedPolicyCSPMiddleware(response())
rf = RequestFactory()


Expand All @@ -17,8 +19,16 @@ def test_report_percentage():
for _ in range(5000):
request = rf.get('/')
response = HttpResponse()
mw.process_response(request, response)
rate_limited_mw.process_response(request, response)
if 'report-uri' in response[HEADER]:
times_seen += 1
# Roughly 10%
assert 400 <= times_seen <= 600


@override_legacy_settings(CSP_DEFAULT_SRC=["{host}"])
def test_formatted_policy():
request = rf.get('/', HTTP_HOST='example.com')
response = HttpResponse()
formatted_mw.process_response(request, response)
assert response[HEADER] == 'default-src example.com'

0 comments on commit aa1fa13

Please sign in to comment.