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

Usage of ASGI client with CORS #47

Open
ahopkins opened this issue Jul 13, 2020 · 7 comments
Open

Usage of ASGI client with CORS #47

ahopkins opened this issue Jul 13, 2020 · 7 comments

Comments

@ahopkins
Copy link

I think this is a problem more with SPF than this project. However, there is an exception when trying to run the ASGI test client.

_, response = await app.asgi_client.post("/foo/bar", json=data)
Traceback (most recent call last):
  File "/home/adam/.virtualenvs/pf/lib/python3.7/site-packages/sanic/app.py", line 910, in handle_request
    response = await self._run_request_middleware(request)
  File "/home/adam/.virtualenvs/pf/lib/python3.7/site-packages/spf/framework.py", line 533, in _run_request_middleware
    "App must be running before you can run middleware!"
AssertionError: App must be running before you can run middleware!

There is a PR on the Sanic repo that would help this by adding a test_mode flag to the app instance.

@ashleysommer
Copy link
Owner

ashleysommer commented Jul 13, 2020

@ahopkins
I'm not completely up to speed on all of the asgi features in Sanic. Is asgi_client only used when testing, ie is it a type of test_client? Or would there be other cases when a user would use asgi_client to call into their app?

@ahopkins
Copy link
Author

It is a test client only. It reaches in and executes the Middleware, listeners, and handlers without having to startup a server.

@ashleysommer
Copy link
Owner

Ok, I might have to think about this some more.

SPF does some initialization on the app's after_server_start hook (or before_server_start depending on Sanic version and mode of operation). This initialization step transitions SPF from its "setup" state to its "operational" state, ready to have requests passed through it.

Executing a route (or middleware) on the Sanic app without first booting the app causes the request to be pass through SPF while it is still in its "setup" state, and results in unspecified operation, that is why there is an assertion inserted at that point.

The message "App must be running before you can run middleware!" could definitely be tweaked to make it more clear why the error is emitted.

Its not reasonable to expect the asgi_client to boot the app for every request either, that would defeat the purpose.

The test_mode check might help here. SPF might be able to put itself in a kind of 'serverless' state. But until that is merged, I might be able to come up with another way of getting this to work.

@ahopkins
Copy link
Author

Yeah, I'd say let's get test mode merged and released and the sanic test client broken out. They seem like that would help a lot.
As for CORS, it seems like that is a Middleware that could probably be skipped in test mode. Maybe that's an option?

@ashleysommer
Copy link
Owner

ashleysommer commented Jul 13, 2020

Yep, simplest way forward would be simply skip all SPF-derived routes and middleware when in test mode. That wouldn't matter for something like CORS, but for other plugins maybe thats not a good long-term solution.

This PR sanic-org/sanic#1894 is still showing test errors. Is it ok to merge as-is?

@ahopkins
Copy link
Author

This PR huge-success/sanic#1894 is still showing test errors. Is it ok to merge as-is?

Yes, after I figure those tests out.

@ashleysommer
Copy link
Owner

@ahopkins I've pushed a new version of Sanic-Plugins-Framework v0.9.3, it has a simple mechanism to switch SPF into serverless "running" mode if a request is received before the app is started, and the app is in asgi mode.

Can you please upgrade your SPF to 0.9.3 to see if it fixes this problem for you?

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

No branches or pull requests

2 participants