Skip to content

Commit

Permalink
Merge pull request #1634 from nationalarchives/404-on-trailing-slash
Browse files Browse the repository at this point in the history
Trailing slash on document 404s rather than 500
  • Loading branch information
dragon-dxw authored Nov 20, 2024
2 parents f79aa32 + 189e413 commit 3ba9337
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
9 changes: 9 additions & 0 deletions judgments/resolvers/document_resolver_engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from typing import Optional

from caselawclient.models.documents import DocumentURIString
from caselawclient.models.documents.exceptions import InvalidDocumentURIException
from django.http import Http404
from django.http.request import HttpRequest
from django.views.generic import View

Expand All @@ -24,6 +27,12 @@ def dispatch(
component_lookup = {
"press-summary": press_summaries,
}

try:
document_uri = DocumentURIString(document_uri)
except InvalidDocumentURIException:
raise Http404("Document Resolver recieved an invalid DocumentURIString")

if file_format:
return fileformat_lookup[file_format](request, document_uri)

Expand Down
14 changes: 1 addition & 13 deletions judgments/tests/test_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from caselawclient.errors import DocumentNotFoundError
from caselawclient.factories import DocumentBodyFactory, JudgmentFactory, PressSummaryFactory
from caselawclient.models.documents import DocumentURIString
from django.http import Http404, HttpResponseRedirect
from django.http import Http404
from django.template.defaultfilters import filesizeformat
from django.test import Client, TestCase

Expand Down Expand Up @@ -164,18 +164,6 @@ def test_download_options(
assert_contains_html(response, download_options_html)


class TestDocumentURIRedirects(TestCase):
@patch("judgments.views.detail.detail_html.get_published_document_by_uri")
def test_non_canonical_uri_redirects(self, mock_get_document_by_uri):
mock_get_document_by_uri.return_value = JudgmentFactory.build(
uri=DocumentURIString("test/1234/567"), is_published=True
)
response = self.client.get("/test/1234/567/")
assert isinstance(response, HttpResponseRedirect)
assert response.status_code == 302
assert response.url == "/test/1234/567"


class TestPressSummaryLabel(TestCase):
@patch("judgments.views.detail.detail_html.DocumentPdf", autospec=True)
@patch("judgments.views.detail.detail_html.get_published_document_by_uri")
Expand Down
7 changes: 7 additions & 0 deletions judgments/tests/test_url_resolution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.test import TestCase


class TestURLResolution(TestCase):
def test_trailing_slash(self):
response = self.client.get("/test/2023/123/")
assert response.status_code == 404

0 comments on commit 3ba9337

Please sign in to comment.