| | 501 | |
| | 502 | |
| | 503 | class GZipMiddlewareTest(TestCase): |
| | 504 | """ |
| | 505 | Tests the GZip middleware. |
| | 506 | """ |
| | 507 | short_string = "This string is too short to be worth compressing." |
| | 508 | compressible_string = 'a'*1000 |
| | 509 | uncompressible_string = [] |
| | 510 | for i in xrange(1000): |
| | 511 | uncompressible_string.append(chr(randint(0,255))) |
| | 512 | uncompressible_string = "".join(uncompressible_string) |
| | 513 | |
| | 514 | def setUp(self): |
| | 515 | self.req = HttpRequest() |
| | 516 | self.req.META = { |
| | 517 | 'SERVER_NAME': 'testserver', |
| | 518 | 'SERVER_PORT': 80, |
| | 519 | } |
| | 520 | self.req.path = self.req.path_info = "/" |
| | 521 | self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate' |
| | 522 | self.req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1' |
| | 523 | self.resp = HttpResponse() |
| | 524 | self.resp.status_code = 200 |
| | 525 | self.resp.content = self.compressible_string |
| | 526 | self.resp['Content-Type'] = 'text/html; charset=UTF-8' |
| | 527 | |
| | 528 | # tests if compression is performed on responses with compressible content, |
| | 529 | # and no other prohibitting factors. |
| | 530 | def test_compress_response(self): |
| | 531 | r = GZipMiddleware().process_response(self.req, self.resp) |
| | 532 | self.assertEqual(compress_string(self.compressible_string), r.content) |
| | 533 | self.assertEqual(r.get('Content-Encoding', ''), 'gzip') |
| | 534 | self.assertEqual(r.get('Content-Length', ''), str(len(r.content))) |
| | 535 | self.assertNotEqual(r.content, self.compressible_string) |
| | 536 | |
| | 537 | # tests if compression is performed on responses with a status other than 200. |
| | 538 | def test_no_compress_non_200_response(self): |
| | 539 | self.resp.status_code = 404 |
| | 540 | r = GZipMiddleware().process_response(self.req, self.resp) |
| | 541 | self.assertNotEqual(compress_string(self.compressible_string), r.content) |
| | 542 | self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip') |
| | 543 | self.assertEqual(r.content, self.compressible_string) |
| | 544 | |
| | 545 | # tests if compression is performed on responses with short content |
| | 546 | def test_no_compress_short_response(self): |
| | 547 | self.resp.content = self.short_string |
| | 548 | r = GZipMiddleware().process_response(self.req, self.resp) |
| | 549 | self.assertNotEqual(compress_string(self.short_string), r.content) |
| | 550 | self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip') |
| | 551 | self.assertEqual(r.content, self.short_string) |
| | 552 | |
| | 553 | # tests if compression is performed on responses that are already compressed |
| | 554 | def test_no_compress_compressed_response(self): |
| | 555 | first_content = GZipMiddleware().process_response(self.req, self.resp).content |
| | 556 | r = GZipMiddleware().process_response(self.req, self.resp) |
| | 557 | self.assertEqual(r.content, first_content) |
| | 558 | |
| | 559 | # tests if compression is performed on JavaScript requests from Internet Explorer |
| | 560 | def test_no_compress_ie_js_requests(self): |
| | 561 | self.req.META['HTTP_USER_AGENT'] = 'Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)' |
| | 562 | self.resp['Content-Type'] = 'application/javascript; charset=UTF-8' |
| | 563 | r = GZipMiddleware().process_response(self.req, self.resp) |
| | 564 | self.assertNotEqual(compress_string(self.compressible_string), r.content) |
| | 565 | self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip') |
| | 566 | self.assertEqual(r.content, self.compressible_string) |
| | 567 | |
| | 568 | # tests if compression is performed on responses with uncompressible content |
| | 569 | def test_no_compress_uncompressible_response(self): |
| | 570 | self.resp.content = self.uncompressible_string |
| | 571 | r = GZipMiddleware().process_response(self.req, self.resp) |
| | 572 | self.assertNotEqual(compress_string(self.compressible_string), r.content) |
| | 573 | self.assertNotEqual(r.get('Content-Encoding', ''), 'gzip') |
| | 574 | self.assertEqual(r.content, self.uncompressible_string) |