Skip to content
Merged
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
7 changes: 6 additions & 1 deletion sphinxext/rediraffe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
"""
<html>
<head>
<meta http-equiv="refresh" content="0; url={{rel_url}}"/>
<noscript>
<meta http-equiv="refresh" content="0; url={{rel_url}}"/>
</noscript>
</head>
<body>
<p>You should have been redirected.</p>
<a href="{{rel_url}}">If not, click here to continue.</a>
<script>
window.location.href = '{{rel_url}}' + (window.location.search || '') + (window.location.hash || '');
</script>
</body>
</html>

Expand Down
8 changes: 8 additions & 0 deletions tests/roots/ext/test-pass_url_fragments_queries/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extensions = ["sphinxext.rediraffe"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

rediraffe_redirects = "redirects.txt"
1 change: 1 addition & 0 deletions tests/roots/ext/test-pass_url_fragments_queries/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index File
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
another.rst index.rst
98 changes: 98 additions & 0 deletions tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,55 @@ def test_jinja_bad_path(self, app: Sphinx, ensure_redirect):

ensure_redirect("another.html", "index.html")

@pytest.mark.sphinx("html", testroot="pass_url_fragments_queries")
def test_pass_url_fragments(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
app.build()

ensure_redirect("another.html", "index.html")
_sb.open(rel2url(app.outdir, "another.html") + "#haha")
# check url
assert Path(rel2url(app.outdir, "index.html")) == Path(
_sb.execute_script(
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
)
)
# check hash
assert "#haha" == _sb.execute_script("return window.location.hash")

@pytest.mark.sphinx("html", testroot="pass_url_fragments_queries")
def test_pass_url_queries(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
app.build()

ensure_redirect("another.html", "index.html")
_sb.open(rel2url(app.outdir, "another.html") + "?phrase=haha")
# check url
assert Path(rel2url(app.outdir, "index.html")) == Path(
_sb.execute_script(
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
)
)
# check query
assert "?phrase=haha" == _sb.execute_script("return window.location.search")

@pytest.mark.sphinx("html", testroot="pass_url_fragments_queries")
def test_pass_url_fragment_and_query(
self, app: Sphinx, _sb: BaseCase, ensure_redirect
):
app.build()

ensure_redirect("another.html", "index.html")
_sb.open(rel2url(app.outdir, "another.html") + "?phrase=haha#giraffe")
# check url
assert Path(rel2url(app.outdir, "index.html")) == Path(
_sb.execute_script(
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
)
)
# check query
assert "?phrase=haha" == _sb.execute_script("return window.location.search")
# check hash
assert "#giraffe" == _sb.execute_script("return window.location.hash")


class TestExtDirHtml:
@pytest.mark.sphinx("dirhtml", testroot="no_redirects")
Expand Down Expand Up @@ -379,3 +428,52 @@ def test_jinja_bad_path(self, app: Sphinx, ensure_redirect):
assert app.statuscode == 0

ensure_redirect("another/index.html", "index.html")

@pytest.mark.sphinx("dirhtml", testroot="pass_url_fragments_queries")
def test_pass_url_fragments(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
app.build()

ensure_redirect("another/index.html", "index.html")
_sb.open(rel2url(app.outdir, "another/index.html") + "#haha")
# check url
assert Path(rel2url(app.outdir, "index.html")) == Path(
_sb.execute_script(
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
)
)
# check hash
assert "#haha" == _sb.execute_script("return window.location.hash")

@pytest.mark.sphinx("dirhtml", testroot="pass_url_fragments_queries")
def test_pass_url_queries(self, app: Sphinx, _sb: BaseCase, ensure_redirect):
app.build()

ensure_redirect("another/index.html", "index.html")
_sb.open(rel2url(app.outdir, "another/index.html") + "?phrase=haha")
# check url
assert Path(rel2url(app.outdir, "index.html")) == Path(
_sb.execute_script(
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
)
)
# check query
assert "?phrase=haha" == _sb.execute_script("return window.location.search")

@pytest.mark.sphinx("dirhtml", testroot="pass_url_fragments_queries")
def test_pass_url_fragment_and_query(
self, app: Sphinx, _sb: BaseCase, ensure_redirect
):
app.build()

ensure_redirect("another/index.html", "index.html")
_sb.open(rel2url(app.outdir, "another/index.html") + "?phrase=haha#giraffe")
# check url
assert Path(rel2url(app.outdir, "index.html")) == Path(
_sb.execute_script(
'return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname'
)
)
# check query
assert "?phrase=haha" == _sb.execute_script("return window.location.search")
# check hash
assert "#giraffe" == _sb.execute_script("return window.location.hash")