-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Zhiwei <[email protected]> Co-authored-by: Adam Hopkins <[email protected]>
- Loading branch information
1 parent
13d5a44
commit e4be70b
Showing
3 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from json import loads as sloads | ||
|
||
import pytest | ||
|
||
|
||
try: | ||
from ujson import loads as uloads | ||
|
||
NO_UJSON = False | ||
DEFAULT_LOADS = uloads | ||
except ModuleNotFoundError: | ||
NO_UJSON = True | ||
DEFAULT_LOADS = sloads | ||
|
||
from sanic import Request, Sanic, json | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def default_back_to_ujson(): | ||
yield | ||
Request._loads = DEFAULT_LOADS | ||
|
||
|
||
def test_change_decoder(): | ||
Sanic("Test", loads=sloads) | ||
assert Request._loads == sloads | ||
|
||
|
||
def test_change_decoder_to_some_custom(): | ||
def my_custom_decoder(some_str: str): | ||
dict = sloads(some_str) | ||
dict["some_key"] = "new_value" | ||
return dict | ||
|
||
app = Sanic("Test", loads=my_custom_decoder) | ||
assert Request._loads == my_custom_decoder | ||
|
||
req_body = {"some_key": "some_value"} | ||
|
||
@app.post("/test") | ||
def handler(request): | ||
new_json = request.json | ||
return json(new_json) | ||
|
||
req, res = app.test_client.post("/test", json=req_body) | ||
assert sloads(res.body) == {"some_key": "new_value"} | ||
|
||
|
||
@pytest.mark.skipif(NO_UJSON is True, reason="ujson not installed") | ||
def test_default_decoder(): | ||
Sanic("Test") | ||
assert Request._loads == uloads |