Exercise 3: A Real Response.html() Classmethod — Possible Solution
====================================================================
THE CLASSMETHOD
------------------------------
@classmethod
def html(cls, markup, status=200):
return cls(body=markup, status=status, content_type='text/html')
# registered like any other handler:
router.add_route('GET', '/', lambda req: Response.html('
Hello
'), name='home')
VERIFIED AGAINST A REAL LIVE REQUEST
------------------------------
status: 200
Content-type: text/html
body: b'Hello
'
Content-type reads back as exactly 'text/html', not the class's own
default 'text/plain' - confirmed from the real response object a live
client received, not just from inspecting the Response instance
locally.
WHY THIS WORKS AS AN ANSWER
------------------------------
It follows the exact same shape as Response.json() from the chapter
itself - a classmethod that builds and returns a new Response with one
field (content_type) pinned to a specific value - reusing to_wsgi()'s
own existing Content-Length computation and header-list construction
completely unchanged, and confirms the real header value a client
actually receives rather than only checking the Response object's own
attribute before it's ever sent.