Challenge - 5 Problems
WSGI Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this WSGI middleware do to the response?
Consider this WSGI middleware wrapping a Flask app. What will be the final response body sent to the client?
Flask
class SimpleMiddleware: def __init__(self, app): self.app = app def __call__(self, environ, start_response): def custom_start_response(status, headers, exc_info=None): headers.append(('X-Custom-Header', 'Middleware')) return start_response(status, headers, exc_info) response = self.app(environ, custom_start_response) modified_response = [] for data in response: modified_response.append(data.upper()) return modified_response from flask import Flask app = Flask(__name__) @app.route('/') def index(): return 'hello world' app.wsgi_app = SimpleMiddleware(app.wsgi_app)
Attempts:
2 left
💡 Hint
Think about how the middleware modifies both headers and response body.
✗ Incorrect
The middleware adds a custom header by modifying the start_response call and converts the response body to uppercase by iterating over it and changing each chunk to uppercase.
❓ lifecycle
intermediate1:30remaining
When is the WSGI middleware __call__ method executed?
In a Flask app wrapped by WSGI middleware, at what point is the middleware's __call__ method triggered?
Attempts:
2 left
💡 Hint
Think about what WSGI middleware wraps and how requests flow.
✗ Incorrect
The __call__ method of WSGI middleware is called every time a new HTTP request comes in, because it wraps the app's WSGI callable which handles requests.
🔧 Debug
advanced2:30remaining
Why does this WSGI middleware cause a TypeError?
This middleware tries to modify the response but causes a TypeError. Why?
Flask
class FaultyMiddleware: def __init__(self, app): self.app = app def __call__(self, environ, start_response): response = self.app(environ, start_response) return response.upper()
Attempts:
2 left
💡 Hint
Check the type of the response returned by the app.
✗ Incorrect
The app returns an iterable (like a list or generator) of byte strings, not a single string. Calling .upper() on the iterable causes a TypeError.
📝 Syntax
advanced2:00remaining
Which option correctly adds a header in WSGI middleware?
Select the code snippet that correctly adds a custom header 'X-Test: Yes' in WSGI middleware.
Attempts:
2 left
💡 Hint
Remember the start_response signature and how to wrap it.
✗ Incorrect
Option D correctly defines a wrapper function with the right signature including exc_info, appends the header, and calls the original start_response. Others have wrong signatures or call start_response too early.
🧠 Conceptual
expert3:00remaining
What is the main benefit of using WSGI middleware in Flask apps?
Why would a developer add WSGI middleware around a Flask app instead of modifying the Flask app code directly?
Attempts:
2 left
💡 Hint
Think about separation of concerns and reusability.
✗ Incorrect
WSGI middleware allows adding features like logging, security checks, or header modifications in a reusable way without touching the Flask app code itself.