Exercise 3: 405 With Allow vs. a Real 404 — Possible Solution ==================================================================== THE SETUP ------------------------------ router = Router() router.add_route( 'POST', '/subscribe', lambda **p: 'subscribed', name='subscribe', ) status, handler, params, allowed = router.resolve('GET', '/subscribe') print(status, sorted(allowed) if allowed else allowed) # 405 ['POST'] status, handler, params, allowed = router.resolve('GET', '/totally-unregistered-path') print(status, allowed) # 404 None WHY THE FIRST CASE DESERVES A 405, NOT A 404 ------------------------------ /subscribe is a real, registered path - Router.resolve() finds a route whose regex matches it and adds 'POST' to its own allowed set before ever checking the method. The resource genuinely exists at that path; the client just tried to reach it the wrong way. HTTP's own 405 status exists specifically for this - "the resource exists, try again with a different method" - and the Allow header this chapter's own app() builds from allowed tells the client exactly which method(s) would have worked, without it having to guess. WHY THE SECOND CASE DESERVES A PLAIN 404 ------------------------------ No route's pattern ever matches /totally-unregistered-path under any method at all, so allowed stays an empty set and resolve() returns '404', None instead. There is nothing true to say about which methods "would have worked" here, because nothing about this path is registered in the first place - a 405 with an empty or fabricated Allow header would be actively misleading, claiming a resource exists when it doesn't. WHY THIS WORKS AS AN ANSWER ------------------------------ It verifies both branches of resolve()'s own real behavior on two genuinely different real inputs rather than asserting the distinction abstractly, and the explanation ties each status code back to what allowed actually contains in that case - non-empty means "found the path, wrong method," empty means "never found the path at all."