MCPcopy
hub / github.com/encode/starlette / test_use_testclient_in_endpoint

Function test_use_testclient_in_endpoint

tests/test_testclient.py:50–67  ·  view source on GitHub ↗

We should be able to use the test client within applications. This is useful if we need to mock out other services, during tests or in development.

(test_client_factory: TestClientFactory)

Source from the content-addressed store, hash-verified

48
49
50def test_use_testclient_in_endpoint(test_client_factory: TestClientFactory) -> None:
51 """
52 We should be able to use the test client within applications.
53
54 This is useful if we need to mock out other services,
55 during tests or in development.
56 """
57
58 def homepage(request: Request) -> JSONResponse:
59 client = test_client_factory(mock_service)
60 response = client.get("/")
61 return JSONResponse(response.json())
62
63 app = Starlette(routes=[Route("/", endpoint=homepage)])
64
65 client = test_client_factory(app)
66 response = client.get("/")
67 assert response.json() == {"mock": "example"}
68
69
70def test_testclient_headers_behavior() -> None:

Callers

nothing calls this directly

Calls 5

StarletteClass · 0.90
RouteClass · 0.90
test_client_factoryFunction · 0.85
jsonMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected