tests.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. # -*- coding: utf-8 -*-
  2. import gzip
  3. import re
  4. import random
  5. import StringIO
  6. from django.conf import settings
  7. from django.core import mail
  8. from django.http import HttpRequest
  9. from django.http import HttpResponse
  10. from django.middleware.clickjacking import XFrameOptionsMiddleware
  11. from django.middleware.common import CommonMiddleware
  12. from django.middleware.http import ConditionalGetMiddleware
  13. from django.middleware.gzip import GZipMiddleware
  14. from django.test import TestCase
  15. class CommonMiddlewareTest(TestCase):
  16. def setUp(self):
  17. self.append_slash = settings.APPEND_SLASH
  18. self.prepend_www = settings.PREPEND_WWW
  19. self.ignorable_404_urls = settings.IGNORABLE_404_URLS
  20. self.send_broken_email_links = settings.SEND_BROKEN_LINK_EMAILS
  21. def tearDown(self):
  22. settings.APPEND_SLASH = self.append_slash
  23. settings.PREPEND_WWW = self.prepend_www
  24. settings.IGNORABLE_404_URLS = self.ignorable_404_urls
  25. settings.SEND_BROKEN_LINK_EMAILS = self.send_broken_email_links
  26. def _get_request(self, path):
  27. request = HttpRequest()
  28. request.META = {
  29. 'SERVER_NAME': 'testserver',
  30. 'SERVER_PORT': 80,
  31. }
  32. request.path = request.path_info = "/middleware/%s" % path
  33. return request
  34. def test_append_slash_have_slash(self):
  35. """
  36. Tests that URLs with slashes go unmolested.
  37. """
  38. settings.APPEND_SLASH = True
  39. request = self._get_request('slash/')
  40. self.assertEqual(CommonMiddleware().process_request(request), None)
  41. def test_append_slash_slashless_resource(self):
  42. """
  43. Tests that matches to explicit slashless URLs go unmolested.
  44. """
  45. settings.APPEND_SLASH = True
  46. request = self._get_request('noslash')
  47. self.assertEqual(CommonMiddleware().process_request(request), None)
  48. def test_append_slash_slashless_unknown(self):
  49. """
  50. Tests that APPEND_SLASH doesn't redirect to unknown resources.
  51. """
  52. settings.APPEND_SLASH = True
  53. request = self._get_request('unknown')
  54. self.assertEqual(CommonMiddleware().process_request(request), None)
  55. def test_append_slash_redirect(self):
  56. """
  57. Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
  58. """
  59. settings.APPEND_SLASH = True
  60. request = self._get_request('slash')
  61. r = CommonMiddleware().process_request(request)
  62. self.assertEqual(r.status_code, 301)
  63. self.assertEqual(r['Location'], 'http://testserver/middleware/slash/')
  64. def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
  65. """
  66. Tests that while in debug mode, an exception is raised with a warning
  67. when a failed attempt is made to POST to an URL which would normally be
  68. redirected to a slashed version.
  69. """
  70. settings.APPEND_SLASH = True
  71. settings.DEBUG = True
  72. request = self._get_request('slash')
  73. request.method = 'POST'
  74. self.assertRaises(
  75. RuntimeError,
  76. CommonMiddleware().process_request,
  77. request)
  78. try:
  79. CommonMiddleware().process_request(request)
  80. except RuntimeError, e:
  81. self.assertTrue('end in a slash' in str(e))
  82. settings.DEBUG = False
  83. def test_append_slash_disabled(self):
  84. """
  85. Tests disabling append slash functionality.
  86. """
  87. settings.APPEND_SLASH = False
  88. request = self._get_request('slash')
  89. self.assertEqual(CommonMiddleware().process_request(request), None)
  90. def test_append_slash_quoted(self):
  91. """
  92. Tests that URLs which require quoting are redirected to their slash
  93. version ok.
  94. """
  95. settings.APPEND_SLASH = True
  96. request = self._get_request('needsquoting#')
  97. r = CommonMiddleware().process_request(request)
  98. self.assertEqual(r.status_code, 301)
  99. self.assertEqual(
  100. r['Location'],
  101. 'http://testserver/middleware/needsquoting%23/')
  102. def test_prepend_www(self):
  103. settings.PREPEND_WWW = True
  104. settings.APPEND_SLASH = False
  105. request = self._get_request('path/')
  106. r = CommonMiddleware().process_request(request)
  107. self.assertEqual(r.status_code, 301)
  108. self.assertEqual(
  109. r['Location'],
  110. 'http://www.testserver/middleware/path/')
  111. def test_prepend_www_append_slash_have_slash(self):
  112. settings.PREPEND_WWW = True
  113. settings.APPEND_SLASH = True
  114. request = self._get_request('slash/')
  115. r = CommonMiddleware().process_request(request)
  116. self.assertEqual(r.status_code, 301)
  117. self.assertEqual(r['Location'],
  118. 'http://www.testserver/middleware/slash/')
  119. def test_prepend_www_append_slash_slashless(self):
  120. settings.PREPEND_WWW = True
  121. settings.APPEND_SLASH = True
  122. request = self._get_request('slash')
  123. r = CommonMiddleware().process_request(request)
  124. self.assertEqual(r.status_code, 301)
  125. self.assertEqual(r['Location'],
  126. 'http://www.testserver/middleware/slash/')
  127. # The following tests examine expected behavior given a custom urlconf that
  128. # overrides the default one through the request object.
  129. def test_append_slash_have_slash_custom_urlconf(self):
  130. """
  131. Tests that URLs with slashes go unmolested.
  132. """
  133. settings.APPEND_SLASH = True
  134. request = self._get_request('customurlconf/slash/')
  135. request.urlconf = 'regressiontests.middleware.extra_urls'
  136. self.assertEqual(CommonMiddleware().process_request(request), None)
  137. def test_append_slash_slashless_resource_custom_urlconf(self):
  138. """
  139. Tests that matches to explicit slashless URLs go unmolested.
  140. """
  141. settings.APPEND_SLASH = True
  142. request = self._get_request('customurlconf/noslash')
  143. request.urlconf = 'regressiontests.middleware.extra_urls'
  144. self.assertEqual(CommonMiddleware().process_request(request), None)
  145. def test_append_slash_slashless_unknown_custom_urlconf(self):
  146. """
  147. Tests that APPEND_SLASH doesn't redirect to unknown resources.
  148. """
  149. settings.APPEND_SLASH = True
  150. request = self._get_request('customurlconf/unknown')
  151. request.urlconf = 'regressiontests.middleware.extra_urls'
  152. self.assertEqual(CommonMiddleware().process_request(request), None)
  153. def test_append_slash_redirect_custom_urlconf(self):
  154. """
  155. Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
  156. """
  157. settings.APPEND_SLASH = True
  158. request = self._get_request('customurlconf/slash')
  159. request.urlconf = 'regressiontests.middleware.extra_urls'
  160. r = CommonMiddleware().process_request(request)
  161. self.assertFalse(r is None,
  162. "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
  163. self.assertEqual(r.status_code, 301)
  164. self.assertEqual(r['Location'], 'http://testserver/middleware/customurlconf/slash/')
  165. def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
  166. """
  167. Tests that while in debug mode, an exception is raised with a warning
  168. when a failed attempt is made to POST to an URL which would normally be
  169. redirected to a slashed version.
  170. """
  171. settings.APPEND_SLASH = True
  172. settings.DEBUG = True
  173. request = self._get_request('customurlconf/slash')
  174. request.urlconf = 'regressiontests.middleware.extra_urls'
  175. request.method = 'POST'
  176. self.assertRaises(
  177. RuntimeError,
  178. CommonMiddleware().process_request,
  179. request)
  180. try:
  181. CommonMiddleware().process_request(request)
  182. except RuntimeError, e:
  183. self.assertTrue('end in a slash' in str(e))
  184. settings.DEBUG = False
  185. def test_append_slash_disabled_custom_urlconf(self):
  186. """
  187. Tests disabling append slash functionality.
  188. """
  189. settings.APPEND_SLASH = False
  190. request = self._get_request('customurlconf/slash')
  191. request.urlconf = 'regressiontests.middleware.extra_urls'
  192. self.assertEqual(CommonMiddleware().process_request(request), None)
  193. def test_append_slash_quoted_custom_urlconf(self):
  194. """
  195. Tests that URLs which require quoting are redirected to their slash
  196. version ok.
  197. """
  198. settings.APPEND_SLASH = True
  199. request = self._get_request('customurlconf/needsquoting#')
  200. request.urlconf = 'regressiontests.middleware.extra_urls'
  201. r = CommonMiddleware().process_request(request)
  202. self.assertFalse(r is None,
  203. "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
  204. self.assertEqual(r.status_code, 301)
  205. self.assertEqual(
  206. r['Location'],
  207. 'http://testserver/middleware/customurlconf/needsquoting%23/')
  208. def test_prepend_www_custom_urlconf(self):
  209. settings.PREPEND_WWW = True
  210. settings.APPEND_SLASH = False
  211. request = self._get_request('customurlconf/path/')
  212. request.urlconf = 'regressiontests.middleware.extra_urls'
  213. r = CommonMiddleware().process_request(request)
  214. self.assertEqual(r.status_code, 301)
  215. self.assertEqual(
  216. r['Location'],
  217. 'http://www.testserver/middleware/customurlconf/path/')
  218. def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
  219. settings.PREPEND_WWW = True
  220. settings.APPEND_SLASH = True
  221. request = self._get_request('customurlconf/slash/')
  222. request.urlconf = 'regressiontests.middleware.extra_urls'
  223. r = CommonMiddleware().process_request(request)
  224. self.assertEqual(r.status_code, 301)
  225. self.assertEqual(r['Location'],
  226. 'http://www.testserver/middleware/customurlconf/slash/')
  227. def test_prepend_www_append_slash_slashless_custom_urlconf(self):
  228. settings.PREPEND_WWW = True
  229. settings.APPEND_SLASH = True
  230. request = self._get_request('customurlconf/slash')
  231. request.urlconf = 'regressiontests.middleware.extra_urls'
  232. r = CommonMiddleware().process_request(request)
  233. self.assertEqual(r.status_code, 301)
  234. self.assertEqual(r['Location'],
  235. 'http://www.testserver/middleware/customurlconf/slash/')
  236. # Tests for the 404 error reporting via email
  237. def test_404_error_reporting(self):
  238. settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
  239. settings.SEND_BROKEN_LINK_EMAILS = True
  240. request = self._get_request('regular_url/that/does/not/exist')
  241. request.META['HTTP_REFERER'] = '/another/url/'
  242. response = self.client.get(request.path)
  243. CommonMiddleware().process_response(request, response)
  244. self.assertEqual(len(mail.outbox), 1)
  245. self.assertIn('Broken', mail.outbox[0].subject)
  246. def test_404_error_reporting_no_referer(self):
  247. settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
  248. settings.SEND_BROKEN_LINK_EMAILS = True
  249. request = self._get_request('regular_url/that/does/not/exist')
  250. response = self.client.get(request.path)
  251. CommonMiddleware().process_response(request, response)
  252. self.assertEqual(len(mail.outbox), 0)
  253. def test_404_error_reporting_ignored_url(self):
  254. settings.IGNORABLE_404_URLS = (re.compile(r'foo'),)
  255. settings.SEND_BROKEN_LINK_EMAILS = True
  256. request = self._get_request('foo_url/that/does/not/exist/either')
  257. request.META['HTTP_REFERER'] = '/another/url/'
  258. response = self.client.get(request.path)
  259. CommonMiddleware().process_response(request, response)
  260. self.assertEqual(len(mail.outbox), 0)
  261. class ConditionalGetMiddlewareTest(TestCase):
  262. urls = 'regressiontests.middleware.cond_get_urls'
  263. def setUp(self):
  264. self.req = HttpRequest()
  265. self.req.META = {
  266. 'SERVER_NAME': 'testserver',
  267. 'SERVER_PORT': 80,
  268. }
  269. self.req.path = self.req.path_info = "/"
  270. self.resp = self.client.get(self.req.path)
  271. # Tests for the Date header
  272. def test_date_header_added(self):
  273. self.assertFalse('Date' in self.resp)
  274. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  275. self.assertTrue('Date' in self.resp)
  276. # Tests for the Content-Length header
  277. def test_content_length_header_added(self):
  278. content_length = len(self.resp.content)
  279. self.assertFalse('Content-Length' in self.resp)
  280. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  281. self.assertTrue('Content-Length' in self.resp)
  282. self.assertEqual(int(self.resp['Content-Length']), content_length)
  283. def test_content_length_header_not_changed(self):
  284. bad_content_length = len(self.resp.content) + 10
  285. self.resp['Content-Length'] = bad_content_length
  286. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  287. self.assertEqual(int(self.resp['Content-Length']), bad_content_length)
  288. # Tests for the ETag header
  289. def test_if_none_match_and_no_etag(self):
  290. self.req.META['HTTP_IF_NONE_MATCH'] = 'spam'
  291. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  292. self.assertEqual(self.resp.status_code, 200)
  293. def test_no_if_none_match_and_etag(self):
  294. self.resp['ETag'] = 'eggs'
  295. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  296. self.assertEqual(self.resp.status_code, 200)
  297. def test_if_none_match_and_same_etag(self):
  298. self.req.META['HTTP_IF_NONE_MATCH'] = self.resp['ETag'] = 'spam'
  299. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  300. self.assertEqual(self.resp.status_code, 304)
  301. def test_if_none_match_and_different_etag(self):
  302. self.req.META['HTTP_IF_NONE_MATCH'] = 'spam'
  303. self.resp['ETag'] = 'eggs'
  304. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  305. self.assertEqual(self.resp.status_code, 200)
  306. # Tests for the Last-Modified header
  307. def test_if_modified_since_and_no_last_modified(self):
  308. self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
  309. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  310. self.assertEqual(self.resp.status_code, 200)
  311. def test_no_if_modified_since_and_last_modified(self):
  312. self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
  313. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  314. self.assertEqual(self.resp.status_code, 200)
  315. def test_if_modified_since_and_same_last_modified(self):
  316. self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
  317. self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
  318. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  319. self.assertEqual(self.resp.status_code, 304)
  320. def test_if_modified_since_and_last_modified_in_the_past(self):
  321. self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
  322. self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:35:44 GMT'
  323. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  324. self.assertEqual(self.resp.status_code, 304)
  325. def test_if_modified_since_and_last_modified_in_the_future(self):
  326. self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
  327. self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:41:44 GMT'
  328. self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
  329. self.assertEqual(self.resp.status_code, 200)
  330. class XFrameOptionsMiddlewareTest(TestCase):
  331. """
  332. Tests for the X-Frame-Options clickjacking prevention middleware.
  333. """
  334. def setUp(self):
  335. self.x_frame_options = settings.X_FRAME_OPTIONS
  336. def tearDown(self):
  337. settings.X_FRAME_OPTIONS = self.x_frame_options
  338. def test_same_origin(self):
  339. """
  340. Tests that the X_FRAME_OPTIONS setting can be set to SAMEORIGIN to
  341. have the middleware use that value for the HTTP header.
  342. """
  343. settings.X_FRAME_OPTIONS = 'SAMEORIGIN'
  344. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  345. HttpResponse())
  346. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  347. settings.X_FRAME_OPTIONS = 'sameorigin'
  348. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  349. HttpResponse())
  350. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  351. def test_deny(self):
  352. """
  353. Tests that the X_FRAME_OPTIONS setting can be set to DENY to
  354. have the middleware use that value for the HTTP header.
  355. """
  356. settings.X_FRAME_OPTIONS = 'DENY'
  357. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  358. HttpResponse())
  359. self.assertEqual(r['X-Frame-Options'], 'DENY')
  360. settings.X_FRAME_OPTIONS = 'deny'
  361. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  362. HttpResponse())
  363. self.assertEqual(r['X-Frame-Options'], 'DENY')
  364. def test_defaults_sameorigin(self):
  365. """
  366. Tests that if the X_FRAME_OPTIONS setting is not set then it defaults
  367. to SAMEORIGIN.
  368. """
  369. del settings.X_FRAME_OPTIONS
  370. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  371. HttpResponse())
  372. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  373. def test_dont_set_if_set(self):
  374. """
  375. Tests that if the X-Frame-Options header is already set then the
  376. middleware does not attempt to override it.
  377. """
  378. settings.X_FRAME_OPTIONS = 'DENY'
  379. response = HttpResponse()
  380. response['X-Frame-Options'] = 'SAMEORIGIN'
  381. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  382. response)
  383. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  384. settings.X_FRAME_OPTIONS = 'SAMEORIGIN'
  385. response = HttpResponse()
  386. response['X-Frame-Options'] = 'DENY'
  387. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  388. response)
  389. self.assertEqual(r['X-Frame-Options'], 'DENY')
  390. def test_response_exempt(self):
  391. """
  392. Tests that if the response has a xframe_options_exempt attribute set
  393. to False then it still sets the header, but if it's set to True then
  394. it does not.
  395. """
  396. settings.X_FRAME_OPTIONS = 'SAMEORIGIN'
  397. response = HttpResponse()
  398. response.xframe_options_exempt = False
  399. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  400. response)
  401. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  402. response = HttpResponse()
  403. response.xframe_options_exempt = True
  404. r = XFrameOptionsMiddleware().process_response(HttpRequest(),
  405. response)
  406. self.assertEqual(r.get('X-Frame-Options', None), None)
  407. def test_is_extendable(self):
  408. """
  409. Tests that the XFrameOptionsMiddleware method that determines the
  410. X-Frame-Options header value can be overridden based on something in
  411. the request or response.
  412. """
  413. class OtherXFrameOptionsMiddleware(XFrameOptionsMiddleware):
  414. # This is just an example for testing purposes...
  415. def get_xframe_options_value(self, request, response):
  416. if getattr(request, 'sameorigin', False):
  417. return 'SAMEORIGIN'
  418. if getattr(response, 'sameorigin', False):
  419. return 'SAMEORIGIN'
  420. return 'DENY'
  421. settings.X_FRAME_OPTIONS = 'DENY'
  422. response = HttpResponse()
  423. response.sameorigin = True
  424. r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
  425. response)
  426. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  427. request = HttpRequest()
  428. request.sameorigin = True
  429. r = OtherXFrameOptionsMiddleware().process_response(request,
  430. HttpResponse())
  431. self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
  432. settings.X_FRAME_OPTIONS = 'SAMEORIGIN'
  433. r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
  434. HttpResponse())
  435. self.assertEqual(r['X-Frame-Options'], 'DENY')
  436. class GZipMiddlewareTest(TestCase):
  437. """
  438. Tests the GZip middleware.
  439. """
  440. short_string = "This string is too short to be worth compressing."
  441. compressible_string = 'a' * 500
  442. uncompressible_string = ''.join(chr(random.randint(0, 255)) for _ in xrange(500))
  443. def setUp(self):
  444. self.req = HttpRequest()
  445. self.req.META = {
  446. 'SERVER_NAME': 'testserver',
  447. 'SERVER_PORT': 80,
  448. }
  449. self.req.path = self.req.path_info = "/"
  450. self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate'
  451. self.req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'
  452. self.resp = HttpResponse()
  453. self.resp.status_code = 200
  454. self.resp.content = self.compressible_string
  455. self.resp['Content-Type'] = 'text/html; charset=UTF-8'
  456. @staticmethod
  457. def decompress(gzipped_string):
  458. return gzip.GzipFile(mode='rb', fileobj=StringIO.StringIO(gzipped_string)).read()
  459. def test_compress_response(self):
  460. """
  461. Tests that compression is performed on responses with compressible content.
  462. """
  463. r = GZipMiddleware().process_response(self.req, self.resp)
  464. self.assertEqual(self.decompress(r.content), self.compressible_string)
  465. self.assertEqual(r.get('Content-Encoding'), 'gzip')
  466. self.assertEqual(r.get('Content-Length'), str(len(r.content)))
  467. def test_compress_non_200_response(self):
  468. """
  469. Tests that compression is performed on responses with a status other than 200.
  470. See #10762.
  471. """
  472. self.resp.status_code = 404
  473. r = GZipMiddleware().process_response(self.req, self.resp)
  474. self.assertEqual(self.decompress(r.content), self.compressible_string)
  475. self.assertEqual(r.get('Content-Encoding'), 'gzip')
  476. def test_no_compress_short_response(self):
  477. """
  478. Tests that compression isn't performed on responses with short content.
  479. """
  480. self.resp.content = self.short_string
  481. r = GZipMiddleware().process_response(self.req, self.resp)
  482. self.assertEqual(r.content, self.short_string)
  483. self.assertEqual(r.get('Content-Encoding'), None)
  484. def test_no_compress_compressed_response(self):
  485. """
  486. Tests that compression isn't performed on responses that are already compressed.
  487. """
  488. self.resp['Content-Encoding'] = 'deflate'
  489. r = GZipMiddleware().process_response(self.req, self.resp)
  490. self.assertEqual(r.content, self.compressible_string)
  491. self.assertEqual(r.get('Content-Encoding'), 'deflate')
  492. def test_no_compress_ie_js_requests(self):
  493. """
  494. Tests that compression isn't performed on JavaScript requests from Internet Explorer.
  495. """
  496. self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)'
  497. self.resp['Content-Type'] = 'application/javascript; charset=UTF-8'
  498. r = GZipMiddleware().process_response(self.req, self.resp)
  499. self.assertEqual(r.content, self.compressible_string)
  500. self.assertEqual(r.get('Content-Encoding'), None)
  501. def test_no_compress_uncompressible_response(self):
  502. """
  503. Tests that compression isn't performed on responses with uncompressible content.
  504. """
  505. self.resp.content = self.uncompressible_string
  506. r = GZipMiddleware().process_response(self.req, self.resp)
  507. self.assertEqual(r.content, self.uncompressible_string)
  508. self.assertEqual(r.get('Content-Encoding'), None)