Skip to content

Commit 4bde8eb

Browse files
authored
Proxito: don't check for index.html if the path already ends with /. (#10153)
1 parent d8201e4 commit 4bde8eb

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

readthedocs/proxito/tests/test_full.py

+41-6
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,42 @@ def test_404_storage_serves_custom_404_sphinx(self, storage_exists, storage_open
796796
self.assertEqual(response.status_code, 404)
797797

798798
@mock.patch.object(BuildMediaFileSystemStorageTest, 'exists')
799-
def test_redirects_to_correct_index(self, storage_exists):
800-
"""This case is when the project uses a README.html as index."""
799+
def test_redirects_to_correct_index_ending_with_slash(self, storage_exists):
800+
"""When the path ends with a slash, we try README.html as index."""
801801
self.project.versions.update(active=True, built=True)
802-
fancy_version = fixture.get(
802+
fixture.get(
803+
Version,
804+
slug="fancy-version",
805+
privacy_level=constants.PUBLIC,
806+
active=True,
807+
built=True,
808+
project=self.project,
809+
documentation_type=SPHINX,
810+
)
811+
812+
storage_exists.side_effect = [True]
813+
response = self.client.get(
814+
reverse(
815+
"proxito_404_handler",
816+
kwargs={"proxito_path": "/en/fancy-version/not-found/"},
817+
),
818+
HTTP_HOST="project.readthedocs.io",
819+
)
820+
storage_exists.assert_has_calls(
821+
[
822+
mock.call("html/project/fancy-version/not-found/README.html"),
823+
]
824+
)
825+
self.assertEqual(response.status_code, 302)
826+
self.assertEqual(
827+
response["location"], "/en/fancy-version/not-found/README.html"
828+
)
829+
830+
@mock.patch.object(BuildMediaFileSystemStorageTest, "exists")
831+
def test_redirects_to_correct_index_ending_without_slash(self, storage_exists):
832+
"""When the path doesn't end with a slash, we try both, index.html and README.html."""
833+
self.project.versions.update(active=True, built=True)
834+
fixture.get(
803835
Version,
804836
slug='fancy-version',
805837
privacy_level=constants.PUBLIC,
@@ -811,8 +843,11 @@ def test_redirects_to_correct_index(self, storage_exists):
811843

812844
storage_exists.side_effect = [False, True]
813845
response = self.client.get(
814-
reverse('proxito_404_handler', kwargs={'proxito_path': '/en/fancy-version/not-found/'}),
815-
HTTP_HOST='project.readthedocs.io',
846+
reverse(
847+
"proxito_404_handler",
848+
kwargs={"proxito_path": "/en/fancy-version/not-found"},
849+
),
850+
HTTP_HOST="project.readthedocs.io",
816851
)
817852
storage_exists.assert_has_calls(
818853
[
@@ -1143,7 +1178,7 @@ def test_track_broken_link_custom_404(self, storage_exists, storage_open):
11431178
HTTP_HOST="project.readthedocs.io",
11441179
)
11451180
self.assertEqual(resp.status_code, 404)
1146-
storage_open.assert_called_once_with("html/project/latest/404.html")
1181+
storage_open.assert_called_once()
11471182

11481183
self.assertEqual(PageView.objects.all().count(), 2)
11491184
version = self.project.versions.get(slug="latest")

readthedocs/proxito/views/serve.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,15 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
691691
version_type=self.version_type,
692692
)
693693

694+
tryfiles = ["index.html", "README.html"]
695+
# If the path ends with `/`, we already tried to serve
696+
# the `/index.html` file, so we only need to test for
697+
# the `/README.html` file.
698+
if full_path.endswith("/"):
699+
tryfiles = ["README.html"]
700+
694701
# First, check for dirhtml with slash
695-
for tryfile in ("index.html", "README.html"):
702+
for tryfile in tryfiles:
696703
storage_filename_path = build_media_storage.join(
697704
storage_root_path,
698705
f"{filename}/{tryfile}".lstrip("/"),

0 commit comments

Comments
 (0)