示例#1
0
文件: test_http.py 项目: ish/restish
 def test_method_not_allowed(self):
     r = http.method_not_allowed('GET, POST')
     assert r.status.startswith('405')
     assert r.headers['Content-Type'] == 'text/plain'
     assert r.headers['Allow'] == 'GET, POST'
     assert '405 Method Not Allowed' in r.body
     r = http.method_not_allowed(['GET', 'POST'])
     assert r.headers['Allow'] == 'GET, POST'
     exc = http.MethodNotAllowedError(['GET', 'POST'])
     r = exc.make_response()
     assert r.status.startswith('405')
示例#2
0
 def test_method_not_allowed(self):
     r = http.method_not_allowed("GET, POST")
     assert r.status.startswith("405")
     assert r.headers["Content-Type"] == "text/plain"
     assert r.headers["Allow"] == "GET, POST"
     assert "405 Method Not Allowed" in r.body
     r = http.method_not_allowed(["GET", "POST"])
     assert r.headers["Allow"] == "GET, POST"
     exc = http.MethodNotAllowedError(["GET", "POST"])
     r = exc.make_response()
     assert r.status.startswith("405")
示例#3
0
 def test_method_not_allowed(self):
     r = http.method_not_allowed('GET, POST')
     assert r.status.startswith('405')
     assert r.headers['Content-Type'] == 'text/plain'
     assert r.headers['Allow'] == 'GET, POST'
     assert '405 Method Not Allowed' in r.body
     r = http.method_not_allowed(['GET', 'POST'])
     assert r.headers['Allow'] == 'GET, POST'
     exc = http.MethodNotAllowedError(['GET', 'POST'])
     r = exc.make_response()
     assert r.status.startswith('405')
示例#4
0
文件: resource.py 项目: psr/restish
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No dispatchers for method, send 405 with list of allowed methods.
     if dispatchers is None:
         return http.method_not_allowed(", ".join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         response = callable(self, request)
         # Try to autocomplete the content-type header if not set
         # explicitly.
         # If there's no accept from the client and there's only one
         # possible type from the match then use that as the best match.
         # Otherwise use mimeparse to work out what the best match was. If
         # the best match if not a wildcard then we know what content-type
         # should be.
         if isinstance(response, http.Response) and not response.headers.get("content-type"):
             accept = str(request.accept)
             if not accept and len(match["accept"]) == 1:
                 best_match = match["accept"][0]
             else:
                 best_match = mimeparse.best_match(match["accept"], accept)
             if "*" not in best_match:
                 response.headers["content-type"] = best_match
         return response
     # No match, send 406
     return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
示例#5
0
文件: resource.py 项目: ish/restish
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No dispatchers for method, send 405 with list of allowed methods.
     if dispatchers is None:
         return http.method_not_allowed(", ".join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher, reason = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         return _dispatch(request, match, lambda r: callable(self, r))
     # No match
     return _best_dispatcher_error_response(reason)
示例#6
0
文件: resource.py 项目: ish/restish
 def __call__(self, request):
     # Extract annotations.
     method = getattr(self, _RESTISH_METHOD)
     match = getattr(self, _RESTISH_MATCH)
     # Check for correct method.
     if request.method != method:
         return http.method_not_allowed([method])
     # Look for a dispatcher.
     dispatcher, reason = _best_dispatcher([(self.func, match)], request)
     if dispatcher is not None:
         return _dispatch(request, match, self.func)
     # No dispatcher.
     return _best_dispatcher_error_response(reason)
示例#7
0
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No dispatchers for method, send 405 with list of allowed methods.
     if dispatchers is None:
         return http.method_not_allowed(", ".join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         return _dispatch(request, match, lambda r: callable(self, r))
     # No match, send 406
     return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
示例#8
0
 def __call__(self, request):
     # Extract annotations.
     method = getattr(self, _RESTISH_METHOD)
     match = getattr(self, _RESTISH_MATCH)
     # Check for correct method.
     if request.method != method:
         return http.method_not_allowed([method])
     # Look for a dispatcher.
     dispatcher = _best_dispatcher([(self.func, match)], request)
     if dispatcher is not None:
         return _dispatch(request, match, self.func)
     # No dispatcher.
     return http.not_acceptable([("Content-Type", "text/plain")], "406 Not Acceptable")
示例#9
0
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No dispatchers for method, send 405 with list of allowed methods.
     if dispatchers is None:
         return http.method_not_allowed(', '.join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher, reason = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         return _dispatch(request, match, lambda r: callable(self, r))
     # No match
     return _best_dispatcher_error_response(reason)
示例#10
0
 def __call__(self, request):
     # Extract annotations.
     method = getattr(self, _RESTISH_METHOD)
     match = getattr(self, _RESTISH_MATCH)
     # Check for correct method.
     if request.method != method:
         return http.method_not_allowed([method])
     # Look for a dispatcher.
     dispatcher, reason = _best_dispatcher([(self.func, match)], request)
     if dispatcher is not None:
         return _dispatch(request, match, self.func)
     # No dispatcher.
     return _best_dispatcher_error_response(reason)
示例#11
0
文件: resource.py 项目: greut/restish
 def __call__(self, request):
     # Get the dispatchers for the request method.
     dispatchers = self.request_dispatchers.get(request.method)
     # No normal dispatchers for method found,
     if dispatchers is None:
         # Looking for a magic dispatcher
         dispatchers = self.request_dispatchers.get(ALL.method)
         # No magic dispatchers found either,
         # send 405 with list of allowed methods.
         if dispatchers is None:
             return http.method_not_allowed(', '.join(self.request_dispatchers))
     # Look up the best dispatcher
     dispatcher = _best_dispatcher(dispatchers, request)
     if dispatcher is not None:
         (callable, match) = dispatcher
         return _dispatch(request, match, lambda r: callable(self, r))
     # No match, send 406
     return http.not_acceptable([('Content-Type', 'text/plain')], \
                                '406 Not Acceptable')