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

SanicTestClient throws error with response.text(...) #1703

Closed
wochinge opened this issue Oct 16, 2019 · 6 comments
Closed

SanicTestClient throws error with response.text(...) #1703

wochinge opened this issue Oct 16, 2019 · 6 comments
Labels

Comments

@wochinge
Copy link

wochinge commented Oct 16, 2019

Describe the bug
When an endpoint does not return a json und we are using the SanicTestClient, it gives an error:

        if exceptions:
>           raise ValueError("Exception during request: {}".format(exceptions))
E           ValueError: Exception during request: [JSONDecodeError('Expecting value: line 1 column 1 (char 0)',)]

This is because sanic.testing import JSONDecodeError from json, but requests (used by requests_async) uses simplejson, which means that this

 try:
    response.json = response.json()
except (JSONDecodeError, UnicodeDecodeError):
    response.json = None

does not work cause it does not handle the actual type simplejson.errors.JSONDecodeError

Code snippet

Endpoint:

@blueprint.route("/")
async def do_something(request: Request) -> HTTPResponse:
    return response.text("", 200)

Request:

test_client = app.test_client
_ = test_client.get("/")

Expected behavior
No error.

Environment (please complete the following information):

  • OS: MacOS
  • Version: 19.6.3

Additional context
Add any other context about the problem here.

@wochinge
Copy link
Author

A simple quickfix would be to import JSONDecodeError from simplejson, I can submit a PR for that if you want.

@harshanarayana
Copy link
Contributor

harshanarayana commented Oct 16, 2019

@wochinge Hi Tobias, mind if I ask what are you using the SanicTestClient for? If you are using that to test your application then you should probably try https://github.com/yunstanford/pytest-sanic instead of the test client.

@wochinge
Copy link
Author

Hi @harshanarayana What speaks against using the test_client which is described here https://sanic.readthedocs.io/en/latest/sanic/testing.html ?

@stale
Copy link

stale bot commented Jan 14, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

@stale stale bot added the stale label Jan 14, 2020
@fabschn
Copy link

fabschn commented Jan 16, 2020

Trying to revive the discussion since I am currently facing the same issue.

I'm not explicitly calling response.text() but using the abort() function from sanic.exceptions.

I this case, the proposed solution of importing the JSONDecodeError from simplejson (from simplejson import JSONDecodeError) in sanic.testing leads to the expected behavior.

@wochinge Did you solve the issue in some way?
Also interested in arguments against using the test_client described in the documentation.

@stale stale bot removed the stale label Jan 16, 2020
@stale
Copy link

stale bot commented Apr 15, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

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

3 participants