Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URLs like /projects/subproject from 404ing when they don't end with a slash #6888

Merged
merged 8 commits into from
Apr 14, 2020
15 changes: 15 additions & 0 deletions readthedocs/proxito/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ def test_versioned_no_slash(self):
response['location'], '/en/latest/',
)

@mock.patch('readthedocs.proxito.views.serve.get_storage_class')
def test_subproject_no_slash(self, storage_mock):
self.subproject.versions.update(active=True, built=True)
storage_mock()().exists.return_value = False
response = self.client.get(
reverse('proxito_404_handler', kwargs={'proxito_path': '/projects/subproject'}),
HTTP_HOST='project.readthedocs.io',
)
self.assertEqual(
response.status_code, 302
)
self.assertEqual(
response['location'], '/projects/subproject/',
)

@mock.patch('readthedocs.proxito.views.serve.get_storage_class')
def test_directory_indexes_readme_serving(self, storage_mock):
self.project.versions.update(active=True, built=True)
Expand Down
14 changes: 14 additions & 0 deletions readthedocs/proxito/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
)
class RedirectTests(BaseDocServing):

def test_root_url_no_slash(self):
r = self.client.get('', HTTP_HOST='project.dev.readthedocs.io')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'], 'https://project.dev.readthedocs.io/en/latest/',
)

def test_root_url(self):
r = self.client.get('/', HTTP_HOST='project.dev.readthedocs.io')
self.assertEqual(r.status_code, 302)
Expand All @@ -26,6 +33,13 @@ def test_subproject_root_url(self):
r['Location'], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/',
)

def test_subproject_root_url_no_slash(self):
r = self.client.get('/projects/subproject', HTTP_HOST='project.dev.readthedocs.io')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/',
)

def test_root_redirect_with_query_params(self):
r = self.client.get('/?foo=bar', HTTP_HOST='project.dev.readthedocs.io')
self.assertEqual(r.status_code, 302)
Expand Down
4 changes: 3 additions & 1 deletion readthedocs/proxito/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@
# (Sub)project single version
url(
(
r'^(?:projects/(?P<subproject_slug>{project_slug})/)?'
# the /? at the end of this regex is for ``/projects/subproject``
# so that it will get captured here and redirect properly.
r'^(?:projects/(?P<subproject_slug>{project_slug})/?)?'
r'(?P<filename>{filename_slug})$'.format(**pattern_opts)
),
ServeDocs.as_view(),
Expand Down