Merge pull request #386 from youknowone/sanic_endpoint_test

Fix sanic_endpoint_test working with redirects
This commit is contained in:
Eli Uriegas
2017-02-07 10:57:05 -06:00
committed by GitHub
3 changed files with 104 additions and 77 deletions

View File

@@ -19,19 +19,20 @@ async def local_request(method, uri, cookies=None, *args, **kwargs):
def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
debug=False, server_kwargs={},
*request_args, **request_kwargs):
results = []
results = [None, None]
exceptions = []
if gather_request:
def _collect_request(request):
results.append(request)
if results[0] is None:
results[0] = request
app.request_middleware.appendleft(_collect_request)
async def _collect_response(sanic, loop):
try:
response = await local_request(method, uri, *request_args,
**request_kwargs)
results.append(response)
results[-1] = response
except Exception as e:
exceptions.append(e)
app.stop()
@@ -52,7 +53,7 @@ def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
results))
else:
try:
return results[0]
return results[-1]
except:
raise ValueError(
"Request object expected, got ({})".format(results))