Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
ROLE = os.getenv("APP_ROLE", "web")
INSTANCE = os.getenv("INSTANCE", ROLE)

def sanitize_header_val(val: str | None) -> str | None:
if not val:
return None
cleaned = val.replace("\r", "").replace("\n", "").strip()
return cleaned if cleaned else None

async def health(request):
req_id = request.headers.get("X-Request-ID")
req_id = sanitize_header_val(request.headers.get("X-Request-ID"))
data = {"status": "ok", "service": INSTANCE}
if req_id:
data["requestId"] = req_id
headers = {"X-Request-ID": req_id} if req_id else {}
return web.json_response(data, headers=headers)

async def index(request):
req_id = request.headers.get("X-Request-ID")
req_id = sanitize_header_val(request.headers.get("X-Request-ID"))
headers = {"X-Request-ID": req_id} if req_id else {}
if ROLE == "web":
return web.Response(
Expand Down
1 change: 1 addition & 0 deletions nginx/snippets/proxy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ proxy_set_header Connection "";
proxy_hide_header X-Powered-By;
proxy_next_upstream error timeout http_502 http_503 http_504;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 10s;
4 changes: 3 additions & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def test_proxy_forwarding_invariants(self):
self.assertIn('proxy_hide_header X-Powered-By;', proxy)
self.assertIn('proxy_next_upstream error timeout http_502 http_503 http_504;', proxy)
self.assertIn('proxy_next_upstream_tries 2;', proxy)
self.assertIn('proxy_next_upstream_timeout 10s;', proxy)

def test_rate_limiting_and_http_status(self):
nginx_cfg = load('nginx/nginx.conf')
Expand Down Expand Up @@ -131,7 +132,8 @@ def test_gzip_compression_and_buffer_tuning(self):

def test_trace_id_propagation_in_demo_app(self):
source = load('apps/demo/app.py')
self.assertIn('request.headers.get("X-Request-ID")', source)
self.assertIn('def sanitize_header_val', source)
self.assertIn('sanitize_header_val(request.headers.get("X-Request-ID"))', source)
self.assertIn('data["requestId"] = req_id', source)
self.assertIn('"X-Request-ID": req_id', source)

Expand Down
Loading