MCPcopy Create free account
hub / github.com/aio-libs/aiohttp / run_tests

Function run_tests

examples/basic_auth_middleware.py:134–176  ·  view source on GitHub ↗

Run all basic auth middleware tests.

()

Source from the content-addressed store, hash-verified

132
133
134async def run_tests() -> None:
135 """Run all basic auth middleware tests."""
136 # Create middleware instance
137 auth_middleware = BasicAuthMiddleware("user", "pass")
138
139 # Use middleware in session
140 async with ClientSession(middlewares=(auth_middleware,)) as session:
141 # Test 1: Correct credentials endpoint
142 print("=== Test 1: Correct credentials ===")
143 async with session.get("http://localhost:8080/basic-auth/user/pass") as resp:
144 _LOGGER.info("Status: %s", resp.status)
145
146 if resp.status == 200:
147 data = await resp.json()
148 _LOGGER.info("Response: %s", data)
149 print("Authentication successful!")
150 print(f"Authenticated: {data.get('authenticated')}")
151 print(f"User: {data.get('user')}")
152 else:
153 print("Authentication failed!")
154 print(f"Status: {resp.status}")
155 text = await resp.text()
156 print(f"Response: {text}")
157
158 # Test 2: Wrong credentials endpoint
159 print("\n=== Test 2: Wrong credentials endpoint ===")
160 async with session.get("http://localhost:8080/basic-auth/other/secret") as resp:
161 if resp.status == 401:
162 print("Authentication failed as expected (wrong credentials)")
163 text = await resp.text()
164 print(f"Response: {text}")
165 else:
166 print(f"Unexpected status: {resp.status}")
167
168 # Test 3: Protected resource
169 print("\n=== Test 3: Access protected resource ===")
170 async with session.get("http://localhost:8080/protected") as resp:
171 if resp.status == 200:
172 data = await resp.json()
173 print("Successfully accessed protected resource!")
174 print(f"Response: {data}")
175 else:
176 print(f"Failed to access protected resource: {resp.status}")
177
178
179async def main() -> None:

Callers 1

mainFunction · 0.70

Calls 5

ClientSessionClass · 0.90
BasicAuthMiddlewareClass · 0.70
getMethod · 0.45
jsonMethod · 0.45
textMethod · 0.45

Tested by

no test coverage detected