Middleware — Code Around Every Request — Concept Check
Questions on Middleware — Code Around Every Request. @app.middleware("http") wraps every request: work before call_next, work after — request IDs, timing, access logs. Built-ins cover gzip and host checks; know when a dependency is the better tool.
Questions in this quiz
8 questions on Middleware — Code Around Every Request — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer adds a middleware to log request IDs and response times. However, they notice that the request IDs are not consistent across the system. What might be the issue?
- AThe middleware is not logging the request method and URL path.
- BThe middleware is not compressing the response body.
- CThe middleware is not propagating the X-Request-ID header properly.
- DThe middleware is not using the correct logging level.
2.Which of the following scenarios is best handled by middleware rather than a dependency?
- AImplementing rate limiting for certain endpoints.
- BLogging access details for every request.
- CAdding authentication logic to specific routes.
- DResolving tenant information for user-specific data.
3.A service uses GZipMiddleware to compress responses. Users report slow response times for small payloads. What could be the cause?
- AGZipMiddleware is not compatible with FastAPI.
- BGZipMiddleware is incorrectly configured to compress all payloads.
- CGZipMiddleware compresses small payloads, adding overhead.
- DGZipMiddleware only compresses payloads larger than a threshold.
4.A developer wants to implement user authentication in middleware for a FastAPI app. Why might this be a mistake?
- AMiddleware cannot be overridden in tests.
- BMiddleware cannot log request details.
- CMiddleware cannot access request headers.
- DMiddleware cannot handle HTTP exceptions.
5.What is the primary reason to use middleware for setting request IDs and timing headers?
- AMiddleware can handle complex business logic.
- BMiddleware can access user-specific data.
- CMiddleware runs for every request, providing a consistent approach.
- DMiddleware can be overridden per route.
6.A FastAPI app has multiple middleware layers. Which middleware will process the request first?
- AThe last middleware added to the app.
- BThe first middleware added to the app.
- CThe middleware that handles exceptions.
- DThe middleware with the highest priority setting.
7.Why might a developer choose a dependency over middleware for implementing tenant resolution?
- ADependencies can access endpoint-specific information.
- BDependencies run for every route, ensuring consistency.
- CDependencies handle cross-cutting concerns more efficiently.
- DDependencies cannot be overridden in tests.
8.A developer notices that their FastAPI middleware logs do not include the request method. What might they be missing?
- AThe middleware is not using the correct logging configuration.
- BThe middleware is not setting the X-Request-ID header.
- CThe middleware is not accessing the request method from the request object.
- DThe middleware is not calling the call_next function.