linkcheck: Rewrite headers tests without mocking

Makes the test more realistic by issuing an HTTP request.
Reduces coupling between test and the code under test.
This commit is contained in:
François Freitag
2020-11-11 16:26:22 +01:00
parent 341b98f794
commit ce9d4c1e68

View File

@@ -163,36 +163,54 @@ def test_auth_header_no_match(app, capsys):
@pytest.mark.sphinx( @pytest.mark.sphinx(
'linkcheck', testroot='linkcheck', freshenv=True, 'linkcheck', testroot='linkcheck-localserver', freshenv=True,
confoverrides={'linkcheck_request_headers': { confoverrides={'linkcheck_request_headers': {
"https://localhost:7777/": { "http://localhost:7777/": {
"Accept": "text/html", "Accept": "text/html",
}, },
"http://www.sphinx-doc.org": { # no slash at the end
"Accept": "application/json",
},
"*": { "*": {
"X-Secret": "open sesami", "X-Secret": "open sesami",
} }
}}) }})
def test_linkcheck_request_headers(app): def test_linkcheck_request_headers(app, capsys):
mock_req = mock.MagicMock() with http_server(HeadersDumperHandler):
mock_req.return_value = 'fake-response'
with mock.patch.multiple('requests', get=mock_req, head=mock_req):
app.builder.build_all() app.builder.build_all()
for args, kwargs in mock_req.call_args_list:
url = args[0] stdout, _stderr = capsys.readouterr()
headers = kwargs.get('headers', {}) assert "Accept: text/html\n" in stdout
if "https://localhost:7777" in url: assert "X-Secret" not in stdout
assert headers["Accept"] == "text/html" assert "sesami" not in stdout
elif 'http://www.sphinx-doc.org' in url:
assert headers["Accept"] == "application/json"
elif 'https://www.google.com' in url: @pytest.mark.sphinx(
assert headers["Accept"] == "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8" 'linkcheck', testroot='linkcheck-localserver', freshenv=True,
assert headers["X-Secret"] == "open sesami" confoverrides={'linkcheck_request_headers': {
else: "http://localhost:7777": {"Accept": "application/json"},
assert headers["Accept"] == "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8" "*": {"X-Secret": "open sesami"}
}})
def test_linkcheck_request_headers_no_slash(app, capsys):
with http_server(HeadersDumperHandler):
app.builder.build_all()
stdout, _stderr = capsys.readouterr()
assert "Accept: application/json\n" in stdout
assert "X-Secret" not in stdout
assert "sesami" not in stdout
@pytest.mark.sphinx(
'linkcheck', testroot='linkcheck-localserver', freshenv=True,
confoverrides={'linkcheck_request_headers': {
"http://do.not.match.org": {"Accept": "application/json"},
"*": {"X-Secret": "open sesami"}
}})
def test_linkcheck_request_headers_default(app, capsys):
with http_server(HeadersDumperHandler):
app.builder.build_all()
stdout, _stderr = capsys.readouterr()
assert "Accepts: application/json\n" not in stdout
assert "X-Secret: open sesami\n" in stdout
def make_redirect_handler(*, support_head): def make_redirect_handler(*, support_head):
class RedirectOnceHandler(http.server.BaseHTTPRequestHandler): class RedirectOnceHandler(http.server.BaseHTTPRequestHandler):