Example #1
0
 def test_get_valid_methods(self):
     self.assertEqual(webapp2.get_valid_methods(BrokenHandler).sort(), ["GET"].sort())
     self.assertEqual(webapp2.get_valid_methods(HomeHandler).sort(), ["GET", "POST"].sort())
     self.assertEqual(
         webapp2.get_valid_methods(MethodsHandler).sort(),
         ["GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE"].sort(),
     )
Example #2
0
 def test_get_valid_methods(self):
     self.assertEqual(webapp2.get_valid_methods(BrokenHandler).sort(),
         ['GET'].sort())
     self.assertEqual(webapp2.get_valid_methods(HomeHandler).sort(),
         ['GET', 'POST'].sort())
     self.assertEqual(webapp2.get_valid_methods(MethodsHandler).sort(),
         ['GET', 'POST', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE'].sort())
Example #3
0
    def test_extra_request_methods(self):
        allowed_methods_backup = webapp2.ALLOWED_METHODS
        webdav_methods = ('VERSION-CONTROL', 'UNLOCK', 'PROPFIND')

        for method in webdav_methods:
            # It is still not possible to use WebDav methods...
            req = webapp2.Request.blank('/webdav')
            req.method = method
            res = req.get_response(app)
            self.assertEqual(res.status, '501 Not Implemented')

        # Let's extend ALLOWED_METHODS with some WebDav methods.
        webapp2.ALLOWED_METHODS = tuple(webapp2.ALLOWED_METHODS) + webdav_methods

        self.assertEqual(sorted(webapp2.get_valid_methods(WebDavHandler)), sorted(list(webdav_methods)))

        # Now we can use WebDav methods...
        for method in webdav_methods:
            req = webapp2.Request.blank('/webdav')
            req.method = method
            res = req.get_response(app)
            self.assertEqual(res.status, '200 OK')
            self.assertEqual(res.body, 'Method: %s' % method)

        # Restore initial values.
        webapp2.ALLOWED_METHODS = allowed_methods_backup
        self.assertEqual(len(webapp2.ALLOWED_METHODS), 7)
    def __call__(self, _method, *args, **kwargs):
        """Dispatches the requested method.

        :param _method:
            The method to be dispatched: the request method in lower case
            (e.g., 'get', 'post', 'head', 'put' etc).
        :param args:
            Positional arguments to be passed to the method, coming from the
            matched :class:`Route`.
        :param kwargs:
            Keyword arguments to be passed to the method, coming from the
            matched :class:`Route`.
        :returns:
            None.
        """
        method = getattr(self, _method, None)
        if method is None:
            # 405 Method Not Allowed.
            # The response MUST include an Allow header containing a
            # list of valid methods for the requested resource.
            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6
            valid = ', '.join(webapp2.get_valid_methods(self))
            self.abort(405, headers=[('Allow', valid)])

        if not self.plugins:
            # Simply execute the requested method.
            return method(*args, **kwargs)

        # Execute before_dispatch plugins.
        for plugin in self.plugins:
            hook = getattr(plugin, 'before_dispatch', None)
            if hook and hook(self) is True:
                break
        else:
            # Execute the requested method.
            method(*args, **kwargs)

        # Execute after_dispatch plugins.
        for plugin in reversed(self.plugins):
            hook = getattr(plugin, 'after_dispatch', None)
            if hook and hook(self) is True:
                break