Commits

Armin Ronacher committed 1423002

Added a way to query for allowed methods

  • Participants
  • Parent commits 4f4be24

Comments (0)

Files changed (3)

   server due to problems it caused.
 - Werkzeug no longer overrides the Date header when creating a
   conditional HTTP response.
+- The routing system provides a method to retrieve the matching
+  methods for a given path.
 
 Version 0.6.2
 -------------

File tests/test_routing.py

 
 
 def test_protocol_joining_bug():
+    """Make sure the protocol joining bug is fixed"""
     m = Map([Rule('/<foo>', endpoint='x')])
     a = m.bind('example.org')
     assert a.build('x', {'foo': 'x:y'}) == '/x:y'
     assert a.build('x', {'foo': 'x:y'}, force_external=True) == 'http://example.org/x:y'
+
+
+def test_allowed_methods_querying():
+    """Make sure it's possible to test for allowed methods"""
+    m = Map([Rule('/<foo>', methods=['GET', 'HEAD']),
+             Rule('/foo', methods=['POST'])])
+    a = m.bind('example.org')
+    assert sorted(a.allowed_methods('/foo')) == ['GET', 'HEAD', 'POST']

File werkzeug/routing.py

             self.match(path_info, method)
         except RequestRedirect:
             pass
-        except NotFound:
+        except (NotFound, MethodNotAllowed):
             return False
         return True
 
+    def allowed_methods(self, path_info=None):
+        """Returns the valid methods that match for a given path.
+
+        .. versionadded:: 0.7
+        """
+        try:
+            self.match(path_info, method='--')
+        except MethodNotAllowed, e:
+            return e.valid_methods
+        except HTTPException, e:
+            pass
+        return []
+
     def _partial_build(self, endpoint, values, method, append_unknown):
         """Helper for :meth:`build`.  Returns subdomain and path for the
         rule that accepts this endpoint, values and method.