#!/usr/bin/env python3 """ Test script to understand FastAPI routing behavior with different prefix configurations. """ from fastapi import FastAPI, APIRouter, Request from fastapi.responses import JSONResponse # Test 1: Router with prefix="" and full path in route router1 = APIRouter() @router1.post("/api/v1/webhooks/stripe") async def webhook_stripe_v1(request: Request): return {"message": "webhook_stripe_v1"} # Test 2: Router with prefix="" and relative path in route router2 = APIRouter() @router2.post("/webhooks/stripe") async def webhook_stripe_v2(request: Request): return {"message": "webhook_stripe_v2"} # Test 3: Router with prefix="/api/v1" and relative path in route router3 = APIRouter() @router3.post("/webhooks/stripe") async def webhook_stripe_v3(request: Request): return {"message": "webhook_stripe_v3"} # Create test apps app1 = FastAPI() app1.include_router(router1, prefix="") app2 = FastAPI() app2.include_router(router2, prefix="") app3 = FastAPI() app3.include_router(router3, prefix="/api/v1") print("Testing routing behavior...") print() # Test each configuration from fastapi.testclient import TestClient client1 = TestClient(app1) response1 = client1.post("/api/v1/webhooks/stripe") print(f"Test 1 (prefix='', full path): {response1.status_code} - {response1.json() if response1.status_code == 200 else 'Not found'}") client2 = TestClient(app2) response2 = client2.post("/api/v1/webhooks/stripe") print(f"Test 2 (prefix='', relative path): {response2.status_code} - {response2.json() if response2.status_code == 200 else 'Not found'}") client3 = TestClient(app3) response3 = client3.post("/api/v1/webhooks/stripe") print(f"Test 3 (prefix='/api/v1', relative path): {response3.status_code} - {response3.json() if response3.status_code == 200 else 'Not found'}") print() print("Analysis:") print("- Test 1 should work: prefix='' + full path '/api/v1/webhooks/stripe' = '/api/v1/webhooks/stripe'") print("- Test 2 should NOT work: prefix='' + relative path '/webhooks/stripe' = '/webhooks/stripe' (not '/api/v1/webhooks/stripe')") print("- Test 3 should work: prefix='/api/v1' + relative path '/webhooks/stripe' = '/api/v1/webhooks/stripe'")