linkcheck: Use context managers for HTTP requests (#11318)

This closes HTTP responses when no content reads are required, as
when requests are made in streaming mode, ``requests`` doesn't know
whether the caller may intend to later read content from a streamed
HTTP response object and holds the socket open.

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
James Addison
2023-05-09 17:09:35 +01:00
committed by GitHub
co-authored by Adam Turner
parent 2b1c106bbf
commit c9d0933e5d
13 changed files with 38 additions and 24 deletions
@@ -1,3 +1,3 @@
exclude_patterns = ['_build']
linkcheck_anchors = True
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
@@ -3,4 +3,4 @@ linkcheck_exclude_documents = [
'^broken_link$',
'br[0-9]ken_link',
]
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
@@ -1,3 +1,3 @@
exclude_patterns = ['_build']
linkcheck_anchors = True
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
@@ -1,2 +1,2 @@
exclude_patterns = ['_build']
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
@@ -1,2 +1,2 @@
exclude_patterns = ['_build']
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
@@ -1,2 +1,2 @@
exclude_patterns = ['_build']
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
+1 -1
View File
@@ -1,2 +1,2 @@
exclude_patterns = ['_build']
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
@@ -1,3 +1,3 @@
exclude_patterns = ['_build']
linkcheck_anchors = True
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
+1 -1
View File
@@ -1,4 +1,4 @@
root_doc = 'links'
exclude_patterns = ['_build']
linkcheck_anchors = True
linkcheck_timeout = 0.075
linkcheck_timeout = 0.05
+1
View File
@@ -11,3 +11,4 @@ Some additional anchors to exercise ignore code
.. image:: http://localhost:7777/image.png
.. figure:: http://localhost:7777/image2.png
* `Valid anchored url <http://localhost:7777/anchor.html#found>`_
+17 -2
View File
@@ -31,6 +31,9 @@ class DefaultsHandler(http.server.BaseHTTPRequestHandler):
if self.path[1:].rstrip() == "":
self.send_response(200, "OK")
self.end_headers()
elif self.path[1:].rstrip() == "anchor.html":
self.send_response(200, "OK")
self.end_headers()
else:
self.send_response(404, "Not Found")
self.end_headers()
@@ -39,6 +42,9 @@ class DefaultsHandler(http.server.BaseHTTPRequestHandler):
self.do_HEAD()
if self.path[1:].rstrip() == "":
self.wfile.write(b"ok\n\n")
elif self.path[1:].rstrip() == "anchor.html":
doc = '<!DOCTYPE html><html><body><a id="found"></a></body></html>'
self.wfile.write(doc.encode('utf-8'))
@pytest.mark.sphinx('linkcheck', testroot='linkcheck', freshenv=True)
@@ -69,8 +75,8 @@ def test_defaults(app):
for attr in ("filename", "lineno", "status", "code", "uri", "info"):
assert attr in row
assert len(content.splitlines()) == 9
assert len(rows) == 9
assert len(content.splitlines()) == 10
assert len(rows) == 10
# the output order of the rows is not stable
# due to possible variance in network latency
rowsby = {row["uri"]: row for row in rows}
@@ -95,6 +101,15 @@ def test_defaults(app):
assert rowsby["http://localhost:7777#does-not-exist"]["info"] == "Anchor 'does-not-exist' not found"
# images should fail
assert "Not Found for url: http://localhost:7777/image.png" in rowsby["http://localhost:7777/image.png"]["info"]
# anchor should be found
assert rowsby['http://localhost:7777/anchor.html#found'] == {
'filename': 'links.rst',
'lineno': 14,
'status': 'working',
'code': 0,
'uri': 'http://localhost:7777/anchor.html#found',
'info': '',
}
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-too-many-retries', freshenv=True)