diff --git a/sphinxext/rediraffe.py b/sphinxext/rediraffe.py index 509aed2..712bf24 100644 --- a/sphinxext/rediraffe.py +++ b/sphinxext/rediraffe.py @@ -21,11 +21,16 @@ """
- +You should have been redirected.
If not, click here to continue. + diff --git a/tests/roots/ext/test-pass_url_fragments_queries/conf.py b/tests/roots/ext/test-pass_url_fragments_queries/conf.py new file mode 100644 index 0000000..8136c0e --- /dev/null +++ b/tests/roots/ext/test-pass_url_fragments_queries/conf.py @@ -0,0 +1,8 @@ +extensions = ["sphinxext.rediraffe"] + +master_doc = "index" +exclude_patterns = ["_build"] + +html_theme = "basic" + +rediraffe_redirects = "redirects.txt" diff --git a/tests/roots/ext/test-pass_url_fragments_queries/index.rst b/tests/roots/ext/test-pass_url_fragments_queries/index.rst new file mode 100644 index 0000000..e5616f3 --- /dev/null +++ b/tests/roots/ext/test-pass_url_fragments_queries/index.rst @@ -0,0 +1 @@ +Index File \ No newline at end of file diff --git a/tests/roots/ext/test-pass_url_fragments_queries/redirects.txt b/tests/roots/ext/test-pass_url_fragments_queries/redirects.txt new file mode 100644 index 0000000..f25711e --- /dev/null +++ b/tests/roots/ext/test-pass_url_fragments_queries/redirects.txt @@ -0,0 +1 @@ +another.rst index.rst \ No newline at end of file diff --git a/tests/test_ext.py b/tests/test_ext.py index 23512f9..964d176 100644 --- a/tests/test_ext.py +++ b/tests/test_ext.py @@ -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") @@ -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")