Open omarryhan opened 5 years ago
Why do you need get request back from test_client
? any use case ?
Also if you need to test the request obj itself, you can easily test it inside handler.
One use case can be to test request['session']
.
Sorry, what do you mean by inside handler?
you can either
async def test_func(app):
@app.route("/test_get", methods=['GET'])
async def test_get(request):
# test request session
test_cli = await test_client(app)
resp = await test_cli.get('/test_get')
or make the route handler to return request["session"] for testing.
I think it's not very convenient to have test code inside route handlers. Is there any particular reason you don't make use of Sanic's own test client? https://github.com/huge-success/sanic/blob/master/sanic/testing.py It returns both the request and response object by default.
Cool! I appreciate your contribution to this repo and Sanic. Neat work!
One more question, what do you think of maybe having a kwarg named: return_request
that can be passed to pytest-sanic's http client and return the request object created by Sanic and Sanic's middleware? e.g.
async def test_teapot(test_client):
resp = await test_client.get('/teapot')
req, resp = await test_client.get('/teapot', return_request=True)
Yeah, let me think about this.
Just an idea, here's how I managed to preserve the most recent request:
while setting up my app in my test fixtures, I included the following lines:
def request_preserver(req, resp):
app.prev_req = req
# Appending instead of registiring so that it wouldn't append left
# which makes it preserve the latest version of the request object
app.response_middleware.append(request_preserver)
Now whenever I want to access the previous request, I do the following
def test_foo(app, test_client):
resp = await test_client.post('/', json={'foo': 'bar'})
request = app.prev_req
assert request.json['foo'] == 'bar'
I think it would be nice if there's an easy way to get the request object back from the test client. What do you think?