From a403ba1216fc0950d26e8c6fdf98d78038a9f95d Mon Sep 17 00:00:00 2001 From: Jamie Painter Date: Wed, 6 Feb 2013 08:48:14 -0500 Subject: [PATCH 1/3] fix($routing): Fix endpoint routing w/ params When performing an HTTP request with the incorrect method (verb) to an endpoint the expected result should be an HTTP 405. However the method_not_allowed() method that is dynamically assigned to each Resource object does not have a variable-length argument parameter to handle these objects. This pull request simply adds (but ignores) the parameter and provides a corresponding unit test. --- falcon/responders.py | 2 +- tests/test_http_method_routing.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/falcon/responders.py b/falcon/responders.py index ebc7571..d1deea4 100644 --- a/falcon/responders.py +++ b/falcon/responders.py @@ -43,7 +43,7 @@ def create_method_not_allowed(allowed_methods): """ - def method_not_allowed(req, resp): + def method_not_allowed(req, resp, **params): resp.status = HTTP_405 resp.set_header('Allow', ', '.join(allowed_methods)) diff --git a/tests/test_http_method_routing.py b/tests/test_http_method_routing.py index 24f04b4..61fd967 100644 --- a/tests/test_http_method_routing.py +++ b/tests/test_http_method_routing.py @@ -61,6 +61,15 @@ class ResourceMisc(object): pass +class ResourceGetWithParam(object): + def __init__(self): + self.called = False + + @capture + def on_get(self, req, resp): + resp.status = falcon.HTTP_204 + + class TestHttpMethodRouting(helpers.TestSuite): def prepare(self): @@ -70,6 +79,10 @@ class TestHttpMethodRouting(helpers.TestSuite): self.resource_misc = ResourceMisc() self.api.add_route('/misc', self.resource_misc) + self.resource_get_with_param = ResourceGetWithParam() + self.api.add_route( + '/get_with_param/{param}', self.resource_get_with_param) + def test_get(self): self._simulate_request('/get') self.assertTrue(self.resource_get.called) @@ -97,6 +110,23 @@ class TestHttpMethodRouting(helpers.TestSuite): self.assertThat(headers, Contains(allow_header)) + def test_method_not_allowed_with_param(self): + for method in HTTP_METHODS: + if method == 'GET': + continue + + self.resource_get.called = False + self._simulate_request( + '/get_with_param/bogus_param', method=method) + + self.assertFalse(self.resource_get.called) + self.assertEquals(self.srmock.status, '405 Method Not Allowed') + + headers = self.srmock.headers + allow_header = ('Allow', 'GET') + + self.assertThat(headers, Contains(allow_header)) + def test_bogus_method(self): self._simulate_request('/get', method=self.getUniqueString()) self.assertFalse(self.resource_get.called) From fcf8121eef83c9a1654fef0926e9c19f3bda3e91 Mon Sep 17 00:00:00 2001 From: Jamie Painter Date: Wed, 6 Feb 2013 16:46:04 -0500 Subject: [PATCH 2/3] fix($style): Fixes style --- tests/test_http_method_routing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_http_method_routing.py b/tests/test_http_method_routing.py index 61fd967..0629f76 100644 --- a/tests/test_http_method_routing.py +++ b/tests/test_http_method_routing.py @@ -80,8 +80,8 @@ class TestHttpMethodRouting(helpers.TestSuite): self.api.add_route('/misc', self.resource_misc) self.resource_get_with_param = ResourceGetWithParam() - self.api.add_route( - '/get_with_param/{param}', self.resource_get_with_param) + self.api.add_route('/get_with_param/{param}', + self.resource_get_with_param) def test_get(self): self._simulate_request('/get') From cd47af48813e7c9f5642133a63e593432fa9f0bd Mon Sep 17 00:00:00 2001 From: Jamie Painter Date: Thu, 7 Feb 2013 08:13:44 -0500 Subject: [PATCH 3/3] fix($test): Wrong resource checked in unit test test_method_not_allowed_with_param was asserting that a different resource was not being called, which of course is a correct assumption! This commit fixes that and checks that the correct resource is being called --- tests/test_http_method_routing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_http_method_routing.py b/tests/test_http_method_routing.py index 0629f76..71958e2 100644 --- a/tests/test_http_method_routing.py +++ b/tests/test_http_method_routing.py @@ -115,11 +115,11 @@ class TestHttpMethodRouting(helpers.TestSuite): if method == 'GET': continue - self.resource_get.called = False + self.resource_get_with_param.called = False self._simulate_request( '/get_with_param/bogus_param', method=method) - self.assertFalse(self.resource_get.called) + self.assertFalse(self.resource_get_with_param.called) self.assertEquals(self.srmock.status, '405 Method Not Allowed') headers = self.srmock.headers