tests.py 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. """
  2. Regression tests for the Test Client, especially the customized assertions.
  3. """
  4. import itertools
  5. import os
  6. from django.contrib.auth.models import User
  7. from django.contrib.auth.signals import user_logged_in, user_logged_out
  8. from django.http import HttpResponse
  9. from django.template import Context, RequestContext, TemplateSyntaxError, engines
  10. from django.template.response import SimpleTemplateResponse
  11. from django.test import (
  12. Client,
  13. SimpleTestCase,
  14. TestCase,
  15. modify_settings,
  16. override_settings,
  17. )
  18. from django.test.client import RedirectCycleError, RequestFactory, encode_file
  19. from django.test.utils import ContextList
  20. from django.urls import NoReverseMatch, reverse
  21. from django.utils.translation import gettext_lazy
  22. from .models import CustomUser
  23. from .views import CustomTestException
  24. class TestDataMixin:
  25. @classmethod
  26. def setUpTestData(cls):
  27. cls.u1 = User.objects.create_user(username="testclient", password="password")
  28. cls.staff = User.objects.create_user(
  29. username="staff", password="password", is_staff=True
  30. )
  31. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  32. class AssertContainsTests(SimpleTestCase):
  33. def test_contains(self):
  34. "Responses can be inspected for content, including counting repeated substrings"
  35. response = self.client.get("/no_template_view/")
  36. self.assertNotContains(response, "never")
  37. self.assertContains(response, "never", 0)
  38. self.assertContains(response, "once")
  39. self.assertContains(response, "once", 1)
  40. self.assertContains(response, "twice")
  41. self.assertContains(response, "twice", 2)
  42. try:
  43. self.assertContains(response, "text", status_code=999)
  44. except AssertionError as e:
  45. self.assertIn(
  46. "Couldn't retrieve content: Response code was 200 (expected 999)",
  47. str(e),
  48. )
  49. try:
  50. self.assertContains(response, "text", status_code=999, msg_prefix="abc")
  51. except AssertionError as e:
  52. self.assertIn(
  53. "abc: Couldn't retrieve content: Response code was 200 (expected 999)",
  54. str(e),
  55. )
  56. try:
  57. self.assertNotContains(response, "text", status_code=999)
  58. except AssertionError as e:
  59. self.assertIn(
  60. "Couldn't retrieve content: Response code was 200 (expected 999)",
  61. str(e),
  62. )
  63. try:
  64. self.assertNotContains(response, "text", status_code=999, msg_prefix="abc")
  65. except AssertionError as e:
  66. self.assertIn(
  67. "abc: Couldn't retrieve content: Response code was 200 (expected 999)",
  68. str(e),
  69. )
  70. try:
  71. self.assertNotContains(response, "once")
  72. except AssertionError as e:
  73. self.assertIn(
  74. "'once' unexpectedly found in the following response\n"
  75. f"{response.content}",
  76. str(e),
  77. )
  78. try:
  79. self.assertNotContains(response, "once", msg_prefix="abc")
  80. except AssertionError as e:
  81. self.assertIn(
  82. "abc: 'once' unexpectedly found in the following response\n"
  83. f"{response.content}",
  84. str(e),
  85. )
  86. try:
  87. self.assertContains(response, "never", 1)
  88. except AssertionError as e:
  89. self.assertIn(
  90. "Found 0 instances of 'never' (expected 1) in the following response\n"
  91. f"{response.content}",
  92. str(e),
  93. )
  94. try:
  95. self.assertContains(response, "never", 1, msg_prefix="abc")
  96. except AssertionError as e:
  97. self.assertIn(
  98. "abc: Found 0 instances of 'never' (expected 1) in the following "
  99. f"response\n{response.content}",
  100. str(e),
  101. )
  102. try:
  103. self.assertContains(response, "once", 0)
  104. except AssertionError as e:
  105. self.assertIn(
  106. "Found 1 instances of 'once' (expected 0) in the following response\n"
  107. f"{response.content}",
  108. str(e),
  109. )
  110. try:
  111. self.assertContains(response, "once", 0, msg_prefix="abc")
  112. except AssertionError as e:
  113. self.assertIn(
  114. "abc: Found 1 instances of 'once' (expected 0) in the following "
  115. f"response\n{response.content}",
  116. str(e),
  117. )
  118. try:
  119. self.assertContains(response, "once", 2)
  120. except AssertionError as e:
  121. self.assertIn(
  122. "Found 1 instances of 'once' (expected 2) in the following response\n"
  123. f"{response.content}",
  124. str(e),
  125. )
  126. try:
  127. self.assertContains(response, "once", 2, msg_prefix="abc")
  128. except AssertionError as e:
  129. self.assertIn(
  130. "abc: Found 1 instances of 'once' (expected 2) in the following "
  131. f"response\n{response.content}",
  132. str(e),
  133. )
  134. try:
  135. self.assertContains(response, "twice", 1)
  136. except AssertionError as e:
  137. self.assertIn(
  138. "Found 2 instances of 'twice' (expected 1) in the following response\n"
  139. f"{response.content}",
  140. str(e),
  141. )
  142. try:
  143. self.assertContains(response, "twice", 1, msg_prefix="abc")
  144. except AssertionError as e:
  145. self.assertIn(
  146. "abc: Found 2 instances of 'twice' (expected 1) in the following "
  147. f"response\n{response.content}",
  148. str(e),
  149. )
  150. try:
  151. self.assertContains(response, "thrice")
  152. except AssertionError as e:
  153. self.assertIn(
  154. f"Couldn't find 'thrice' in the following response\n{response.content}",
  155. str(e),
  156. )
  157. try:
  158. self.assertContains(response, "thrice", msg_prefix="abc")
  159. except AssertionError as e:
  160. self.assertIn(
  161. "abc: Couldn't find 'thrice' in the following response\n"
  162. f"{response.content}",
  163. str(e),
  164. )
  165. try:
  166. self.assertContains(response, "thrice", 3)
  167. except AssertionError as e:
  168. self.assertIn(
  169. "Found 0 instances of 'thrice' (expected 3) in the following response\n"
  170. f"{response.content}",
  171. str(e),
  172. )
  173. try:
  174. self.assertContains(response, "thrice", 3, msg_prefix="abc")
  175. except AssertionError as e:
  176. self.assertIn(
  177. "abc: Found 0 instances of 'thrice' (expected 3) in the following "
  178. f"response\n{response.content}",
  179. str(e),
  180. )
  181. long_content = (
  182. b"This is a very very very very very very very very long message which "
  183. b"exceedes the max limit of truncation."
  184. )
  185. response = HttpResponse(long_content)
  186. msg = f"Couldn't find 'thrice' in the following response\n{long_content}"
  187. with self.assertRaisesMessage(AssertionError, msg):
  188. self.assertContains(response, "thrice")
  189. msg = (
  190. "Found 1 instances of 'This' (expected 3) in the following response\n"
  191. f"{long_content}"
  192. )
  193. with self.assertRaisesMessage(AssertionError, msg):
  194. self.assertContains(response, "This", 3)
  195. msg = f"'very' unexpectedly found in the following response\n{long_content}"
  196. with self.assertRaisesMessage(AssertionError, msg):
  197. self.assertNotContains(response, "very")
  198. def test_unicode_contains(self):
  199. "Unicode characters can be found in template context"
  200. # Regression test for #10183
  201. r = self.client.get("/check_unicode/")
  202. self.assertContains(r, "さかき")
  203. self.assertContains(r, b"\xe5\xb3\xa0".decode())
  204. def test_unicode_not_contains(self):
  205. "Unicode characters can be searched for, and not found in template context"
  206. # Regression test for #10183
  207. r = self.client.get("/check_unicode/")
  208. self.assertNotContains(r, "はたけ")
  209. self.assertNotContains(r, b"\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91".decode())
  210. def test_binary_contains(self):
  211. r = self.client.get("/check_binary/")
  212. self.assertContains(r, b"%PDF-1.4\r\n%\x93\x8c\x8b\x9e")
  213. with self.assertRaises(AssertionError):
  214. self.assertContains(r, b"%PDF-1.4\r\n%\x93\x8c\x8b\x9e", count=2)
  215. def test_binary_not_contains(self):
  216. r = self.client.get("/check_binary/")
  217. self.assertNotContains(r, b"%ODF-1.4\r\n%\x93\x8c\x8b\x9e")
  218. with self.assertRaises(AssertionError):
  219. self.assertNotContains(r, b"%PDF-1.4\r\n%\x93\x8c\x8b\x9e")
  220. def test_nontext_contains(self):
  221. r = self.client.get("/no_template_view/")
  222. self.assertContains(r, gettext_lazy("once"))
  223. def test_nontext_not_contains(self):
  224. r = self.client.get("/no_template_view/")
  225. self.assertNotContains(r, gettext_lazy("never"))
  226. def test_assert_contains_renders_template_response(self):
  227. """
  228. An unrendered SimpleTemplateResponse may be used in assertContains().
  229. """
  230. template = engines["django"].from_string("Hello")
  231. response = SimpleTemplateResponse(template)
  232. self.assertContains(response, "Hello")
  233. def test_assert_contains_using_non_template_response(self):
  234. """auto-rendering does not affect responses that aren't
  235. instances (or subclasses) of SimpleTemplateResponse.
  236. Refs #15826.
  237. """
  238. response = HttpResponse("Hello")
  239. self.assertContains(response, "Hello")
  240. def test_assert_not_contains_renders_template_response(self):
  241. """
  242. An unrendered SimpleTemplateResponse may be used in assertNotContains().
  243. """
  244. template = engines["django"].from_string("Hello")
  245. response = SimpleTemplateResponse(template)
  246. self.assertNotContains(response, "Bye")
  247. def test_assert_not_contains_using_non_template_response(self):
  248. """
  249. auto-rendering does not affect responses that aren't instances (or
  250. subclasses) of SimpleTemplateResponse.
  251. """
  252. response = HttpResponse("Hello")
  253. self.assertNotContains(response, "Bye")
  254. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  255. class AssertTemplateUsedTests(TestDataMixin, TestCase):
  256. def test_no_context(self):
  257. "Template usage assertions work then templates aren't in use"
  258. response = self.client.get("/no_template_view/")
  259. # The no template case doesn't mess with the template assertions
  260. self.assertTemplateNotUsed(response, "GET Template")
  261. try:
  262. self.assertTemplateUsed(response, "GET Template")
  263. except AssertionError as e:
  264. self.assertIn("No templates used to render the response", str(e))
  265. try:
  266. self.assertTemplateUsed(response, "GET Template", msg_prefix="abc")
  267. except AssertionError as e:
  268. self.assertIn("abc: No templates used to render the response", str(e))
  269. msg = "No templates used to render the response"
  270. with self.assertRaisesMessage(AssertionError, msg):
  271. self.assertTemplateUsed(response, "GET Template", count=2)
  272. def test_single_context(self):
  273. "Template assertions work when there is a single context"
  274. response = self.client.get("/post_view/", {})
  275. msg = (
  276. ": Template 'Empty GET Template' was used unexpectedly in "
  277. "rendering the response"
  278. )
  279. with self.assertRaisesMessage(AssertionError, msg):
  280. self.assertTemplateNotUsed(response, "Empty GET Template")
  281. with self.assertRaisesMessage(AssertionError, "abc" + msg):
  282. self.assertTemplateNotUsed(response, "Empty GET Template", msg_prefix="abc")
  283. msg = (
  284. ": Template 'Empty POST Template' was not a template used to "
  285. "render the response. Actual template(s) used: Empty GET Template"
  286. )
  287. with self.assertRaisesMessage(AssertionError, msg):
  288. self.assertTemplateUsed(response, "Empty POST Template")
  289. with self.assertRaisesMessage(AssertionError, "abc" + msg):
  290. self.assertTemplateUsed(response, "Empty POST Template", msg_prefix="abc")
  291. msg = (
  292. ": Template 'Empty GET Template' was expected to be rendered 2 "
  293. "time(s) but was actually rendered 1 time(s)."
  294. )
  295. with self.assertRaisesMessage(AssertionError, msg):
  296. self.assertTemplateUsed(response, "Empty GET Template", count=2)
  297. with self.assertRaisesMessage(AssertionError, "abc" + msg):
  298. self.assertTemplateUsed(
  299. response, "Empty GET Template", msg_prefix="abc", count=2
  300. )
  301. def test_multiple_context(self):
  302. "Template assertions work when there are multiple contexts"
  303. post_data = {
  304. "text": "Hello World",
  305. "email": "foo@example.com",
  306. "value": 37,
  307. "single": "b",
  308. "multi": ("b", "c", "e"),
  309. }
  310. response = self.client.post("/form_view_with_template/", post_data)
  311. self.assertContains(response, "POST data OK")
  312. msg = "Template '%s' was used unexpectedly in rendering the response"
  313. with self.assertRaisesMessage(AssertionError, msg % "form_view.html"):
  314. self.assertTemplateNotUsed(response, "form_view.html")
  315. with self.assertRaisesMessage(AssertionError, msg % "base.html"):
  316. self.assertTemplateNotUsed(response, "base.html")
  317. msg = (
  318. "Template 'Valid POST Template' was not a template used to render "
  319. "the response. Actual template(s) used: form_view.html, base.html"
  320. )
  321. with self.assertRaisesMessage(AssertionError, msg):
  322. self.assertTemplateUsed(response, "Valid POST Template")
  323. msg = (
  324. "Template 'base.html' was expected to be rendered 2 time(s) but "
  325. "was actually rendered 1 time(s)."
  326. )
  327. with self.assertRaisesMessage(AssertionError, msg):
  328. self.assertTemplateUsed(response, "base.html", count=2)
  329. def test_template_rendered_multiple_times(self):
  330. """Template assertions work when a template is rendered multiple times."""
  331. response = self.client.get("/render_template_multiple_times/")
  332. self.assertTemplateUsed(response, "base.html", count=2)
  333. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  334. class AssertRedirectsTests(SimpleTestCase):
  335. def test_redirect_page(self):
  336. "An assertion is raised if the original page couldn't be retrieved as expected"
  337. # This page will redirect with code 301, not 302
  338. response = self.client.get("/permanent_redirect_view/")
  339. try:
  340. self.assertRedirects(response, "/get_view/")
  341. except AssertionError as e:
  342. self.assertIn(
  343. "Response didn't redirect as expected: Response code was 301 "
  344. "(expected 302)",
  345. str(e),
  346. )
  347. try:
  348. self.assertRedirects(response, "/get_view/", msg_prefix="abc")
  349. except AssertionError as e:
  350. self.assertIn(
  351. "abc: Response didn't redirect as expected: Response code was 301 "
  352. "(expected 302)",
  353. str(e),
  354. )
  355. def test_lost_query(self):
  356. """
  357. An assertion is raised if the redirect location doesn't preserve GET
  358. parameters.
  359. """
  360. response = self.client.get("/redirect_view/", {"var": "value"})
  361. try:
  362. self.assertRedirects(response, "/get_view/")
  363. except AssertionError as e:
  364. self.assertIn(
  365. "Response redirected to '/get_view/?var=value', expected '/get_view/'",
  366. str(e),
  367. )
  368. try:
  369. self.assertRedirects(response, "/get_view/", msg_prefix="abc")
  370. except AssertionError as e:
  371. self.assertIn(
  372. "abc: Response redirected to '/get_view/?var=value', expected "
  373. "'/get_view/'",
  374. str(e),
  375. )
  376. def test_incorrect_target(self):
  377. "An assertion is raised if the response redirects to another target"
  378. response = self.client.get("/permanent_redirect_view/")
  379. try:
  380. # Should redirect to get_view
  381. self.assertRedirects(response, "/some_view/")
  382. except AssertionError as e:
  383. self.assertIn(
  384. "Response didn't redirect as expected: Response code was 301 "
  385. "(expected 302)",
  386. str(e),
  387. )
  388. def test_target_page(self):
  389. """
  390. An assertion is raised if the response redirect target cannot be
  391. retrieved as expected.
  392. """
  393. response = self.client.get("/double_redirect_view/")
  394. try:
  395. # The redirect target responds with a 301 code, not 200
  396. self.assertRedirects(response, "http://testserver/permanent_redirect_view/")
  397. except AssertionError as e:
  398. self.assertIn(
  399. "Couldn't retrieve redirection page '/permanent_redirect_view/': "
  400. "response code was 301 (expected 200)",
  401. str(e),
  402. )
  403. try:
  404. # The redirect target responds with a 301 code, not 200
  405. self.assertRedirects(
  406. response, "http://testserver/permanent_redirect_view/", msg_prefix="abc"
  407. )
  408. except AssertionError as e:
  409. self.assertIn(
  410. "abc: Couldn't retrieve redirection page '/permanent_redirect_view/': "
  411. "response code was 301 (expected 200)",
  412. str(e),
  413. )
  414. def test_redirect_chain(self):
  415. "You can follow a redirect chain of multiple redirects"
  416. response = self.client.get("/redirects/further/more/", {}, follow=True)
  417. self.assertRedirects(
  418. response, "/no_template_view/", status_code=302, target_status_code=200
  419. )
  420. self.assertEqual(len(response.redirect_chain), 1)
  421. self.assertEqual(response.redirect_chain[0], ("/no_template_view/", 302))
  422. def test_multiple_redirect_chain(self):
  423. "You can follow a redirect chain of multiple redirects"
  424. response = self.client.get("/redirects/", {}, follow=True)
  425. self.assertRedirects(
  426. response, "/no_template_view/", status_code=302, target_status_code=200
  427. )
  428. self.assertEqual(len(response.redirect_chain), 3)
  429. self.assertEqual(response.redirect_chain[0], ("/redirects/further/", 302))
  430. self.assertEqual(response.redirect_chain[1], ("/redirects/further/more/", 302))
  431. self.assertEqual(response.redirect_chain[2], ("/no_template_view/", 302))
  432. def test_redirect_chain_to_non_existent(self):
  433. "You can follow a chain to a nonexistent view."
  434. response = self.client.get("/redirect_to_non_existent_view2/", {}, follow=True)
  435. self.assertRedirects(
  436. response, "/non_existent_view/", status_code=302, target_status_code=404
  437. )
  438. def test_redirect_chain_to_self(self):
  439. "Redirections to self are caught and escaped"
  440. with self.assertRaises(RedirectCycleError) as context:
  441. self.client.get("/redirect_to_self/", {}, follow=True)
  442. response = context.exception.last_response
  443. # The chain of redirects stops once the cycle is detected.
  444. self.assertRedirects(
  445. response, "/redirect_to_self/", status_code=302, target_status_code=302
  446. )
  447. self.assertEqual(len(response.redirect_chain), 2)
  448. def test_redirect_to_self_with_changing_query(self):
  449. "Redirections don't loop forever even if query is changing"
  450. with self.assertRaises(RedirectCycleError):
  451. self.client.get(
  452. "/redirect_to_self_with_changing_query_view/",
  453. {"counter": "0"},
  454. follow=True,
  455. )
  456. def test_circular_redirect(self):
  457. "Circular redirect chains are caught and escaped"
  458. with self.assertRaises(RedirectCycleError) as context:
  459. self.client.get("/circular_redirect_1/", {}, follow=True)
  460. response = context.exception.last_response
  461. # The chain of redirects will get back to the starting point, but stop there.
  462. self.assertRedirects(
  463. response, "/circular_redirect_2/", status_code=302, target_status_code=302
  464. )
  465. self.assertEqual(len(response.redirect_chain), 4)
  466. def test_redirect_chain_post(self):
  467. "A redirect chain will be followed from an initial POST post"
  468. response = self.client.post("/redirects/", {"nothing": "to_send"}, follow=True)
  469. self.assertRedirects(response, "/no_template_view/", 302, 200)
  470. self.assertEqual(len(response.redirect_chain), 3)
  471. def test_redirect_chain_head(self):
  472. "A redirect chain will be followed from an initial HEAD request"
  473. response = self.client.head("/redirects/", {"nothing": "to_send"}, follow=True)
  474. self.assertRedirects(response, "/no_template_view/", 302, 200)
  475. self.assertEqual(len(response.redirect_chain), 3)
  476. def test_redirect_chain_options(self):
  477. "A redirect chain will be followed from an initial OPTIONS request"
  478. response = self.client.options("/redirects/", follow=True)
  479. self.assertRedirects(response, "/no_template_view/", 302, 200)
  480. self.assertEqual(len(response.redirect_chain), 3)
  481. def test_redirect_chain_put(self):
  482. "A redirect chain will be followed from an initial PUT request"
  483. response = self.client.put("/redirects/", follow=True)
  484. self.assertRedirects(response, "/no_template_view/", 302, 200)
  485. self.assertEqual(len(response.redirect_chain), 3)
  486. def test_redirect_chain_delete(self):
  487. "A redirect chain will be followed from an initial DELETE request"
  488. response = self.client.delete("/redirects/", follow=True)
  489. self.assertRedirects(response, "/no_template_view/", 302, 200)
  490. self.assertEqual(len(response.redirect_chain), 3)
  491. @modify_settings(ALLOWED_HOSTS={"append": "otherserver"})
  492. def test_redirect_to_different_host(self):
  493. "The test client will preserve scheme, host and port changes"
  494. response = self.client.get("/redirect_other_host/", follow=True)
  495. self.assertRedirects(
  496. response,
  497. "https://otherserver:8443/no_template_view/",
  498. status_code=302,
  499. target_status_code=200,
  500. )
  501. # We can't use is_secure() or get_host()
  502. # because response.request is a dictionary, not an HttpRequest
  503. self.assertEqual(response.request.get("wsgi.url_scheme"), "https")
  504. self.assertEqual(response.request.get("SERVER_NAME"), "otherserver")
  505. self.assertEqual(response.request.get("SERVER_PORT"), "8443")
  506. # assertRedirects() can follow redirect to 'otherserver' too.
  507. response = self.client.get("/redirect_other_host/", follow=False)
  508. self.assertRedirects(
  509. response,
  510. "https://otherserver:8443/no_template_view/",
  511. status_code=302,
  512. target_status_code=200,
  513. )
  514. def test_redirect_chain_on_non_redirect_page(self):
  515. """
  516. An assertion is raised if the original page couldn't be retrieved as
  517. expected.
  518. """
  519. # This page will redirect with code 301, not 302
  520. response = self.client.get("/get_view/", follow=True)
  521. try:
  522. self.assertRedirects(response, "/get_view/")
  523. except AssertionError as e:
  524. self.assertIn(
  525. "Response didn't redirect as expected: Response code was 200 "
  526. "(expected 302)",
  527. str(e),
  528. )
  529. try:
  530. self.assertRedirects(response, "/get_view/", msg_prefix="abc")
  531. except AssertionError as e:
  532. self.assertIn(
  533. "abc: Response didn't redirect as expected: Response code was 200 "
  534. "(expected 302)",
  535. str(e),
  536. )
  537. def test_redirect_on_non_redirect_page(self):
  538. "An assertion is raised if the original page couldn't be retrieved as expected"
  539. # This page will redirect with code 301, not 302
  540. response = self.client.get("/get_view/")
  541. try:
  542. self.assertRedirects(response, "/get_view/")
  543. except AssertionError as e:
  544. self.assertIn(
  545. "Response didn't redirect as expected: Response code was 200 "
  546. "(expected 302)",
  547. str(e),
  548. )
  549. try:
  550. self.assertRedirects(response, "/get_view/", msg_prefix="abc")
  551. except AssertionError as e:
  552. self.assertIn(
  553. "abc: Response didn't redirect as expected: Response code was 200 "
  554. "(expected 302)",
  555. str(e),
  556. )
  557. def test_redirect_scheme(self):
  558. """
  559. An assertion is raised if the response doesn't have the scheme
  560. specified in expected_url.
  561. """
  562. # For all possible True/False combinations of follow and secure
  563. for follow, secure in itertools.product([True, False], repeat=2):
  564. # always redirects to https
  565. response = self.client.get(
  566. "/https_redirect_view/", follow=follow, secure=secure
  567. )
  568. # the goal scheme is https
  569. self.assertRedirects(
  570. response, "https://testserver/secure_view/", status_code=302
  571. )
  572. with self.assertRaises(AssertionError):
  573. self.assertRedirects(
  574. response, "http://testserver/secure_view/", status_code=302
  575. )
  576. def test_redirect_fetch_redirect_response(self):
  577. """Preserve extra headers of requests made with django.test.Client."""
  578. methods = (
  579. "get",
  580. "post",
  581. "head",
  582. "options",
  583. "put",
  584. "patch",
  585. "delete",
  586. "trace",
  587. )
  588. for method in methods:
  589. with self.subTest(method=method):
  590. req_method = getattr(self.client, method)
  591. # HTTP_REDIRECT in "extra".
  592. response = req_method(
  593. "/redirect_based_on_extra_headers_1/",
  594. follow=False,
  595. HTTP_REDIRECT="val",
  596. )
  597. self.assertRedirects(
  598. response,
  599. "/redirect_based_on_extra_headers_2/",
  600. fetch_redirect_response=True,
  601. status_code=302,
  602. target_status_code=302,
  603. )
  604. # HTTP_REDIRECT in "headers".
  605. response = req_method(
  606. "/redirect_based_on_extra_headers_1/",
  607. follow=False,
  608. headers={"redirect": "val"},
  609. )
  610. self.assertRedirects(
  611. response,
  612. "/redirect_based_on_extra_headers_2/",
  613. fetch_redirect_response=True,
  614. status_code=302,
  615. target_status_code=302,
  616. )
  617. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  618. class LoginTests(TestDataMixin, TestCase):
  619. def test_login_different_client(self):
  620. "Using a different test client doesn't violate authentication"
  621. # Create a second client, and log in.
  622. c = Client()
  623. login = c.login(username="testclient", password="password")
  624. self.assertTrue(login, "Could not log in")
  625. # Get a redirection page with the second client.
  626. response = c.get("/login_protected_redirect_view/")
  627. # At this points, the self.client isn't logged in.
  628. # assertRedirects uses the original client, not the default client.
  629. self.assertRedirects(response, "/get_view/")
  630. @override_settings(
  631. SESSION_ENGINE="test_client_regress.session",
  632. ROOT_URLCONF="test_client_regress.urls",
  633. )
  634. class SessionEngineTests(TestDataMixin, TestCase):
  635. def test_login(self):
  636. "A session engine that modifies the session key can be used to log in"
  637. login = self.client.login(username="testclient", password="password")
  638. self.assertTrue(login, "Could not log in")
  639. # Try to access a login protected page.
  640. response = self.client.get("/login_protected_view/")
  641. self.assertEqual(response.status_code, 200)
  642. self.assertEqual(response.context["user"].username, "testclient")
  643. @override_settings(
  644. ROOT_URLCONF="test_client_regress.urls",
  645. )
  646. class URLEscapingTests(SimpleTestCase):
  647. def test_simple_argument_get(self):
  648. "Get a view that has a simple string argument"
  649. response = self.client.get(reverse("arg_view", args=["Slartibartfast"]))
  650. self.assertEqual(response.status_code, 200)
  651. self.assertEqual(response.content, b"Howdy, Slartibartfast")
  652. def test_argument_with_space_get(self):
  653. "Get a view that has a string argument that requires escaping"
  654. response = self.client.get(reverse("arg_view", args=["Arthur Dent"]))
  655. self.assertEqual(response.status_code, 200)
  656. self.assertEqual(response.content, b"Hi, Arthur")
  657. def test_simple_argument_post(self):
  658. "Post for a view that has a simple string argument"
  659. response = self.client.post(reverse("arg_view", args=["Slartibartfast"]))
  660. self.assertEqual(response.status_code, 200)
  661. self.assertEqual(response.content, b"Howdy, Slartibartfast")
  662. def test_argument_with_space_post(self):
  663. "Post for a view that has a string argument that requires escaping"
  664. response = self.client.post(reverse("arg_view", args=["Arthur Dent"]))
  665. self.assertEqual(response.status_code, 200)
  666. self.assertEqual(response.content, b"Hi, Arthur")
  667. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  668. class ExceptionTests(TestDataMixin, TestCase):
  669. def test_exception_cleared(self):
  670. "#5836 - A stale user exception isn't re-raised by the test client."
  671. login = self.client.login(username="testclient", password="password")
  672. self.assertTrue(login, "Could not log in")
  673. with self.assertRaises(CustomTestException):
  674. self.client.get("/staff_only/")
  675. # At this point, an exception has been raised, and should be cleared.
  676. # This next operation should be successful; if it isn't we have a problem.
  677. login = self.client.login(username="staff", password="password")
  678. self.assertTrue(login, "Could not log in")
  679. self.client.get("/staff_only/")
  680. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  681. class TemplateExceptionTests(SimpleTestCase):
  682. @override_settings(
  683. TEMPLATES=[
  684. {
  685. "BACKEND": "django.template.backends.django.DjangoTemplates",
  686. "DIRS": [os.path.join(os.path.dirname(__file__), "bad_templates")],
  687. }
  688. ]
  689. )
  690. def test_bad_404_template(self):
  691. "Errors found when rendering 404 error templates are re-raised"
  692. with self.assertRaises(TemplateSyntaxError):
  693. self.client.get("/no_such_view/")
  694. # We need two different tests to check URLconf substitution - one to check
  695. # it was changed, and another one (without self.urls) to check it was reverted on
  696. # teardown. This pair of tests relies upon the alphabetical ordering of test execution.
  697. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  698. class UrlconfSubstitutionTests(SimpleTestCase):
  699. def test_urlconf_was_changed(self):
  700. "TestCase can enforce a custom URLconf on a per-test basis"
  701. url = reverse("arg_view", args=["somename"])
  702. self.assertEqual(url, "/arg_view/somename/")
  703. # This test needs to run *after* UrlconfSubstitutionTests; the zz prefix in the
  704. # name is to ensure alphabetical ordering.
  705. class zzUrlconfSubstitutionTests(SimpleTestCase):
  706. def test_urlconf_was_reverted(self):
  707. """URLconf is reverted to original value after modification in a TestCase
  708. This will not find a match as the default ROOT_URLCONF is empty.
  709. """
  710. with self.assertRaises(NoReverseMatch):
  711. reverse("arg_view", args=["somename"])
  712. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  713. class ContextTests(TestDataMixin, TestCase):
  714. def test_single_context(self):
  715. "Context variables can be retrieved from a single context"
  716. response = self.client.get("/request_data/", data={"foo": "whiz"})
  717. self.assertIsInstance(response.context, RequestContext)
  718. self.assertIn("get-foo", response.context)
  719. self.assertEqual(response.context["get-foo"], "whiz")
  720. self.assertEqual(response.context["data"], "sausage")
  721. with self.assertRaisesMessage(KeyError, "does-not-exist"):
  722. response.context["does-not-exist"]
  723. def test_inherited_context(self):
  724. "Context variables can be retrieved from a list of contexts"
  725. response = self.client.get("/request_data_extended/", data={"foo": "whiz"})
  726. self.assertEqual(response.context.__class__, ContextList)
  727. self.assertEqual(len(response.context), 2)
  728. self.assertIn("get-foo", response.context)
  729. self.assertEqual(response.context["get-foo"], "whiz")
  730. self.assertEqual(response.context["data"], "bacon")
  731. with self.assertRaisesMessage(KeyError, "does-not-exist"):
  732. response.context["does-not-exist"]
  733. def test_contextlist_keys(self):
  734. c1 = Context()
  735. c1.update({"hello": "world", "goodbye": "john"})
  736. c1.update({"hello": "dolly", "dolly": "parton"})
  737. c2 = Context()
  738. c2.update({"goodbye": "world", "python": "rocks"})
  739. c2.update({"goodbye": "dolly"})
  740. k = ContextList([c1, c2])
  741. # None, True and False are builtins of BaseContext, and present
  742. # in every Context without needing to be added.
  743. self.assertEqual(
  744. {"None", "True", "False", "hello", "goodbye", "python", "dolly"}, k.keys()
  745. )
  746. def test_contextlist_get(self):
  747. c1 = Context({"hello": "world", "goodbye": "john"})
  748. c2 = Context({"goodbye": "world", "python": "rocks"})
  749. k = ContextList([c1, c2])
  750. self.assertEqual(k.get("hello"), "world")
  751. self.assertEqual(k.get("goodbye"), "john")
  752. self.assertEqual(k.get("python"), "rocks")
  753. self.assertEqual(k.get("nonexistent", "default"), "default")
  754. def test_15368(self):
  755. # Need to insert a context processor that assumes certain things about
  756. # the request instance. This triggers a bug caused by some ways of
  757. # copying RequestContext.
  758. with self.settings(
  759. TEMPLATES=[
  760. {
  761. "BACKEND": "django.template.backends.django.DjangoTemplates",
  762. "APP_DIRS": True,
  763. "OPTIONS": {
  764. "context_processors": [
  765. "test_client_regress.context_processors.special",
  766. ],
  767. },
  768. }
  769. ]
  770. ):
  771. response = self.client.get("/request_context_view/")
  772. self.assertContains(response, "Path: /request_context_view/")
  773. def test_nested_requests(self):
  774. """
  775. response.context is not lost when view call another view.
  776. """
  777. response = self.client.get("/nested_view/")
  778. self.assertIsInstance(response.context, RequestContext)
  779. self.assertEqual(response.context["nested"], "yes")
  780. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  781. class SessionTests(TestDataMixin, TestCase):
  782. def test_session(self):
  783. "The session isn't lost if a user logs in"
  784. # The session doesn't exist to start.
  785. response = self.client.get("/check_session/")
  786. self.assertEqual(response.status_code, 200)
  787. self.assertEqual(response.content, b"NO")
  788. # This request sets a session variable.
  789. response = self.client.get("/set_session/")
  790. self.assertEqual(response.status_code, 200)
  791. self.assertEqual(response.content, b"set_session")
  792. # The session has been modified
  793. response = self.client.get("/check_session/")
  794. self.assertEqual(response.status_code, 200)
  795. self.assertEqual(response.content, b"YES")
  796. # Log in
  797. login = self.client.login(username="testclient", password="password")
  798. self.assertTrue(login, "Could not log in")
  799. # Session should still contain the modified value
  800. response = self.client.get("/check_session/")
  801. self.assertEqual(response.status_code, 200)
  802. self.assertEqual(response.content, b"YES")
  803. def test_session_initiated(self):
  804. session = self.client.session
  805. session["session_var"] = "foo"
  806. session.save()
  807. response = self.client.get("/check_session/")
  808. self.assertEqual(response.content, b"foo")
  809. def test_logout(self):
  810. """Logout should work whether the user is logged in or not (#9978)."""
  811. self.client.logout()
  812. login = self.client.login(username="testclient", password="password")
  813. self.assertTrue(login, "Could not log in")
  814. self.client.logout()
  815. self.client.logout()
  816. def test_logout_with_user(self):
  817. """Logout should send user_logged_out signal if user was logged in."""
  818. def listener(*args, **kwargs):
  819. listener.executed = True
  820. self.assertEqual(kwargs["sender"], User)
  821. listener.executed = False
  822. user_logged_out.connect(listener)
  823. self.client.login(username="testclient", password="password")
  824. self.client.logout()
  825. user_logged_out.disconnect(listener)
  826. self.assertTrue(listener.executed)
  827. @override_settings(AUTH_USER_MODEL="test_client_regress.CustomUser")
  828. def test_logout_with_custom_user(self):
  829. """Logout should send user_logged_out signal if custom user was logged in."""
  830. def listener(*args, **kwargs):
  831. self.assertEqual(kwargs["sender"], CustomUser)
  832. listener.executed = True
  833. listener.executed = False
  834. u = CustomUser.custom_objects.create(email="test@test.com")
  835. u.set_password("password")
  836. u.save()
  837. user_logged_out.connect(listener)
  838. self.client.login(username="test@test.com", password="password")
  839. self.client.logout()
  840. user_logged_out.disconnect(listener)
  841. self.assertTrue(listener.executed)
  842. @override_settings(
  843. AUTHENTICATION_BACKENDS=(
  844. "django.contrib.auth.backends.ModelBackend",
  845. "test_client_regress.auth_backends.CustomUserBackend",
  846. )
  847. )
  848. def test_logout_with_custom_auth_backend(self):
  849. "Request a logout after logging in with custom authentication backend"
  850. def listener(*args, **kwargs):
  851. self.assertEqual(kwargs["sender"], CustomUser)
  852. listener.executed = True
  853. listener.executed = False
  854. u = CustomUser.custom_objects.create(email="test@test.com")
  855. u.set_password("password")
  856. u.save()
  857. user_logged_out.connect(listener)
  858. self.client.login(username="test@test.com", password="password")
  859. self.client.logout()
  860. user_logged_out.disconnect(listener)
  861. self.assertTrue(listener.executed)
  862. def test_logout_without_user(self):
  863. """Logout should send signal even if user not authenticated."""
  864. def listener(user, *args, **kwargs):
  865. listener.user = user
  866. listener.executed = True
  867. listener.executed = False
  868. user_logged_out.connect(listener)
  869. self.client.login(username="incorrect", password="password")
  870. self.client.logout()
  871. user_logged_out.disconnect(listener)
  872. self.assertTrue(listener.executed)
  873. self.assertIsNone(listener.user)
  874. def test_login_with_user(self):
  875. """Login should send user_logged_in signal on successful login."""
  876. def listener(*args, **kwargs):
  877. listener.executed = True
  878. listener.executed = False
  879. user_logged_in.connect(listener)
  880. self.client.login(username="testclient", password="password")
  881. user_logged_out.disconnect(listener)
  882. self.assertTrue(listener.executed)
  883. def test_login_without_signal(self):
  884. """Login shouldn't send signal if user wasn't logged in"""
  885. def listener(*args, **kwargs):
  886. listener.executed = True
  887. listener.executed = False
  888. user_logged_in.connect(listener)
  889. self.client.login(username="incorrect", password="password")
  890. user_logged_in.disconnect(listener)
  891. self.assertFalse(listener.executed)
  892. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  893. class RequestMethodTests(SimpleTestCase):
  894. def test_get(self):
  895. "Request a view via request method GET"
  896. response = self.client.get("/request_methods/")
  897. self.assertEqual(response.status_code, 200)
  898. self.assertEqual(response.content, b"request method: GET")
  899. def test_post(self):
  900. "Request a view via request method POST"
  901. response = self.client.post("/request_methods/")
  902. self.assertEqual(response.status_code, 200)
  903. self.assertEqual(response.content, b"request method: POST")
  904. def test_head(self):
  905. "Request a view via request method HEAD"
  906. response = self.client.head("/request_methods/")
  907. self.assertEqual(response.status_code, 200)
  908. # A HEAD request doesn't return any content.
  909. self.assertNotEqual(response.content, b"request method: HEAD")
  910. self.assertEqual(response.content, b"")
  911. def test_options(self):
  912. "Request a view via request method OPTIONS"
  913. response = self.client.options("/request_methods/")
  914. self.assertEqual(response.status_code, 200)
  915. self.assertEqual(response.content, b"request method: OPTIONS")
  916. def test_put(self):
  917. "Request a view via request method PUT"
  918. response = self.client.put("/request_methods/")
  919. self.assertEqual(response.status_code, 200)
  920. self.assertEqual(response.content, b"request method: PUT")
  921. def test_delete(self):
  922. "Request a view via request method DELETE"
  923. response = self.client.delete("/request_methods/")
  924. self.assertEqual(response.status_code, 200)
  925. self.assertEqual(response.content, b"request method: DELETE")
  926. def test_patch(self):
  927. "Request a view via request method PATCH"
  928. response = self.client.patch("/request_methods/")
  929. self.assertEqual(response.status_code, 200)
  930. self.assertEqual(response.content, b"request method: PATCH")
  931. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  932. class RequestMethodStringDataTests(SimpleTestCase):
  933. def test_post(self):
  934. "Request a view with string data via request method POST"
  935. # Regression test for #11371
  936. data = '{"test": "json"}'
  937. response = self.client.post(
  938. "/request_methods/", data=data, content_type="application/json"
  939. )
  940. self.assertEqual(response.status_code, 200)
  941. self.assertEqual(response.content, b"request method: POST")
  942. def test_put(self):
  943. "Request a view with string data via request method PUT"
  944. # Regression test for #11371
  945. data = '{"test": "json"}'
  946. response = self.client.put(
  947. "/request_methods/", data=data, content_type="application/json"
  948. )
  949. self.assertEqual(response.status_code, 200)
  950. self.assertEqual(response.content, b"request method: PUT")
  951. def test_patch(self):
  952. "Request a view with string data via request method PATCH"
  953. # Regression test for #17797
  954. data = '{"test": "json"}'
  955. response = self.client.patch(
  956. "/request_methods/", data=data, content_type="application/json"
  957. )
  958. self.assertEqual(response.status_code, 200)
  959. self.assertEqual(response.content, b"request method: PATCH")
  960. def test_empty_string_data(self):
  961. "Request a view with empty string data via request method GET/POST/HEAD"
  962. # Regression test for #21740
  963. response = self.client.get("/body/", data="", content_type="application/json")
  964. self.assertEqual(response.content, b"")
  965. response = self.client.post("/body/", data="", content_type="application/json")
  966. self.assertEqual(response.content, b"")
  967. response = self.client.head("/body/", data="", content_type="application/json")
  968. self.assertEqual(response.content, b"")
  969. def test_json_bytes(self):
  970. response = self.client.post(
  971. "/body/", data=b"{'value': 37}", content_type="application/json"
  972. )
  973. self.assertEqual(response.content, b"{'value': 37}")
  974. def test_json(self):
  975. response = self.client.get("/json_response/")
  976. self.assertEqual(response.json(), {"key": "value"})
  977. def test_json_charset(self):
  978. response = self.client.get("/json_response_latin1/")
  979. self.assertEqual(response.charset, "latin1")
  980. self.assertEqual(response.json(), {"a": "Å"})
  981. def test_json_structured_suffixes(self):
  982. valid_types = (
  983. "application/vnd.api+json",
  984. "application/vnd.api.foo+json",
  985. "application/json; charset=utf-8",
  986. "application/activity+json",
  987. "application/activity+json; charset=utf-8",
  988. )
  989. for content_type in valid_types:
  990. response = self.client.get(
  991. "/json_response/", {"content_type": content_type}
  992. )
  993. self.assertEqual(response.headers["Content-Type"], content_type)
  994. self.assertEqual(response.json(), {"key": "value"})
  995. def test_json_multiple_access(self):
  996. response = self.client.get("/json_response/")
  997. self.assertIs(response.json(), response.json())
  998. def test_json_wrong_header(self):
  999. response = self.client.get("/body/")
  1000. msg = (
  1001. 'Content-Type header is "text/html; charset=utf-8", not "application/json"'
  1002. )
  1003. with self.assertRaisesMessage(ValueError, msg):
  1004. self.assertEqual(response.json(), {"key": "value"})
  1005. @override_settings(
  1006. ROOT_URLCONF="test_client_regress.urls",
  1007. )
  1008. class QueryStringTests(SimpleTestCase):
  1009. def test_get_like_requests(self):
  1010. for method_name in ("get", "head"):
  1011. # A GET-like request can pass a query string as data (#10571)
  1012. method = getattr(self.client, method_name)
  1013. response = method("/request_data/", data={"foo": "whiz"})
  1014. self.assertEqual(response.context["get-foo"], "whiz")
  1015. # A GET-like request can pass a query string as part of the URL
  1016. response = method("/request_data/?foo=whiz")
  1017. self.assertEqual(response.context["get-foo"], "whiz")
  1018. # Data provided in the URL to a GET-like request is overridden by
  1019. # actual form data.
  1020. response = method("/request_data/?foo=whiz", data={"foo": "bang"})
  1021. self.assertEqual(response.context["get-foo"], "bang")
  1022. response = method("/request_data/?foo=whiz", data={"bar": "bang"})
  1023. self.assertIsNone(response.context["get-foo"])
  1024. self.assertEqual(response.context["get-bar"], "bang")
  1025. def test_post_like_requests(self):
  1026. # A POST-like request can pass a query string as data
  1027. response = self.client.post("/request_data/", data={"foo": "whiz"})
  1028. self.assertIsNone(response.context["get-foo"])
  1029. self.assertEqual(response.context["post-foo"], "whiz")
  1030. # A POST-like request can pass a query string as part of the URL
  1031. response = self.client.post("/request_data/?foo=whiz")
  1032. self.assertEqual(response.context["get-foo"], "whiz")
  1033. self.assertIsNone(response.context["post-foo"])
  1034. response = self.client.post("/request_data/", query_params={"foo": "whiz"})
  1035. self.assertEqual(response.context["get-foo"], "whiz")
  1036. self.assertIsNone(response.context["post-foo"])
  1037. # POST data provided in the URL augments actual form data
  1038. response = self.client.post("/request_data/?foo=whiz", data={"foo": "bang"})
  1039. self.assertEqual(response.context["get-foo"], "whiz")
  1040. self.assertEqual(response.context["post-foo"], "bang")
  1041. response = self.client.post("/request_data/?foo=whiz", data={"bar": "bang"})
  1042. self.assertEqual(response.context["get-foo"], "whiz")
  1043. self.assertIsNone(response.context["get-bar"])
  1044. self.assertIsNone(response.context["post-foo"])
  1045. self.assertEqual(response.context["post-bar"], "bang")
  1046. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  1047. class PayloadEncodingTests(SimpleTestCase):
  1048. """Regression tests for #10571."""
  1049. def test_simple_payload(self):
  1050. """A simple ASCII-only text can be POSTed."""
  1051. text = "English: mountain pass"
  1052. response = self.client.post(
  1053. "/parse_encoded_text/", text, content_type="text/plain"
  1054. )
  1055. self.assertEqual(response.content, text.encode())
  1056. def test_utf8_payload(self):
  1057. """Non-ASCII data encoded as UTF-8 can be POSTed."""
  1058. text = "dog: собака"
  1059. response = self.client.post(
  1060. "/parse_encoded_text/", text, content_type="text/plain; charset=utf-8"
  1061. )
  1062. self.assertEqual(response.content, text.encode())
  1063. def test_utf16_payload(self):
  1064. """Non-ASCII data encoded as UTF-16 can be POSTed."""
  1065. text = "dog: собака"
  1066. response = self.client.post(
  1067. "/parse_encoded_text/", text, content_type="text/plain; charset=utf-16"
  1068. )
  1069. self.assertEqual(response.content, text.encode("utf-16"))
  1070. def test_non_utf_payload(self):
  1071. """Non-ASCII data as a non-UTF based encoding can be POSTed."""
  1072. text = "dog: собака"
  1073. response = self.client.post(
  1074. "/parse_encoded_text/", text, content_type="text/plain; charset=koi8-r"
  1075. )
  1076. self.assertEqual(response.content, text.encode("koi8-r"))
  1077. class DummyFile:
  1078. def __init__(self, filename):
  1079. self.name = filename
  1080. def read(self):
  1081. return b"TEST_FILE_CONTENT"
  1082. class UploadedFileEncodingTest(SimpleTestCase):
  1083. def test_file_encoding(self):
  1084. encoded_file = encode_file(
  1085. "TEST_BOUNDARY", "TEST_KEY", DummyFile("test_name.bin")
  1086. )
  1087. self.assertEqual(b"--TEST_BOUNDARY", encoded_file[0])
  1088. self.assertEqual(
  1089. b'Content-Disposition: form-data; name="TEST_KEY"; '
  1090. b'filename="test_name.bin"',
  1091. encoded_file[1],
  1092. )
  1093. self.assertEqual(b"TEST_FILE_CONTENT", encoded_file[-1])
  1094. def test_guesses_content_type_on_file_encoding(self):
  1095. self.assertEqual(
  1096. b"Content-Type: application/octet-stream",
  1097. encode_file("IGNORE", "IGNORE", DummyFile("file.bin"))[2],
  1098. )
  1099. self.assertEqual(
  1100. b"Content-Type: text/plain",
  1101. encode_file("IGNORE", "IGNORE", DummyFile("file.txt"))[2],
  1102. )
  1103. self.assertIn(
  1104. encode_file("IGNORE", "IGNORE", DummyFile("file.zip"))[2],
  1105. (
  1106. b"Content-Type: application/x-compress",
  1107. b"Content-Type: application/x-zip",
  1108. b"Content-Type: application/x-zip-compressed",
  1109. b"Content-Type: application/zip",
  1110. ),
  1111. )
  1112. self.assertEqual(
  1113. b"Content-Type: application/octet-stream",
  1114. encode_file("IGNORE", "IGNORE", DummyFile("file.unknown"))[2],
  1115. )
  1116. @override_settings(
  1117. ROOT_URLCONF="test_client_regress.urls",
  1118. )
  1119. class RequestHeadersTest(SimpleTestCase):
  1120. def test_client_headers(self):
  1121. "A test client can receive custom headers"
  1122. response = self.client.get(
  1123. "/check_headers/", headers={"x-arg-check": "Testing 123"}
  1124. )
  1125. self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
  1126. self.assertEqual(response.status_code, 200)
  1127. def test_client_headers_redirect(self):
  1128. "Test client headers are preserved through redirects"
  1129. response = self.client.get(
  1130. "/check_headers_redirect/",
  1131. follow=True,
  1132. headers={"x-arg-check": "Testing 123"},
  1133. )
  1134. self.assertEqual(response.content, b"HTTP_X_ARG_CHECK: Testing 123")
  1135. self.assertRedirects(
  1136. response, "/check_headers/", status_code=302, target_status_code=200
  1137. )
  1138. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  1139. class ReadLimitedStreamTest(SimpleTestCase):
  1140. """
  1141. HttpRequest.body, HttpRequest.read(), and HttpRequest.read(BUFFER) have
  1142. proper LimitedStream behavior.
  1143. Refs #14753, #15785
  1144. """
  1145. def test_body_from_empty_request(self):
  1146. """HttpRequest.body on a test client GET request should return
  1147. the empty string."""
  1148. self.assertEqual(self.client.get("/body/").content, b"")
  1149. def test_read_from_empty_request(self):
  1150. """HttpRequest.read() on a test client GET request should return the
  1151. empty string."""
  1152. self.assertEqual(self.client.get("/read_all/").content, b"")
  1153. def test_read_numbytes_from_empty_request(self):
  1154. """HttpRequest.read(LARGE_BUFFER) on a test client GET request should
  1155. return the empty string."""
  1156. self.assertEqual(self.client.get("/read_buffer/").content, b"")
  1157. def test_read_from_nonempty_request(self):
  1158. """HttpRequest.read() on a test client PUT request with some payload
  1159. should return that payload."""
  1160. payload = b"foobar"
  1161. self.assertEqual(
  1162. self.client.put(
  1163. "/read_all/", data=payload, content_type="text/plain"
  1164. ).content,
  1165. payload,
  1166. )
  1167. def test_read_numbytes_from_nonempty_request(self):
  1168. """HttpRequest.read(LARGE_BUFFER) on a test client PUT request with
  1169. some payload should return that payload."""
  1170. payload = b"foobar"
  1171. self.assertEqual(
  1172. self.client.put(
  1173. "/read_buffer/", data=payload, content_type="text/plain"
  1174. ).content,
  1175. payload,
  1176. )
  1177. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  1178. class RequestFactoryStateTest(SimpleTestCase):
  1179. """Regression tests for #15929."""
  1180. # These tests are checking that certain middleware don't change certain
  1181. # global state. Alternatively, from the point of view of a test, they are
  1182. # ensuring test isolation behavior. So, unusually, it doesn't make sense to
  1183. # run the tests individually, and if any are failing it is confusing to run
  1184. # them with any other set of tests.
  1185. def common_test_that_should_always_pass(self):
  1186. request = RequestFactory().get("/")
  1187. request.session = {}
  1188. self.assertFalse(hasattr(request, "user"))
  1189. def test_request(self):
  1190. self.common_test_that_should_always_pass()
  1191. def test_request_after_client(self):
  1192. # apart from the next line the three tests are identical
  1193. self.client.get("/")
  1194. self.common_test_that_should_always_pass()
  1195. def test_request_after_client_2(self):
  1196. # This test is executed after the previous one
  1197. self.common_test_that_should_always_pass()
  1198. @override_settings(ROOT_URLCONF="test_client_regress.urls")
  1199. class RequestFactoryEnvironmentTests(SimpleTestCase):
  1200. """
  1201. Regression tests for #8551 and #17067: ensure that environment variables
  1202. are set correctly in RequestFactory.
  1203. """
  1204. def test_should_set_correct_env_variables(self):
  1205. request = RequestFactory().get("/path/")
  1206. self.assertEqual(request.META.get("REMOTE_ADDR"), "127.0.0.1")
  1207. self.assertEqual(request.META.get("SERVER_NAME"), "testserver")
  1208. self.assertEqual(request.META.get("SERVER_PORT"), "80")
  1209. self.assertEqual(request.META.get("SERVER_PROTOCOL"), "HTTP/1.1")
  1210. self.assertEqual(
  1211. request.META.get("SCRIPT_NAME") + request.META.get("PATH_INFO"), "/path/"
  1212. )
  1213. def test_cookies(self):
  1214. factory = RequestFactory()
  1215. factory.cookies.load('A="B"; C="D"; Path=/; Version=1')
  1216. request = factory.get("/")
  1217. self.assertEqual(request.META["HTTP_COOKIE"], 'A="B"; C="D"')