|
| 1 | +from email.utils import parsedate_to_datetime |
1 | 2 | from urllib.parse import urljoin |
2 | 3 |
|
3 | 4 | import pytest |
@@ -35,6 +36,20 @@ def synced_distro( |
35 | 36 | return python_distribution_factory(repository=repo) |
36 | 37 |
|
37 | 38 |
|
| 39 | +@pytest.fixture |
| 40 | +def synced_distro_no_cache( |
| 41 | + python_remote_factory, |
| 42 | + python_repo_with_sync, |
| 43 | + python_distribution_factory, |
| 44 | +): |
| 45 | + """ |
| 46 | + Sync a repo and create a distribution (no cache requirement). |
| 47 | + """ |
| 48 | + remote = python_remote_factory(includes=PYTHON_SM_PROJECT_SPECIFIER) |
| 49 | + repo = python_repo_with_sync(remote) |
| 50 | + return python_distribution_factory(repository=repo) |
| 51 | + |
| 52 | + |
38 | 53 | @pytest.mark.parallel |
39 | 54 | def test_simple_cache_hit_miss_and_headers(synced_distro): |
40 | 55 | """ |
@@ -139,3 +154,102 @@ def test_simple_cache_etag_conditional_request(synced_distro): |
139 | 154 | assert r3.headers["Cache-Control"] == cache_control |
140 | 155 | assert r3.headers["X-PULP-CACHE"] == "HIT" |
141 | 156 | assert len(r3.content) > 0 |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.parallel |
| 160 | +def test_simple_last_modified_header(synced_distro_no_cache): |
| 161 | + """Simple API responses include Last-Modified header.""" |
| 162 | + index_url = urljoin(synced_distro_no_cache.base_url, "simple/") |
| 163 | + detail_url = f"{index_url}aiohttp" |
| 164 | + |
| 165 | + for url in [index_url, detail_url]: |
| 166 | + r = requests.get(url) |
| 167 | + assert r.status_code == 200 |
| 168 | + assert "Last-Modified" in r.headers |
| 169 | + parsedate_to_datetime(r.headers["Last-Modified"]) |
| 170 | + |
| 171 | + |
| 172 | +@pytest.mark.parallel |
| 173 | +def test_simple_if_modified_since_304(synced_distro_no_cache): |
| 174 | + """If-Modified-Since with matching timestamp returns 304.""" |
| 175 | + url = urljoin(synced_distro_no_cache.base_url, "simple/") |
| 176 | + |
| 177 | + r1 = requests.get(url) |
| 178 | + assert r1.status_code == 200 |
| 179 | + last_modified = r1.headers["Last-Modified"] |
| 180 | + |
| 181 | + r2 = requests.get(url, headers={"If-Modified-Since": last_modified}) |
| 182 | + assert r2.status_code == 304 |
| 183 | + assert len(r2.content) == 0 |
| 184 | + |
| 185 | + |
| 186 | +@pytest.mark.parallel |
| 187 | +def test_simple_if_modified_since_old_timestamp_200(synced_distro_no_cache): |
| 188 | + """If-Modified-Since with old timestamp returns 200 with content.""" |
| 189 | + url = urljoin(synced_distro_no_cache.base_url, "simple/") |
| 190 | + |
| 191 | + r1 = requests.get(url) |
| 192 | + assert r1.status_code == 200 |
| 193 | + |
| 194 | + r2 = requests.get(url, headers={"If-Modified-Since": "Thu, 01 Jan 2009 00:00:00 GMT"}) |
| 195 | + assert r2.status_code == 200 |
| 196 | + assert len(r2.content) > 0 |
| 197 | + |
| 198 | + |
| 199 | +@pytest.mark.parallel |
| 200 | +def test_metadata_conditional_request_headers(synced_distro_no_cache): |
| 201 | + """JSON metadata responses include ETag, Last-Modified, and Cache-Control headers.""" |
| 202 | + url = urljoin(synced_distro_no_cache.base_url, "pypi/aiohttp/json/") |
| 203 | + |
| 204 | + r = requests.get(url) |
| 205 | + assert r.status_code == 200 |
| 206 | + assert r.headers["Cache-Control"] == "max-age=900, public" |
| 207 | + assert "ETag" in r.headers |
| 208 | + assert r.headers["ETag"].startswith('"') and r.headers["ETag"].endswith('"') |
| 209 | + assert "Last-Modified" in r.headers |
| 210 | + parsedate_to_datetime(r.headers["Last-Modified"]) |
| 211 | + |
| 212 | + |
| 213 | +@pytest.mark.parallel |
| 214 | +def test_metadata_etag_conditional_request(synced_distro_no_cache): |
| 215 | + """JSON metadata: matching If-None-Match returns 304, non-matching returns 200.""" |
| 216 | + url = urljoin(synced_distro_no_cache.base_url, "pypi/aiohttp/json/") |
| 217 | + |
| 218 | + r1 = requests.get(url) |
| 219 | + assert r1.status_code == 200 |
| 220 | + etag = r1.headers["ETag"] |
| 221 | + |
| 222 | + r2 = requests.get(url, headers={"If-None-Match": etag}) |
| 223 | + assert r2.status_code == 304 |
| 224 | + assert len(r2.content) == 0 |
| 225 | + |
| 226 | + r3 = requests.get(url, headers={"If-None-Match": '"old"'}) |
| 227 | + assert r3.status_code == 200 |
| 228 | + assert r3.headers["ETag"] == etag |
| 229 | + |
| 230 | + |
| 231 | +@pytest.mark.parallel |
| 232 | +def test_metadata_if_modified_since_304(synced_distro_no_cache): |
| 233 | + """JSON metadata: If-Modified-Since with matching timestamp returns 304.""" |
| 234 | + url = urljoin(synced_distro_no_cache.base_url, "pypi/aiohttp/json/") |
| 235 | + |
| 236 | + r1 = requests.get(url) |
| 237 | + assert r1.status_code == 200 |
| 238 | + last_modified = r1.headers["Last-Modified"] |
| 239 | + |
| 240 | + r2 = requests.get(url, headers={"If-Modified-Since": last_modified}) |
| 241 | + assert r2.status_code == 304 |
| 242 | + assert len(r2.content) == 0 |
| 243 | + |
| 244 | + |
| 245 | +@pytest.mark.parallel |
| 246 | +def test_metadata_if_modified_since_old_timestamp_200(synced_distro_no_cache): |
| 247 | + """JSON metadata: If-Modified-Since with old timestamp returns 200.""" |
| 248 | + url = urljoin(synced_distro_no_cache.base_url, "pypi/aiohttp/json/") |
| 249 | + |
| 250 | + r1 = requests.get(url) |
| 251 | + assert r1.status_code == 200 |
| 252 | + |
| 253 | + r2 = requests.get(url, headers={"If-Modified-Since": "Thu, 01 Jan 2009 00:00:00 GMT"}) |
| 254 | + assert r2.status_code == 200 |
| 255 | + assert len(r2.content) > 0 |
0 commit comments