0
0
Flaskframework~20 mins

WSGI middleware concept in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WSGI Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
AThe client receives 'HELLO WORLD' with an extra header 'X-Custom-Header: Middleware'.
BThe client receives 'hello world' with no extra headers.
CThe client receives 'HELLO WORLD' but no extra headers.
DThe client receives 'hello world' with an extra header 'X-Custom-Header: Middleware'.
Attempts:
2 left
💡 Hint
Think about how the middleware modifies both headers and response body.
lifecycle
intermediate
1: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?
AOnly once when the Flask app starts.
BEach time the Flask app receives an HTTP request.
COnly when the Flask app sends a response.
DOnly when the Flask app raises an error.
Attempts:
2 left
💡 Hint
Think about what WSGI middleware wraps and how requests flow.
🔧 Debug
advanced
2: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()
ABecause start_response is called twice causing a TypeError.
BBecause the middleware forgot to call start_response.
CBecause response is an iterable, not a string, so .upper() is invalid.
DBecause the app does not return any response causing NoneType error.
Attempts:
2 left
💡 Hint
Check the type of the response returned by the app.
📝 Syntax
advanced
2: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.
A
def __call__(self, environ, start_response):
    def custom_start(status, headers):
        headers.append(('X-Test', 'Yes'))
        return start_response(status, headers)
    return self.app(environ, custom_start)
B
def __call__(self, environ, start_response):
    headers = [('X-Test', 'Yes')]
    start_response('200 OK', headers)
    return self.app(environ, start_response)
C
def __call__(self, environ, start_response):
    start_response('200 OK', [('X-Test', 'Yes')])
    return self.app(environ, start_response)
D
def __call__(self, environ, start_response):
    def custom_start(status, headers, exc_info=None):
        headers.append(('X-Test', 'Yes'))
        return start_response(status, headers, exc_info)
    return self.app(environ, custom_start)
Attempts:
2 left
💡 Hint
Remember the start_response signature and how to wrap it.
🧠 Conceptual
expert
3: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?
ATo add reusable functionality like logging or headers without changing the app code.
BTo speed up the Flask app by bypassing Flask's routing system.
CTo convert Flask apps into asynchronous apps automatically.
DTo replace Flask's request handling with a different framework.
Attempts:
2 left
💡 Hint
Think about separation of concerns and reusability.