| 130 | |
| 131 | |
| 132 | class TestModifiedUnitTests: |
| 133 | def last_modified(self, period): |
| 134 | return time.strftime(TIME_FMT, time.gmtime(self.time_now - period)) |
| 135 | |
| 136 | def setup_method(self): |
| 137 | self.heuristic = LastModified() |
| 138 | self.time_now = time.time() |
| 139 | day_in_seconds = 86400 |
| 140 | self.year_ago = self.last_modified(day_in_seconds * 365) |
| 141 | self.week_ago = self.last_modified(day_in_seconds * 7) |
| 142 | self.day_ago = self.last_modified(day_in_seconds) |
| 143 | self.now = self.last_modified(0) |
| 144 | |
| 145 | # NOTE: We pass in a negative to get a positive... Probably |
| 146 | # should refactor. |
| 147 | self.day_ahead = self.last_modified(-day_in_seconds) |
| 148 | |
| 149 | def test_no_expiry_is_inferred_when_no_last_modified_is_present(self): |
| 150 | assert self.heuristic.update_headers(DummyResponse(200, {})) == {} |
| 151 | |
| 152 | def test_expires_is_not_replaced_when_present(self): |
| 153 | resp = DummyResponse(200, {"Expires": self.day_ahead}) |
| 154 | assert self.heuristic.update_headers(resp) == {} |
| 155 | |
| 156 | def test_last_modified_is_used(self): |
| 157 | resp = DummyResponse(200, {"Date": self.now, "Last-Modified": self.week_ago}) |
| 158 | modified = self.heuristic.update_headers(resp) |
| 159 | assert ["expires"] == list(modified.keys()) |
| 160 | |
| 161 | expected = datetime(*parsedate(modified["expires"])[:6], tzinfo=timezone.utc) |
| 162 | assert expected > datetime.now(timezone.utc) |
| 163 | |
| 164 | def test_last_modified_is_not_used_when_cache_control_present(self): |
| 165 | resp = DummyResponse( |
| 166 | 200, |
| 167 | { |
| 168 | "Date": self.now, |
| 169 | "Last-Modified": self.week_ago, |
| 170 | "Cache-Control": "private", |
| 171 | }, |
| 172 | ) |
| 173 | assert self.heuristic.update_headers(resp) == {} |
| 174 | |
| 175 | def test_last_modified_is_not_used_when_status_is_unknown(self): |
| 176 | resp = DummyResponse(299, {"Date": self.now, "Last-Modified": self.week_ago}) |
| 177 | assert self.heuristic.update_headers(resp) == {} |
| 178 | |
| 179 | def test_last_modified_is_used_when_cache_control_public(self): |
| 180 | resp = DummyResponse( |
| 181 | 200, |
| 182 | { |
| 183 | "Date": self.now, |
| 184 | "Last-Modified": self.week_ago, |
| 185 | "Cache-Control": "public", |
| 186 | }, |
| 187 | ) |
| 188 | modified = self.heuristic.update_headers(resp) |
| 189 | assert ["expires"] == list(modified.keys()) |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…