Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def typing(session):
"""
Check static typing.
"""
session.install("mypy", "types-docutils", "types-lxml", ROOT)
session.install("mypy", "pytest", "types-docutils", "types-lxml", ROOT)
session.run("python", "-m", "mypy", PACKAGE)


Expand Down
3 changes: 2 additions & 1 deletion sphinx_json_schema_spec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from contextlib import suppress
from datetime import UTC, datetime
from email.utils import format_datetime
from importlib import metadata
from pathlib import Path
from urllib.parse import urljoin
Expand Down Expand Up @@ -76,7 +77,7 @@ def fetch_or_load(cache_path, url):

with suppress(FileNotFoundError):
modified = datetime.fromtimestamp(cache_path.stat().st_mtime, UTC)
date = modified.strftime("%a, %d %b %Y %I:%M:%S UTC")
date = format_datetime(modified, usegmt=True)
headers["If-Modified-Since"] = date

request = urllib.request.Request(url, headers=headers)
Expand Down
41 changes: 41 additions & 0 deletions sphinx_json_schema_spec/tests/test_fetching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from datetime import UTC, datetime
from io import BytesIO
import locale
import os

import pytest

from sphinx_json_schema_spec import fetch_or_load


@pytest.mark.parametrize("time_locale", ["C", ""])
def test_conditional_request_uses_http_date(
tmp_path,
monkeypatch,
time_locale,
):
cache_path = tmp_path / "spec.html"
cache_path.write_bytes(b"<html><body>cached</body></html>")
modified = datetime(2025, 1, 2, 15, 4, 5, tzinfo=UTC).timestamp()
os.utime(cache_path, (modified, modified))
requests = []

class Response(BytesIO):
code = 200

def urlopen(request, context):
requests.append(request)
return Response(b"<html><body>fresh</body></html>")

monkeypatch.setattr("urllib.request.urlopen", urlopen)
previous_locale = locale.setlocale(locale.LC_TIME)
try:
locale.setlocale(locale.LC_TIME, time_locale)
result = fetch_or_load(cache_path, "https://example.com/spec.html")
finally:
locale.setlocale(locale.LC_TIME, previous_locale)

assert requests[0].get_header("If-modified-since") == (
"Thu, 02 Jan 2025 15:04:05 GMT"
)
assert result.find("body").text == "fresh"