Skip to content

Commit

Permalink
fix: test and response logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Nov 27, 2024
1 parent 26099bc commit 1ea20bd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/auth-react/django3x/mysite/middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from typing import Callable

from django.http import HttpRequest, HttpResponse

Expand All @@ -23,3 +24,17 @@ def __middleware(request: HttpRequest): # type: ignore
return response # type: ignore

return __middleware


def response_logging_middleware(get_response: Callable): # type: ignore
def middleware(request: HttpRequest) -> HttpResponse:
response = get_response(request) # type: ignore

# Log the response
print(f"Path: {request.path} | Method: {request.method} | Status: {response.status_code}") # type: ignore
if hasattr(response, "content"): # type: ignore
print(f"Response: {response.content.decode('utf-8')}") # type: ignore

return response # type: ignore

return middleware
1 change: 1 addition & 0 deletions tests/auth-react/django3x/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"supertokens_python.framework.django.django_middleware.middleware",
"mysite.middleware.response_logging_middleware",
]

ROOT_URLCONF = "mysite.urls"
Expand Down
8 changes: 8 additions & 0 deletions tests/auth-react/django3x/mysite/store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import Any, Dict, List, Optional, Union
from typing_extensions import Literal

Expand All @@ -10,6 +11,13 @@ def save_url_with_token(url_with_token: str):


def get_url_with_token() -> str:
t = 0
while not latest_url_with_token:
time.sleep(0.5)
t += 1
if t > 10:
break

return latest_url_with_token


Expand Down
20 changes: 20 additions & 0 deletions tests/auth-react/fastapi-server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import time
import typing
from typing import Any, Awaitable, Callable, Dict, List, Optional, Union

Expand Down Expand Up @@ -172,6 +173,17 @@
app.add_middleware(get_middleware())
os.environ.setdefault("SUPERTOKENS_ENV", "testing")


@app.middleware("http")
async def log_response(request: Request, call_next): # type: ignore
response = await call_next(request) # type: ignore
if isinstance(response, Response):
body = response.body
if isinstance(body, bytes):
print(f"Response: {body.decode('utf-8')}")
return response # type: ignore


code_store: Dict[str, List[Dict[str, Any]]] = {}
accountlinking_config: Dict[str, Any] = {}
enabled_providers: Optional[List[Any]] = None
Expand Down Expand Up @@ -1397,6 +1409,14 @@ async def get_session_info(session_: SessionContainer = Depends(verify_session()
@app.get("/token")
async def get_token():
global latest_url_with_token
t = 0

while not latest_url_with_token:
time.sleep(0.5)
t += 1
if t > 10:
break

return JSONResponse({"latestURLWithToken": latest_url_with_token})


Expand Down
15 changes: 15 additions & 0 deletions tests/auth-react/flask-server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import time
import traceback
from typing import Any, Awaitable, Callable, Dict, List, Optional, Union

Expand Down Expand Up @@ -1094,6 +1095,12 @@ def make_default_options_response():
)


@app.after_request
def after_request(response): # type: ignore
print(f"Response: {response.get_data(as_text=True)}") # type: ignore
return response # type: ignore


@app.route("/ping", methods=["GET"]) # type: ignore
def ping():
return "success"
Expand Down Expand Up @@ -1284,6 +1291,14 @@ def get_session_info():
@app.route("/token", methods=["GET"]) # type: ignore
def get_token():
global latest_url_with_token

t = 0
while not latest_url_with_token:
time.sleep(0.5)
t += 1
if t > 10:
break

return jsonify({"latestURLWithToken": latest_url_with_token})


Expand Down

0 comments on commit 1ea20bd

Please sign in to comment.