| | 585 | |
| | 586 | |
| | 587 | @override_settings(USE_ETAGS=True) |
| | 588 | class ETagGZipMiddlewareTest(TestCase): |
| | 589 | """ |
| | 590 | Tests if the ETag middleware behaves correctly with GZip middleware. |
| | 591 | """ |
| | 592 | compressible_string = 'a' * 500 |
| | 593 | |
| | 594 | def _get_request(self): |
| | 595 | req = HttpRequest() |
| | 596 | req.META = { |
| | 597 | 'SERVER_NAME': 'testserver', |
| | 598 | 'SERVER_PORT': 80, |
| | 599 | } |
| | 600 | req.path = req.path_info = "/" |
| | 601 | req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate' |
| | 602 | req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1' |
| | 603 | return req |
| | 604 | |
| | 605 | def _get_response(self): |
| | 606 | resp = HttpResponse() |
| | 607 | resp.status_code = 200 |
| | 608 | resp.content = self.compressible_string |
| | 609 | resp['Content-Type'] = 'text/html; charset=UTF-8' |
| | 610 | return resp |
| | 611 | |
| | 612 | def test_compress_response(self): |
| | 613 | """ |
| | 614 | Tests that ETag is changed after gzip compression is performed. |
| | 615 | """ |
| | 616 | req = self._get_request() |
| | 617 | resp = self._get_response() |
| | 618 | r = GZipMiddleware().process_response(req, |
| | 619 | CommonMiddleware().process_response(req, resp)) |
| | 620 | gzip_etag = r.get('ETag') |
| | 621 | |
| | 622 | req = self._get_request() |
| | 623 | req.META['HTTP_ACCEPT_ENCODING'] = '' |
| | 624 | resp = self._get_response() |
| | 625 | r = GZipMiddleware().process_response(req, |
| | 626 | CommonMiddleware().process_response(req, resp)) |
| | 627 | nogzip_etag = r.get('ETag') |
| | 628 | |
| | 629 | self.assertNotEqual(gzip_etag, nogzip_etag) |