mirror of https://github.com/django/django.git
Refs #34074 -- Used headers argument for RequestFactory and Client in docs and tests.
This commit is contained in:
parent
0d3ccf7512
commit
99bd5fb4c2
|
@ -152,7 +152,10 @@ example, the test suite for docs.djangoproject.com includes the following::
|
|||
|
||||
class SearchFormTestCase(TestCase):
|
||||
def test_empty_get(self):
|
||||
response = self.client.get('/en/dev/search/', HTTP_HOST='docs.djangoproject.dev:8000')
|
||||
response = self.client.get(
|
||||
"/en/dev/search/",
|
||||
headers={"host": "docs.djangoproject.dev:8000"},
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
and the settings file includes a list of the domains supported by the project::
|
||||
|
|
|
@ -682,7 +682,7 @@ a name of :setting:`LANGUAGE_COOKIE_NAME` and a value of the language code::
|
|||
or by including the ``Accept-Language`` HTTP header in the request::
|
||||
|
||||
def test_language_using_header(self):
|
||||
response = self.client.get('/', HTTP_ACCEPT_LANGUAGE='fr')
|
||||
response = self.client.get("/", headers={"accept-language": "fr"})
|
||||
self.assertEqual(response.content, b"Bienvenue sur mon site.")
|
||||
|
||||
More details are in :ref:`how-django-discovers-language-preference`.
|
||||
|
|
|
@ -214,7 +214,7 @@ class PasswordResetTest(AuthViewsTestCase):
|
|||
response = self.client.post(
|
||||
"/password_reset/",
|
||||
{"email": "staffmember@example.com"},
|
||||
HTTP_HOST="www.example:dr.frankenstein@evil.tld",
|
||||
headers={"host": "www.example:dr.frankenstein@evil.tld"},
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertEqual(len(mail.outbox), 0)
|
||||
|
@ -227,7 +227,7 @@ class PasswordResetTest(AuthViewsTestCase):
|
|||
response = self.client.post(
|
||||
"/admin_password_reset/",
|
||||
{"email": "staffmember@example.com"},
|
||||
HTTP_HOST="www.example:dr.frankenstein@evil.tld",
|
||||
headers={"host": "www.example:dr.frankenstein@evil.tld"},
|
||||
)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertEqual(len(mail.outbox), 0)
|
||||
|
|
|
@ -2004,7 +2004,7 @@ class CacheUtils(SimpleTestCase):
|
|||
|
||||
host = "www.example.com"
|
||||
path = "/cache/test/"
|
||||
factory = RequestFactory(HTTP_HOST=host)
|
||||
factory = RequestFactory(headers={"host": host})
|
||||
|
||||
def tearDown(self):
|
||||
cache.clear()
|
||||
|
@ -2095,9 +2095,9 @@ class CacheUtils(SimpleTestCase):
|
|||
"""
|
||||
get_cache_key keys differ by fully-qualified URL instead of path
|
||||
"""
|
||||
request1 = self.factory.get(self.path, HTTP_HOST="sub-1.example.com")
|
||||
request1 = self.factory.get(self.path, headers={"host": "sub-1.example.com"})
|
||||
learn_cache_key(request1, HttpResponse())
|
||||
request2 = self.factory.get(self.path, HTTP_HOST="sub-2.example.com")
|
||||
request2 = self.factory.get(self.path, headers={"host": "sub-2.example.com"})
|
||||
learn_cache_key(request2, HttpResponse())
|
||||
self.assertNotEqual(get_cache_key(request1), get_cache_key(request2))
|
||||
|
||||
|
|
|
@ -252,29 +252,39 @@ class URLRedirectTests(URLTestCaseBase):
|
|||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_en_redirect(self):
|
||||
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="en")
|
||||
response = self.client.get(
|
||||
"/account/register/", headers={"accept-language": "en"}
|
||||
)
|
||||
self.assertRedirects(response, "/en/account/register/")
|
||||
|
||||
response = self.client.get(response.headers["location"])
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_en_redirect_wrong_url(self):
|
||||
response = self.client.get("/profiel/registreren/", HTTP_ACCEPT_LANGUAGE="en")
|
||||
response = self.client.get(
|
||||
"/profiel/registreren/", headers={"accept-language": "en"}
|
||||
)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_nl_redirect(self):
|
||||
response = self.client.get("/profiel/registreren/", HTTP_ACCEPT_LANGUAGE="nl")
|
||||
response = self.client.get(
|
||||
"/profiel/registreren/", headers={"accept-language": "nl"}
|
||||
)
|
||||
self.assertRedirects(response, "/nl/profiel/registreren/")
|
||||
|
||||
response = self.client.get(response.headers["location"])
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_nl_redirect_wrong_url(self):
|
||||
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="nl")
|
||||
response = self.client.get(
|
||||
"/account/register/", headers={"accept-language": "nl"}
|
||||
)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_pt_br_redirect(self):
|
||||
response = self.client.get("/conta/registre-se/", HTTP_ACCEPT_LANGUAGE="pt-br")
|
||||
response = self.client.get(
|
||||
"/conta/registre-se/", headers={"accept-language": "pt-br"}
|
||||
)
|
||||
self.assertRedirects(response, "/pt-br/conta/registre-se/")
|
||||
|
||||
response = self.client.get(response.headers["location"])
|
||||
|
@ -282,7 +292,9 @@ class URLRedirectTests(URLTestCaseBase):
|
|||
|
||||
def test_pl_pl_redirect(self):
|
||||
# language from outside of the supported LANGUAGES list
|
||||
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="pl-pl")
|
||||
response = self.client.get(
|
||||
"/account/register/", headers={"accept-language": "pl-pl"}
|
||||
)
|
||||
self.assertRedirects(response, "/en/account/register/")
|
||||
|
||||
response = self.client.get(response.headers["location"])
|
||||
|
@ -295,7 +307,9 @@ class URLRedirectTests(URLTestCaseBase):
|
|||
],
|
||||
)
|
||||
def test_custom_redirect_class(self):
|
||||
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="en")
|
||||
response = self.client.get(
|
||||
"/account/register/", headers={"accept-language": "en"}
|
||||
)
|
||||
self.assertRedirects(response, "/en/account/register/", 301)
|
||||
|
||||
|
||||
|
@ -314,7 +328,9 @@ class URLVaryAcceptLanguageTests(URLTestCaseBase):
|
|||
The redirect to a prefixed URL depends on 'Accept-Language' and
|
||||
'Cookie', but once prefixed no header is set.
|
||||
"""
|
||||
response = self.client.get("/account/register/", HTTP_ACCEPT_LANGUAGE="en")
|
||||
response = self.client.get(
|
||||
"/account/register/", headers={"accept-language": "en"}
|
||||
)
|
||||
self.assertRedirects(response, "/en/account/register/")
|
||||
self.assertEqual(response.get("Vary"), "Accept-Language, Cookie")
|
||||
|
||||
|
@ -330,19 +346,19 @@ class URLRedirectWithoutTrailingSlashTests(URLTestCaseBase):
|
|||
"""
|
||||
|
||||
def test_not_prefixed_redirect(self):
|
||||
response = self.client.get("/not-prefixed", HTTP_ACCEPT_LANGUAGE="en")
|
||||
response = self.client.get("/not-prefixed", headers={"accept-language": "en"})
|
||||
self.assertRedirects(response, "/not-prefixed/", 301)
|
||||
|
||||
def test_en_redirect(self):
|
||||
response = self.client.get(
|
||||
"/account/register", HTTP_ACCEPT_LANGUAGE="en", follow=True
|
||||
"/account/register", headers={"accept-language": "en"}, follow=True
|
||||
)
|
||||
# We only want one redirect, bypassing CommonMiddleware
|
||||
self.assertEqual(response.redirect_chain, [("/en/account/register/", 302)])
|
||||
self.assertRedirects(response, "/en/account/register/", 302)
|
||||
|
||||
response = self.client.get(
|
||||
"/prefixed.xml", HTTP_ACCEPT_LANGUAGE="en", follow=True
|
||||
"/prefixed.xml", headers={"accept-language": "en"}, follow=True
|
||||
)
|
||||
self.assertRedirects(response, "/en/prefixed.xml", 302)
|
||||
|
||||
|
@ -355,13 +371,13 @@ class URLRedirectWithoutTrailingSlashSettingTests(URLTestCaseBase):
|
|||
|
||||
@override_settings(APPEND_SLASH=False)
|
||||
def test_not_prefixed_redirect(self):
|
||||
response = self.client.get("/not-prefixed", HTTP_ACCEPT_LANGUAGE="en")
|
||||
response = self.client.get("/not-prefixed", headers={"accept-language": "en"})
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
@override_settings(APPEND_SLASH=False)
|
||||
def test_en_redirect(self):
|
||||
response = self.client.get(
|
||||
"/account/register-without-slash", HTTP_ACCEPT_LANGUAGE="en"
|
||||
"/account/register-without-slash", headers={"accept-language": "en"}
|
||||
)
|
||||
self.assertRedirects(response, "/en/account/register-without-slash", 302)
|
||||
|
||||
|
@ -424,7 +440,7 @@ class URLRedirectWithScriptAliasTests(URLTestCaseBase):
|
|||
prefix = "/script_prefix"
|
||||
with override_script_prefix(prefix):
|
||||
response = self.client.get(
|
||||
"/prefixed/", HTTP_ACCEPT_LANGUAGE="en", SCRIPT_NAME=prefix
|
||||
"/prefixed/", headers={"accept-language": "en"}, SCRIPT_NAME=prefix
|
||||
)
|
||||
self.assertRedirects(
|
||||
response, "%s/en/prefixed/" % prefix, target_status_code=404
|
||||
|
|
|
@ -1778,7 +1778,7 @@ class MiscTests(SimpleTestCase):
|
|||
]
|
||||
for header, expected in tests:
|
||||
with self.subTest(header=header):
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE=header)
|
||||
request = self.rf.get("/", headers={"accept-language": header})
|
||||
self.assertEqual(get_language_from_request(request), expected)
|
||||
|
||||
@override_settings(
|
||||
|
@ -1799,10 +1799,10 @@ class MiscTests(SimpleTestCase):
|
|||
refs #18419 -- this is explicitly for browser compatibility
|
||||
"""
|
||||
g = get_language_from_request
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="zh-cn,en")
|
||||
request = self.rf.get("/", headers={"accept-language": "zh-cn,en"})
|
||||
self.assertEqual(g(request), "zh-hans")
|
||||
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="zh-tw,en")
|
||||
request = self.rf.get("/", headers={"accept-language": "zh-tw,en"})
|
||||
self.assertEqual(g(request), "zh-hant")
|
||||
|
||||
def test_special_fallback_language(self):
|
||||
|
@ -1810,7 +1810,7 @@ class MiscTests(SimpleTestCase):
|
|||
Some languages may have special fallbacks that don't follow the simple
|
||||
'fr-ca' -> 'fr' logic (notably Chinese codes).
|
||||
"""
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="zh-my,en")
|
||||
request = self.rf.get("/", headers={"accept-language": "zh-my,en"})
|
||||
self.assertEqual(get_language_from_request(request), "zh-hans")
|
||||
|
||||
def test_subsequent_code_fallback_language(self):
|
||||
|
@ -1828,7 +1828,7 @@ class MiscTests(SimpleTestCase):
|
|||
]
|
||||
for value, expected in tests:
|
||||
with self.subTest(value=value):
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE=f"{value},en")
|
||||
request = self.rf.get("/", headers={"accept-language": f"{value},en"})
|
||||
self.assertEqual(get_language_from_request(request), expected)
|
||||
|
||||
def test_parse_language_cookie(self):
|
||||
|
@ -1840,7 +1840,7 @@ class MiscTests(SimpleTestCase):
|
|||
request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "pt"
|
||||
self.assertEqual("pt", g(request))
|
||||
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="de")
|
||||
request = self.rf.get("/", headers={"accept-language": "de"})
|
||||
request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "es"
|
||||
self.assertEqual("es", g(request))
|
||||
|
||||
|
@ -1854,7 +1854,7 @@ class MiscTests(SimpleTestCase):
|
|||
# translation to variation (zh-hans) the user sets zh-hans as the
|
||||
# preferred language, it should be selected without falling back nor
|
||||
# ignoring it.
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="de")
|
||||
request = self.rf.get("/", headers={"accept-language": "de"})
|
||||
request.COOKIES[settings.LANGUAGE_COOKIE_NAME] = "zh-hans"
|
||||
self.assertEqual(g(request), "zh-hans")
|
||||
|
||||
|
@ -1957,7 +1957,7 @@ class MiscTests(SimpleTestCase):
|
|||
previously valid should not be used (#14170).
|
||||
"""
|
||||
g = get_language_from_request
|
||||
request = self.rf.get("/", HTTP_ACCEPT_LANGUAGE="pt-br")
|
||||
request = self.rf.get("/", headers={"accept-language": "pt-br"})
|
||||
self.assertEqual("pt-br", g(request))
|
||||
with self.settings(LANGUAGES=[("en", "English")]):
|
||||
self.assertNotEqual("pt-br", g(request))
|
||||
|
@ -2215,13 +2215,13 @@ class CountrySpecificLanguageTests(SimpleTestCase):
|
|||
def test_get_language_from_request(self):
|
||||
# issue 19919
|
||||
request = self.rf.get(
|
||||
"/", HTTP_ACCEPT_LANGUAGE="en-US,en;q=0.8,bg;q=0.6,ru;q=0.4"
|
||||
"/", headers={"accept-language": "en-US,en;q=0.8,bg;q=0.6,ru;q=0.4"}
|
||||
)
|
||||
lang = get_language_from_request(request)
|
||||
self.assertEqual("en-us", lang)
|
||||
|
||||
request = self.rf.get(
|
||||
"/", HTTP_ACCEPT_LANGUAGE="bg-bg,en-US;q=0.8,en;q=0.6,ru;q=0.4"
|
||||
"/", headers={"accept-language": "bg-bg,en-US;q=0.8,en;q=0.6,ru;q=0.4"}
|
||||
)
|
||||
lang = get_language_from_request(request)
|
||||
self.assertEqual("bg", lang)
|
||||
|
@ -2233,13 +2233,13 @@ class CountrySpecificLanguageTests(SimpleTestCase):
|
|||
def test_specific_language_codes(self):
|
||||
# issue 11915
|
||||
request = self.rf.get(
|
||||
"/", HTTP_ACCEPT_LANGUAGE="pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"
|
||||
"/", headers={"accept-language": "pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"}
|
||||
)
|
||||
lang = get_language_from_request(request)
|
||||
self.assertEqual("pt-br", lang)
|
||||
|
||||
request = self.rf.get(
|
||||
"/", HTTP_ACCEPT_LANGUAGE="pt-pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"
|
||||
"/", headers={"accept-language": "pt-pt,en-US;q=0.8,en;q=0.6,ru;q=0.4"}
|
||||
)
|
||||
lang = get_language_from_request(request)
|
||||
self.assertEqual("pt-br", lang)
|
||||
|
|
|
@ -419,14 +419,14 @@ class AdminEmailHandlerTest(SimpleTestCase):
|
|||
# Text email
|
||||
admin_email_handler.include_html = False
|
||||
try:
|
||||
self.client.get("/", HTTP_HOST="evil.com")
|
||||
self.client.get("/", headers={"host": "evil.com"})
|
||||
finally:
|
||||
admin_email_handler.include_html = old_include_html
|
||||
|
||||
# HTML email
|
||||
admin_email_handler.include_html = True
|
||||
try:
|
||||
self.client.get("/", HTTP_HOST="evil.com")
|
||||
self.client.get("/", headers={"host": "evil.com"})
|
||||
finally:
|
||||
admin_email_handler.include_html = old_include_html
|
||||
|
||||
|
|
|
@ -690,7 +690,7 @@ class ConditionalGetMiddlewareTest(SimpleTestCase):
|
|||
|
||||
response = ConditionalGetMiddleware(self.get_response)(self.req)
|
||||
etag = response.headers["ETag"]
|
||||
put_request = self.request_factory.put("/", HTTP_IF_MATCH=etag)
|
||||
put_request = self.request_factory.put("/", headers={"if-match": etag})
|
||||
conditional_get_response = ConditionalGetMiddleware(get_200_response)(
|
||||
put_request
|
||||
)
|
||||
|
@ -1061,7 +1061,7 @@ class ETagGZipMiddlewareTest(SimpleTestCase):
|
|||
response.headers["ETag"] = '"eggs"'
|
||||
return response
|
||||
|
||||
request = self.rf.get("/", HTTP_ACCEPT_ENCODING="gzip, deflate")
|
||||
request = self.rf.get("/", headers={"accept-encoding": "gzip, deflate"})
|
||||
gzip_response = GZipMiddleware(get_response)(request)
|
||||
self.assertEqual(gzip_response.headers["ETag"], 'W/"eggs"')
|
||||
|
||||
|
@ -1075,7 +1075,7 @@ class ETagGZipMiddlewareTest(SimpleTestCase):
|
|||
response.headers["ETag"] = 'W/"eggs"'
|
||||
return response
|
||||
|
||||
request = self.rf.get("/", HTTP_ACCEPT_ENCODING="gzip, deflate")
|
||||
request = self.rf.get("/", headers={"accept-encoding": "gzip, deflate"})
|
||||
gzip_response = GZipMiddleware(get_response)(request)
|
||||
self.assertEqual(gzip_response.headers["ETag"], 'W/"eggs"')
|
||||
|
||||
|
@ -1090,11 +1090,12 @@ class ETagGZipMiddlewareTest(SimpleTestCase):
|
|||
def get_cond_response(req):
|
||||
return ConditionalGetMiddleware(get_response)(req)
|
||||
|
||||
request = self.rf.get("/", HTTP_ACCEPT_ENCODING="gzip, deflate")
|
||||
request = self.rf.get("/", headers={"accept-encoding": "gzip, deflate"})
|
||||
response = GZipMiddleware(get_cond_response)(request)
|
||||
gzip_etag = response.headers["ETag"]
|
||||
next_request = self.rf.get(
|
||||
"/", HTTP_ACCEPT_ENCODING="gzip, deflate", HTTP_IF_NONE_MATCH=gzip_etag
|
||||
"/",
|
||||
headers={"accept-encoding": "gzip, deflate", "if-none-match": gzip_etag},
|
||||
)
|
||||
next_response = ConditionalGetMiddleware(get_response)(next_request)
|
||||
self.assertEqual(next_response.status_code, 304)
|
||||
|
|
|
@ -1068,8 +1068,10 @@ class RequestFactoryTest(SimpleTestCase):
|
|||
|
||||
def test_request_factory_default_headers(self):
|
||||
request = RequestFactory(
|
||||
HTTP_AUTHORIZATION="Bearer faketoken",
|
||||
HTTP_X_ANOTHER_HEADER="some other value",
|
||||
headers={
|
||||
"authorization": "Bearer faketoken",
|
||||
"x-another-header": "some other value",
|
||||
}
|
||||
).get("/somewhere/")
|
||||
self.assertEqual(request.headers["authorization"], "Bearer faketoken")
|
||||
self.assertIn("HTTP_AUTHORIZATION", request.META)
|
||||
|
@ -1092,8 +1094,10 @@ class RequestFactoryTest(SimpleTestCase):
|
|||
method = getattr(self.request_factory, method_name)
|
||||
request = method(
|
||||
"/somewhere/",
|
||||
HTTP_AUTHORIZATION="Bearer faketoken",
|
||||
HTTP_X_ANOTHER_HEADER="some other value",
|
||||
headers={
|
||||
"authorization": "Bearer faketoken",
|
||||
"x-another-header": "some other value",
|
||||
},
|
||||
)
|
||||
self.assertEqual(request.headers["authorization"], "Bearer faketoken")
|
||||
self.assertIn("HTTP_AUTHORIZATION", request.META)
|
||||
|
|
|
@ -1228,14 +1228,18 @@ class UploadedFileEncodingTest(SimpleTestCase):
|
|||
class RequestHeadersTest(SimpleTestCase):
|
||||
def test_client_headers(self):
|
||||
"A test client can receive custom headers"
|
||||
response = self.client.get("/check_headers/", HTTP_X_ARG_CHECK="Testing 123")
|
||||
response = self.client.get(
|
||||
"/check_headers/", headers={"x-arg-check": "Testing 123"}
|
||||
)
|
||||
self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_client_headers_redirect(self):
|
||||
"Test client headers are preserved through redirects"
|
||||
response = self.client.get(
|
||||
"/check_headers_redirect/", follow=True, HTTP_X_ARG_CHECK="Testing 123"
|
||||
"/check_headers_redirect/",
|
||||
follow=True,
|
||||
headers={"x-arg-check": "Testing 123"},
|
||||
)
|
||||
self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
|
||||
self.assertRedirects(
|
||||
|
|
|
@ -41,7 +41,7 @@ class CsrfViewTests(SimpleTestCase):
|
|||
Referer header is strictly checked for POST over HTTPS. Trigger the
|
||||
exception by sending an incorrect referer.
|
||||
"""
|
||||
response = self.client.post("/", HTTP_X_FORWARDED_PROTO="https")
|
||||
response = self.client.post("/", headers={"x-forwarded-proto": "https"})
|
||||
self.assertContains(
|
||||
response,
|
||||
"You are seeing this message because this HTTPS site requires a "
|
||||
|
|
|
@ -237,7 +237,7 @@ class DebugViewTests(SimpleTestCase):
|
|||
html=True,
|
||||
)
|
||||
with self.assertLogs("django.request", "ERROR"):
|
||||
response = self.client.get("/raises500/", HTTP_ACCEPT="text/plain")
|
||||
response = self.client.get("/raises500/", headers={"accept": "text/plain"})
|
||||
self.assertContains(
|
||||
response,
|
||||
"Raised during: view_tests.views.raises500",
|
||||
|
@ -254,7 +254,9 @@ class DebugViewTests(SimpleTestCase):
|
|||
html=True,
|
||||
)
|
||||
with self.assertLogs("django.request", "ERROR"):
|
||||
response = self.client.get("/classbased500/", HTTP_ACCEPT="text/plain")
|
||||
response = self.client.get(
|
||||
"/classbased500/", headers={"accept": "text/plain"}
|
||||
)
|
||||
self.assertContains(
|
||||
response,
|
||||
"Raised during: view_tests.views.Raises500View",
|
||||
|
@ -432,7 +434,7 @@ class DebugViewTests(SimpleTestCase):
|
|||
)
|
||||
|
||||
with self.assertLogs("django.request", "ERROR"):
|
||||
response = self.client.get("/raises500/", HTTP_ACCEPT="text/plain")
|
||||
response = self.client.get("/raises500/", headers={"accept": "text/plain"})
|
||||
self.assertContains(response, "Oh dear, an error occurred!", status_code=500)
|
||||
|
||||
|
||||
|
@ -1116,7 +1118,7 @@ class ExceptionReporterTests(SimpleTestCase):
|
|||
@override_settings(ALLOWED_HOSTS="example.com")
|
||||
def test_disallowed_host(self):
|
||||
"An exception report can be generated even for a disallowed host."
|
||||
request = self.rf.get("/", HTTP_HOST="evil.com")
|
||||
request = self.rf.get("/", headers={"host": "evil.com"})
|
||||
reporter = ExceptionReporter(request, None, None, None)
|
||||
html = reporter.get_traceback_html()
|
||||
self.assertIn("http://evil.com/", html)
|
||||
|
@ -1201,7 +1203,7 @@ class ExceptionReporterTests(SimpleTestCase):
|
|||
|
||||
@override_settings(ALLOWED_HOSTS=["example.com"])
|
||||
def test_get_raw_insecure_uri(self):
|
||||
factory = RequestFactory(HTTP_HOST="evil.com")
|
||||
factory = RequestFactory(headers={"host": "evil.com"})
|
||||
tests = [
|
||||
("////absolute-uri", "http://evil.com//absolute-uri"),
|
||||
("/?foo=bar", "http://evil.com/?foo=bar"),
|
||||
|
@ -1326,7 +1328,7 @@ class PlainTextReportTests(SimpleTestCase):
|
|||
@override_settings(ALLOWED_HOSTS="example.com")
|
||||
def test_disallowed_host(self):
|
||||
"An exception report can be generated even for a disallowed host."
|
||||
request = self.rf.get("/", HTTP_HOST="evil.com")
|
||||
request = self.rf.get("/", headers={"host": "evil.com"})
|
||||
reporter = ExceptionReporter(request, None, None, None)
|
||||
text = reporter.get_traceback_text()
|
||||
self.assertIn("http://evil.com/", text)
|
||||
|
@ -1787,7 +1789,7 @@ class ExceptionReporterFilterTests(
|
|||
)
|
||||
|
||||
def test_request_meta_filtering(self):
|
||||
request = self.rf.get("/", HTTP_SECRET_HEADER="super_secret")
|
||||
request = self.rf.get("/", headers={"secret-header": "super_secret"})
|
||||
reporter_filter = SafeExceptionReporterFilter()
|
||||
self.assertEqual(
|
||||
reporter_filter.get_safe_request_meta(request)["HTTP_SECRET_HEADER"],
|
||||
|
@ -1795,12 +1797,13 @@ class ExceptionReporterFilterTests(
|
|||
)
|
||||
|
||||
def test_exception_report_uses_meta_filtering(self):
|
||||
response = self.client.get("/raises500/", HTTP_SECRET_HEADER="super_secret")
|
||||
response = self.client.get(
|
||||
"/raises500/", headers={"secret-header": "super_secret"}
|
||||
)
|
||||
self.assertNotIn(b"super_secret", response.content)
|
||||
response = self.client.get(
|
||||
"/raises500/",
|
||||
HTTP_SECRET_HEADER="super_secret",
|
||||
HTTP_ACCEPT="application/json",
|
||||
headers={"secret-header": "super_secret", "accept": "application/json"},
|
||||
)
|
||||
self.assertNotIn(b"super_secret", response.content)
|
||||
|
||||
|
@ -1863,7 +1866,7 @@ class NonHTMLResponseExceptionReporterFilter(
|
|||
Refs #14614.
|
||||
"""
|
||||
|
||||
rf = RequestFactory(HTTP_ACCEPT="application/json")
|
||||
rf = RequestFactory(headers={"accept": "application/json"})
|
||||
|
||||
def test_non_sensitive_request(self):
|
||||
"""
|
||||
|
@ -1915,7 +1918,9 @@ class NonHTMLResponseExceptionReporterFilter(
|
|||
|
||||
@override_settings(DEBUG=True, ROOT_URLCONF="view_tests.urls")
|
||||
def test_non_html_response_encoding(self):
|
||||
response = self.client.get("/raises500/", HTTP_ACCEPT="application/json")
|
||||
response = self.client.get(
|
||||
"/raises500/", headers={"accept": "application/json"}
|
||||
)
|
||||
self.assertEqual(response.headers["Content-Type"], "text/plain; charset=utf-8")
|
||||
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ class SetLanguageTests(TestCase):
|
|||
lang_code = self._get_inactive_language_code()
|
||||
post_data = {"language": lang_code, "next": "/"}
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", post_data, HTTP_REFERER="/i_should_not_be_used/"
|
||||
"/i18n/setlang/", post_data, headers={"referer": "/i_should_not_be_used/"}
|
||||
)
|
||||
self.assertRedirects(response, "/")
|
||||
# The language is set in a cookie.
|
||||
|
@ -80,7 +80,7 @@ class SetLanguageTests(TestCase):
|
|||
)
|
||||
# Insecure URL in HTTP referer.
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", secure=True, HTTP_REFERER=non_https_next_url
|
||||
"/i18n/setlang/", secure=True, headers={"referer": non_https_next_url}
|
||||
)
|
||||
self.assertEqual(response.url, "/")
|
||||
self.assertEqual(
|
||||
|
@ -94,7 +94,9 @@ class SetLanguageTests(TestCase):
|
|||
"""
|
||||
lang_code = self._get_inactive_language_code()
|
||||
post_data = {"language": lang_code}
|
||||
response = self.client.post("/i18n/setlang/", post_data, HTTP_REFERER="/i18n/")
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", post_data, headers={"referer": "/i18n/"}
|
||||
)
|
||||
self.assertRedirects(response, "/i18n/", fetch_redirect_response=False)
|
||||
self.assertEqual(
|
||||
self.client.cookies[settings.LANGUAGE_COOKIE_NAME].value, lang_code
|
||||
|
@ -121,7 +123,7 @@ class SetLanguageTests(TestCase):
|
|||
lang_code = self._get_inactive_language_code()
|
||||
post_data = {"language": lang_code, "next": "/"}
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", post_data, HTTP_ACCEPT="application/json"
|
||||
"/i18n/setlang/", post_data, headers={"accept": "application/json"}
|
||||
)
|
||||
self.assertRedirects(response, "/")
|
||||
self.assertEqual(
|
||||
|
@ -150,7 +152,7 @@ class SetLanguageTests(TestCase):
|
|||
lang_code = self._get_inactive_language_code()
|
||||
post_data = {"language": lang_code}
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", post_data, HTTP_ACCEPT="application/json"
|
||||
"/i18n/setlang/", post_data, headers={"accept": "application/json"}
|
||||
)
|
||||
self.assertEqual(response.status_code, 204)
|
||||
self.assertEqual(
|
||||
|
@ -165,7 +167,7 @@ class SetLanguageTests(TestCase):
|
|||
lang_code = self._get_inactive_language_code()
|
||||
post_data = {"language": lang_code, "next": "//unsafe/redirection/"}
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", post_data, HTTP_ACCEPT="application/json"
|
||||
"/i18n/setlang/", post_data, headers={"accept": "application/json"}
|
||||
)
|
||||
self.assertEqual(response.url, "/")
|
||||
self.assertEqual(
|
||||
|
@ -213,7 +215,7 @@ class SetLanguageTests(TestCase):
|
|||
# %C3%A4 decodes to ä, %26 to &.
|
||||
encoded_url = "/test-setlang/%C3%A4/?foo=bar&baz=alpha%26omega"
|
||||
response = self.client.post(
|
||||
"/i18n/setlang/", {"language": lang_code}, HTTP_REFERER=encoded_url
|
||||
"/i18n/setlang/", {"language": lang_code}, headers={"referer": encoded_url}
|
||||
)
|
||||
self.assertRedirects(response, encoded_url, fetch_redirect_response=False)
|
||||
self.assertEqual(
|
||||
|
@ -230,7 +232,7 @@ class SetLanguageTests(TestCase):
|
|||
"/i18n/setlang/",
|
||||
data={"language": "nl"},
|
||||
follow=True,
|
||||
HTTP_REFERER="/en/translated/",
|
||||
headers={"referer": "/en/translated/"},
|
||||
)
|
||||
self.assertEqual(self.client.cookies[settings.LANGUAGE_COOKIE_NAME].value, "nl")
|
||||
self.assertRedirects(response, "/nl/vertaald/")
|
||||
|
@ -239,7 +241,7 @@ class SetLanguageTests(TestCase):
|
|||
"/i18n/setlang/",
|
||||
data={"language": "en"},
|
||||
follow=True,
|
||||
HTTP_REFERER="/nl/vertaald/",
|
||||
headers={"referer": "/nl/vertaald/"},
|
||||
)
|
||||
self.assertRedirects(response, "/en/translated/")
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ class StaticTests(SimpleTestCase):
|
|||
file_name = "file.txt"
|
||||
response = self.client.get(
|
||||
"/%s/%s" % (self.prefix, file_name),
|
||||
HTTP_IF_MODIFIED_SINCE="Thu, 1 Jan 1970 00:00:00 GMT",
|
||||
headers={"if-modified-since": "Thu, 1 Jan 1970 00:00:00 GMT"},
|
||||
)
|
||||
response_content = b"".join(response)
|
||||
with open(path.join(media_dir, file_name), "rb") as fp:
|
||||
|
@ -73,9 +73,11 @@ class StaticTests(SimpleTestCase):
|
|||
file_name = "file.txt"
|
||||
response = self.client.get(
|
||||
"/%s/%s" % (self.prefix, file_name),
|
||||
HTTP_IF_MODIFIED_SINCE="Mon, 18 Jan 2038 05:14:07 GMT"
|
||||
headers={
|
||||
# This is 24h before max Unix time. Remember to fix Django and
|
||||
# update this test well before 2038 :)
|
||||
"if-modified-since": "Mon, 18 Jan 2038 05:14:07 GMT"
|
||||
},
|
||||
)
|
||||
self.assertIsInstance(response, HttpResponseNotModified)
|
||||
|
||||
|
@ -88,7 +90,8 @@ class StaticTests(SimpleTestCase):
|
|||
file_name = "file.txt"
|
||||
invalid_date = "Mon, 28 May 999999999999 28:25:26 GMT"
|
||||
response = self.client.get(
|
||||
"/%s/%s" % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE=invalid_date
|
||||
"/%s/%s" % (self.prefix, file_name),
|
||||
headers={"if-modified-since": invalid_date},
|
||||
)
|
||||
response_content = b"".join(response)
|
||||
with open(path.join(media_dir, file_name), "rb") as fp:
|
||||
|
@ -104,7 +107,8 @@ class StaticTests(SimpleTestCase):
|
|||
file_name = "file.txt"
|
||||
invalid_date = ": 1291108438, Wed, 20 Oct 2010 14:05:00 GMT"
|
||||
response = self.client.get(
|
||||
"/%s/%s" % (self.prefix, file_name), HTTP_IF_MODIFIED_SINCE=invalid_date
|
||||
"/%s/%s" % (self.prefix, file_name),
|
||||
headers={"if-modified-since": invalid_date},
|
||||
)
|
||||
response_content = b"".join(response)
|
||||
with open(path.join(media_dir, file_name), "rb") as fp:
|
||||
|
|
Loading…
Reference in New Issue