Skip to content

Commit

Permalink
Merge pull request #42 from cloudblue/test_client_base_url_default_ex…
Browse files Browse the repository at this point in the history
…c_handler

change base url for test client, add client error exception handler
  • Loading branch information
ffaraone authored Oct 21, 2022
2 parents 46ea277 + a27228b commit bc8dd1b
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 175 deletions.
4 changes: 2 additions & 2 deletions connect/eaas/core/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_client_factory():
given a webapp class.
"""

def _get_client(webapp):
return WebAppTestClient(webapp)
def _get_client(webapp, base_url='https://example.org/public/v1'):
return WebAppTestClient(webapp, base_url=base_url)

return _get_client
13 changes: 9 additions & 4 deletions connect/eaas/core/testing/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
from starlette.routing import Match
from starlette.testclient import TestClient

from connect.client import ClientError
from connect.client.testing import AsyncConnectClientMocker, ConnectClientMocker
from connect.eaas.core.inject.models import Context
from connect.eaas.core.utils import client_error_exception_handler


class WebAppTestClient(TestClient):

def __init__(self, webapp):
def __init__(self, webapp, base_url='https://example.org/public/v1'):
self._webapp_class = webapp
self._app = self._get_application()

super().__init__(app=self._app, base_url='https://localhost/public/v1')
super().__init__(app=self._app, base_url=base_url)

self.headers = {
'X-Connect-Api-Gateway-Url': self.base_url,
Expand Down Expand Up @@ -139,8 +141,11 @@ def _populate_internal_headers(
return headers

def _get_application(self):
app = FastAPI()

app = FastAPI(
exception_handlers={
ClientError: client_error_exception_handler,
},
)
auth_router, no_auth_router = self._webapp_class.get_routers()
app.include_router(auth_router, prefix='/api')
app.include_router(no_auth_router, prefix='/guest')
Expand Down
20 changes: 20 additions & 0 deletions connect/eaas/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from fastapi import Request
from fastapi.responses import JSONResponse, PlainTextResponse

from connect.client import ClientError


def obfuscate_header(key, value):
if key in ('authorization', 'authentication'):
if value.startswith('ApiKey '):
Expand All @@ -9,3 +15,17 @@ def obfuscate_header(key, value):
end_idx = value.index('"', start_idx)
return f'{value[0:start_idx + 2]}******{value[end_idx - 2:]}'
return value


def client_error_exception_handler(request: Request, exc: ClientError):
status_code = exc.status_code or 500
if not exc.error_code:
return PlainTextResponse(status_code=status_code, content=str(exc))
else:
return JSONResponse(
status_code=status_code,
content={
'error_code': exc.error_code,
'errors': exc.errors,
},
)
Loading

0 comments on commit bc8dd1b

Please sign in to comment.