mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
linkcheck: Test redirect following for HEAD and GET
This commit is contained in:
parent
6ea0a123c6
commit
53ca608bc0
1
tests/roots/test-linkcheck-localserver/conf.py
Normal file
1
tests/roots/test-linkcheck-localserver/conf.py
Normal file
@ -0,0 +1 @@
|
||||
exclude_patterns = ['_build']
|
1
tests/roots/test-linkcheck-localserver/index.rst
Normal file
1
tests/roots/test-linkcheck-localserver/index.rst
Normal file
@ -0,0 +1 @@
|
||||
`local server <http://localhost:7777/>`_
|
@ -11,6 +11,7 @@
|
||||
import http.server
|
||||
import json
|
||||
import re
|
||||
import textwrap
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
@ -179,3 +180,61 @@ def test_linkcheck_request_headers(app):
|
||||
assert headers["X-Secret"] == "open sesami"
|
||||
else:
|
||||
assert headers["Accept"] == "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8"
|
||||
|
||||
def make_redirect_handler(*, support_head):
|
||||
class RedirectOnceHandler(http.server.BaseHTTPRequestHandler):
|
||||
def do_HEAD(self):
|
||||
if support_head:
|
||||
self.do_GET()
|
||||
else:
|
||||
self.send_response(405, "Method Not Allowed")
|
||||
self.end_headers()
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/?redirected=1":
|
||||
self.send_response(204, "No content")
|
||||
else:
|
||||
self.send_response(302, "Found")
|
||||
self.send_header("Location", "http://localhost:7777/?redirected=1")
|
||||
self.end_headers()
|
||||
|
||||
def log_date_time_string(self):
|
||||
"""Strip date and time from logged messages for assertions."""
|
||||
return ""
|
||||
|
||||
return RedirectOnceHandler
|
||||
|
||||
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)
|
||||
def test_follows_redirects_on_HEAD(app, capsys):
|
||||
with http_server(make_redirect_handler(support_head=True)):
|
||||
app.builder.build_all()
|
||||
stdout, stderr = capsys.readouterr()
|
||||
content = (app.outdir / 'output.txt').read_text()
|
||||
assert content == (
|
||||
"index.rst:1: [redirected with Found] "
|
||||
"http://localhost:7777/ to http://localhost:7777/?redirected=1\n"
|
||||
)
|
||||
assert stderr == textwrap.dedent(
|
||||
"""\
|
||||
127.0.0.1 - - [] "HEAD / HTTP/1.1" 302 -
|
||||
127.0.0.1 - - [] "HEAD /?redirected=1 HTTP/1.1" 204 -
|
||||
"""
|
||||
)
|
||||
|
||||
@pytest.mark.sphinx('linkcheck', testroot='linkcheck-localserver', freshenv=True)
|
||||
def test_follows_redirects_on_GET(app, capsys):
|
||||
with http_server(make_redirect_handler(support_head=False)):
|
||||
app.builder.build_all()
|
||||
stdout, stderr = capsys.readouterr()
|
||||
content = (app.outdir / 'output.txt').read_text()
|
||||
assert content == (
|
||||
"index.rst:1: [redirected with Found] "
|
||||
"http://localhost:7777/ to http://localhost:7777/?redirected=1\n"
|
||||
)
|
||||
assert stderr == textwrap.dedent(
|
||||
"""\
|
||||
127.0.0.1 - - [] "HEAD / HTTP/1.1" 405 -
|
||||
127.0.0.1 - - [] "GET / HTTP/1.1" 302 -
|
||||
127.0.0.1 - - [] "GET /?redirected=1 HTTP/1.1" 204 -
|
||||
"""
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user