Skip to content

Commit

Permalink
Merge pull request #47 from cicekhayri/forbidden-response
Browse files Browse the repository at this point in the history
Enhance ForbiddenResponse with status code and content_type
  • Loading branch information
cicekhayri authored Jan 7, 2024
2 parents a3bbc0a + 41c6d81 commit 18bd349
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
10 changes: 8 additions & 2 deletions inspira/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,11 @@ def __init__(self, url: str, status_code=HTTPStatus.FOUND, headers=None):


class ForbiddenResponse(HttpResponse):
def __init__(self, content=None, status_code=HTTPStatus.FORBIDDEN, headers=None):
super().__init__(content, status_code, TEXT_PLAIN, headers)
def __init__(
self,
content=None,
content_type=TEXT_PLAIN,
status_code=HTTPStatus.FORBIDDEN,
headers=None,
):
super().__init__(content, status_code, content_type, headers)
32 changes: 32 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
HttpResponseRedirect,
JsonResponse,
TemplateResponse,
ForbiddenResponse,
)


Expand Down Expand Up @@ -313,3 +314,34 @@ async def test_set_cookie():
assert "user=john_doe" in headers[0]
assert "Max-Age=3600" in headers[0]
assert "Path=/" in headers[0]


def test_forbidden_response_default_value():
response = ForbiddenResponse()

assert response.content is None
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.content_type == TEXT_PLAIN


def test_forbidden_response_custom_values():
content = "This is forbidden"
custom_status_code = HTTPStatus.UNAUTHORIZED
custom_headers = {"Custom-Header": "Value"}

response = ForbiddenResponse(
content=content, status_code=custom_status_code, headers=custom_headers
)

assert response.content == content
assert response.status_code == custom_status_code
assert response.content_type == TEXT_PLAIN
assert response.headers == custom_headers


def test_forbidden_response_content_type_json():
content = {"message": "You don't have access"}
response = ForbiddenResponse(content=content, content_type=APPLICATION_JSON)

assert response.content == content
assert response.content_type == APPLICATION_JSON

0 comments on commit 18bd349

Please sign in to comment.