gutendex: send a User-Agent (Cloudflare 403s the default urllib agent)
Gutendex.com sits behind Cloudflare, which rejects urllib's default
`Python-urllib/<ver>` User-Agent with HTTP 403. Both skills therefore failed
on their first real call ("could not reach the Gutendex catalog ... HTTP Error
403"); the hermetic loopback tests cannot catch this by design.
Send an identifying `USER_AGENT` on every request (the /books catalog lookup
and the plain-text download) and assert it in each skill's test so the
regression is caught locally.
Verified with a real run: find_books returned 41 live results for
"jane austen"; read_book downloaded and saved Pride and Prejudice (1342).
This commit is contained in:
1 parent
b775069613
commit
52346baf52
4 files changed
+67
-5
No files matched your search
@@ -6,6 +6,9 @@ request's search terms are taken from the user's query and the catalog is
|
||||
searched by author/title (`search`) or by genre/subject (`topic`) — which one
|
||||
is a SemIf decision over the real query. A real HTTP failure is reported
|
||||
honestly; book data is never invented.
|
||||
|
||||
Gutendex sits behind Cloudflare, which rejects the default `Python-urllib`
|
||||
User-Agent with HTTP 403, so every request identifies itself with `USER_AGENT`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -29,6 +32,10 @@ CONTRACT = {
|
||||
"gutendex_base_url": "Base URL of the Gutendex Project Gutenberg API, e.g. https://gutendex.com (no trailing slash needed).",
|
||||
}
|
||||
|
||||
# Gutendex's Cloudflare front blocks the default `Python-urllib/<ver>` agent
|
||||
# with HTTP 403; a plain, identifying agent is accepted.
|
||||
USER_AGENT = "semif-agent/1.0"
|
||||
|
||||
TIMEOUT_SECONDS = 20
|
||||
MAX_RESULTS = 5
|
||||
RESULT_CHARS = 300
|
||||
@@ -88,7 +95,9 @@ def _get_json(base_url, path, params=None):
|
||||
url = base_url + path
|
||||
if params:
|
||||
url += "?" + urllib.parse.urlencode(params)
|
||||
http_request = urllib.request.Request(url, headers={"Accept": "application/json"})
|
||||
http_request = urllib.request.Request(
|
||||
url, headers={"Accept": "application/json", "User-Agent": USER_AGENT}
|
||||
)
|
||||
with urllib.request.urlopen(http_request, timeout=TIMEOUT_SECONDS) as response:
|
||||
return json.loads(response.read().decode("utf-8"))
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
def do_GET(self):
|
||||
self.server.seen.append(self.path)
|
||||
self.server.user_agents.append(self.headers.get("User-Agent"))
|
||||
if self.server.status != 200:
|
||||
self._send({"detail": "boom"}, self.server.status)
|
||||
return
|
||||
@@ -86,9 +87,10 @@ class Handler(BaseHTTPRequestHandler):
|
||||
|
||||
|
||||
def start_server(status=200, results=None, count=None):
|
||||
handler = type("Handler", (Handler,), {"status": status, "seen": []})
|
||||
handler = type("Handler", (Handler,), {"status": status, "seen": [], "user_agents": []})
|
||||
server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
||||
server.seen = handler.seen
|
||||
server.user_agents = handler.user_agents
|
||||
server.status = status
|
||||
server.results = FIXTURE_BOOKS if results is None else results
|
||||
server.count = len(server.results) if count is None else count
|
||||
@@ -146,6 +148,20 @@ def test_empty_result_is_reported_plainly():
|
||||
server.server_close()
|
||||
|
||||
|
||||
def test_sends_an_identifying_user_agent():
|
||||
# Gutendex's Cloudflare front 403s the default `Python-urllib/<ver>` agent,
|
||||
# so the body must identify itself (the real-service bug this guards).
|
||||
server, url = start_server()
|
||||
try:
|
||||
run(url, "find books by charles dickens", selected="search")
|
||||
assert server.user_agents == [skill.USER_AGENT], server.user_agents
|
||||
assert not str(server.user_agents[0]).startswith("Python-urllib"), server.user_agents
|
||||
print("user-agent:", server.user_agents[0])
|
||||
finally:
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
|
||||
|
||||
def test_http_failure_is_reported_honestly():
|
||||
server, url = start_server(status=503)
|
||||
try:
|
||||
@@ -175,6 +191,7 @@ def main():
|
||||
test_searches_by_author_or_title()
|
||||
test_searches_by_genre_via_topic()
|
||||
test_empty_result_is_reported_plainly()
|
||||
test_sends_an_identifying_user_agent()
|
||||
test_http_failure_is_reported_honestly()
|
||||
test_missing_query_asks_for_one()
|
||||
print("ok")
|
||||
|
||||
Reference in new issue
Block a user