workaround fix for an issue in aiohttp.Client

This commit is contained in:
Yun Xu 2018-02-27 21:42:41 -08:00
parent d55e453bd5
commit c39ddd00d3
3 changed files with 21 additions and 1 deletions

View File

@ -156,6 +156,8 @@ class MethodNotSupported(SanicException):
super().__init__(message)
self.headers = dict()
self.headers["Allow"] = ", ".join(allowed_methods)
if method in ['HEAD', 'PATCH', 'PUT', 'DELETE']:
self.headers['Content-Length'] = 0
@add_status_code(500)

View File

@ -1,6 +1,8 @@
import traceback
from json import JSONDecodeError
from sanic.log import logger
from sanic.exceptions import MethodNotSupported
from sanic.response import text
HOST = '127.0.0.1'
@ -54,6 +56,13 @@ class SanicTestClient:
results[0] = request
self.app.request_middleware.appendleft(_collect_request)
@self.app.exception(MethodNotSupported)
async def error_handler(request, exception):
if request.method in ['HEAD', 'PATCH', 'PUT', 'DELETE']:
return text('', exception.status_code, headers=exception.headers)
else:
return self.app.error_handler.default(request, exception)
@self.app.listener('after_server_start')
async def _collect_response(sanic, loop):
try:

View File

@ -45,6 +45,10 @@ def test_method_not_allowed():
request, response = app.test_client.head('/')
assert response.headers['Allow'] == 'GET'
request, response = app.test_client.post('/')
assert response.headers['Allow'] == 'GET'
@app.post('/')
async def test(request):
return response.json({'hello': 'world'})
@ -52,7 +56,12 @@ def test_method_not_allowed():
request, response = app.test_client.head('/')
assert response.status == 405
assert set(response.headers['Allow'].split(', ')) == set(['GET', 'POST'])
assert response.headers['Content-Length'] == '40'
assert response.headers['Content-Length'] == '0'
request, response = app.test_client.patch('/')
assert response.status == 405
assert set(response.headers['Allow'].split(', ')) == set(['GET', 'POST'])
assert response.headers['Content-Length'] == '0'
@pytest.fixture