Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear how to determine allowed methods per URL, so hard to make a 405 #70

Closed
cdent opened this issue Jun 22, 2016 · 5 comments
Closed

Comments

@cdent
Copy link

cdent commented Jun 22, 2016

When a URL exists, but does not support a particular method (e.g. DELETE) the correct response is to return an HTTP 405 response code. Routes doesn't make this immediately easy. By default a mapping that does not match just returns None with no indication that it was because of a failed condition after an initial path match. So what I've had to come up with is something like the following test, which seems cumbersome. Is there a better way?

from routes import Mapper, middleware

def test_405():
    map = Mapper()
    map.connect(
        '/controller/{id}', action='get_handler',
        conditions=dict(method=['GET', 'HEAD']))
    map.connect('/controller/{id}', action='post_handler',
        conditions=dict(method=['POST']))
    map.connect('/controller/{id}', action='405_handler')

    environ = {
        'PATH_INFO': '/controller/cow',
        'REQUEST_METHOD': 'GET',
        'SERVER_NAME': 'example.com',
        'SERVER_PORT': '80',
        'wsgi.url_scheme': 'http',
    }

    result = map.match(environ=environ)
    assert result['action'] == 'get_handler'

    environ['REQUEST_METHOD'] = 'POST'
    result = map.match(environ=environ)
    assert result['action'] == 'post_handler'

    environ['REQUEST_METHOD'] = 'PUT'
    result = map.match(environ=environ)
    assert result['action'] == '405_handler'

    environ['PATH_INFO'] = '/missed/url'
    result = map.match(environ=environ)
    assert result is None
@cdent
Copy link
Author

cdent commented Jun 23, 2016

This leaves out the mechanism whereby the 405_handler would know which methods to put in the Allow header of the response it sends.

@bbangert
Copy link
Owner

bbangert commented Jul 8, 2016

That's correct, it does not. In theory it could though, if it recorded the fact that a route did match, but then the method did not. I'd be open to a PR adding such a capability.

@cdent
Copy link
Author

cdent commented Jul 8, 2016

For reference the way I've ended up doing things is in the make_map() method in this file.

That's suitably explicit for my situation for now, but I'll keep thinking about a more general solution.

@bbangert
Copy link
Owner

bbangert commented Jan 1, 2017

I think having a special match method, or toggle that returns detailed match info would make it possible to handle this case. ie, instead of None, it'd return a Match object with information on what routes matched, and what checks failed. This way you could examine the object and determine if it was just the method that failed to match and issue a 405.

@bbangert
Copy link
Owner

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants