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
@@ -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