Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the best way to get the request object back? #25

Open
omarryhan opened this issue Dec 25, 2018 · 9 comments
Open

What's the best way to get the request object back? #25

omarryhan opened this issue Dec 25, 2018 · 9 comments
Labels

Comments

@omarryhan
Copy link

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?

@yunstanford
Copy link
Owner

Why do you need get request back from test_client ? any use case ?

@yunstanford
Copy link
Owner

Also if you need to test the request obj itself, you can easily test it inside handler.

@omarryhan
Copy link
Author

One use case can be to test request['session'].
Sorry, what do you mean by inside handler?

@yunstanford
Copy link
Owner

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.

@omarryhan
Copy link
Author

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.

@yunstanford
Copy link
Owner

  1. it won't be able to test async coroutines, which is huge gap for async web application. When your webapp becomes large, it will have a lot lib async functions that needs test asynchronously.
  2. it starts up TestServer and tears it down for each single request. It won't work if anyone wanna test behavior that requires server serving several requests.

@omarryhan
Copy link
Author

omarryhan commented Dec 29, 2018

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)

@yunstanford
Copy link
Owner

Yeah, let me think about this.

@omarryhan
Copy link
Author

omarryhan commented Mar 1, 2019

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants