Skip to content

Commit

Permalink
Merge pull request #54 from nathanegillett/3354
Browse files Browse the repository at this point in the history
Return XML error responses for upload API [RHELDST-3354]
  • Loading branch information
negillett committed Oct 15, 2020
2 parents f6a8495 + eb8ca72 commit 10b5d5b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
18 changes: 18 additions & 0 deletions exodus_gw/gateway.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
from fastapi import Depends
from fastapi.exception_handlers import http_exception_handler
from starlette.exceptions import HTTPException as StarletteHTTPException

from .app import app
from .publish import create_publish_id
from .auth import call_context, CallContext
from .s3.util import xml_response


@app.exception_handler(StarletteHTTPException)
async def custom_http_exception_handler(request, exc):
# Override HTTPException to produce XML error responses for the
# given endpoints.

path = request.scope.get("path")

if path.startswith("/upload"):
return xml_response(
"Error", Code=exc.status_code, Message=exc.detail, Endpoint=path
)

return await http_exception_handler(request, exc)


@app.get("/healthcheck", tags=["service"])
Expand Down
8 changes: 7 additions & 1 deletion exodus_gw/s3/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ def xml_response(operation: str, **kwargs) -> Response:
"""
root = Element(operation)

status_code = kwargs.get("Code", 200)

for (key, value) in kwargs.items():
child = SubElement(root, key)
child.text = str(value)

xml = io.BytesIO()
ElementTree(root).write(xml, encoding="UTF-8", xml_declaration=True)
return Response(content=xml.getvalue(), media_type="application/xml")
return Response(
content=xml.getvalue(),
status_code=status_code,
media_type="application/xml",
)


class RequestReader:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_exception_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from fastapi import HTTPException

import pytest
import mock

from exodus_gw.gateway import custom_http_exception_handler

TEST_KEY = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"


@pytest.mark.asyncio
@pytest.mark.parametrize(
"endpoint, media_type",
[
("/upload/foo/%s" % TEST_KEY, "xml"),
("/foo/publish/%s" % TEST_KEY, "json"),
("/healthcheck", "json"),
("/whoami", "json"),
],
ids=["upload", "publish", "healthcheck", "whoami"],
)
async def test_custom_http_exception_handler(endpoint, media_type):
# Verify that HTTPExceptions raised are handled according to endpoint.
# Expand list of endpoints as needed.

request = mock.Mock(scope={"path": endpoint})
err = HTTPException(status_code=600, detail="testing response")
response = await custom_http_exception_handler(request, err)

assert response.media_type == "application/%s" % media_type

0 comments on commit 10b5d5b

Please sign in to comment.