| 22 | |
| 23 | |
| 24 | class TestCacheControllerResponse: |
| 25 | url = "http://url.com/" |
| 26 | |
| 27 | def req(self, headers=None): |
| 28 | headers = headers or {} |
| 29 | return Mock(full_url=self.url, url=self.url, headers=headers) # < 1.x support |
| 30 | |
| 31 | def resp(self, headers=None): |
| 32 | headers = headers or {} |
| 33 | return Mock( |
| 34 | status=200, headers=headers, request=self.req(), read=lambda **k: b"testing" |
| 35 | ) |
| 36 | |
| 37 | @pytest.fixture() |
| 38 | def cc(self): |
| 39 | # Cache controller fixture |
| 40 | return CacheController(Mock(), serializer=Mock()) |
| 41 | |
| 42 | def test_no_cache_non_20x_response(self, cc): |
| 43 | # No caching without some extra headers, so we add them |
| 44 | now = time.strftime(TIME_FMT, time.gmtime()) |
| 45 | resp = self.resp({"cache-control": "max-age=3600", "date": now}) |
| 46 | |
| 47 | no_cache_codes = [201, 400, 500] |
| 48 | for code in no_cache_codes: |
| 49 | resp.status = code |
| 50 | cc.cache_response(Mock(), resp) |
| 51 | assert not cc.cache.set.called |
| 52 | |
| 53 | # this should work b/c the resp is 20x |
| 54 | resp.status = 203 |
| 55 | cc.cache_response(self.req(), resp) |
| 56 | assert cc.serializer.dumps.called |
| 57 | assert cc.cache.set.called |
| 58 | |
| 59 | def test_no_cache_with_no_date(self, cc): |
| 60 | # No date header which makes our max-age pointless |
| 61 | resp = self.resp({"cache-control": "max-age=3600"}) |
| 62 | cc.cache_response(self.req(), resp) |
| 63 | |
| 64 | assert not cc.cache.set.called |
| 65 | |
| 66 | def test_no_cache_with_wrong_sized_body(self, cc): |
| 67 | # When the body is the wrong size, then we don't want to cache it |
| 68 | # because it is obviously broken. |
| 69 | resp = self.resp({"cache-control": "max-age=3600", "Content-Length": "5"}) |
| 70 | cc.cache_response(self.req(), resp, b"0" * 10) |
| 71 | |
| 72 | assert not cc.cache.set.called |
| 73 | |
| 74 | def test_cache_response_no_cache_control(self, cc): |
| 75 | resp = self.resp() |
| 76 | cc.cache_response(self.req(), resp) |
| 77 | |
| 78 | assert not cc.cache.set.called |
| 79 | |
| 80 | def test_cache_response_cache_max_age(self, cc): |
| 81 | now = time.strftime(TIME_FMT, time.gmtime()) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…