gh-86228: Set HTTPResponse.url at init by fbidu · Pull Request #22738 · python/cpython · GitHub
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
4 changes: 4 additions & 0 deletions Lib/http/client.py
16 changes: 16 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,22 @@ def test_response_headers(self):
cookies = r.getheader("Set-Cookie")
self.assertEqual(cookies, hdr)

def test_url_set_at_init(self):
# bpo-42062: HTTPResponse.url (returned by geturl()) is initialized in
# __init__, so it is available on responses created directly via
# http.client and not only on those returned by urllib. Accessing it
# previously raised AttributeError.
body = 'HTTP/1.1 200 OK\r\n\r\nText'
url = 'http://www.python.org/'
resp = client.HTTPResponse(FakeSocket(body), url=url)
self.assertEqual(resp.url, url)
self.assertEqual(resp.geturl(), url)

# When no URL is supplied, geturl() returns None instead of raising.
resp = client.HTTPResponse(FakeSocket(body))
self.assertIsNone(resp.url)
self.assertIsNone(resp.geturl())

def test_read_head(self):
# Test that the library doesn't attempt to read any data
# from a HEAD request. (Tickles SF bug #622042.)
Expand Down
Loading