Add response.json()
This commit is contained in:
parent
a284d0b2ea
commit
2116d44777
@ -128,7 +128,7 @@ inspiration around the lower level networking details.
|
||||
* `.cookies` - **Cookies**
|
||||
* `.history` - **List[Response]**
|
||||
* `def .raise_for_status()` - **None**
|
||||
* `def .json()` - **Any** *TODO*
|
||||
* `def .json()` - **Any**
|
||||
* `def .read()` - **bytes**
|
||||
* `def .stream()` - **bytes iterator**
|
||||
* `def .raw()` - **bytes iterator**
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import cgi
|
||||
import email.message
|
||||
import json
|
||||
import typing
|
||||
import urllib.request
|
||||
from collections.abc import MutableMapping
|
||||
@ -773,6 +774,9 @@ class Response:
|
||||
if message:
|
||||
raise HttpError(message)
|
||||
|
||||
def json(self) -> typing.Any:
|
||||
return json.loads(self.content.decode("utf-8"))
|
||||
|
||||
@property
|
||||
def cookies(self) -> "Cookies":
|
||||
if not hasattr(self, "_cookies"):
|
||||
@ -838,6 +842,9 @@ class SyncResponse:
|
||||
def raise_for_status(self) -> None:
|
||||
return self._response.raise_for_status()
|
||||
|
||||
def json(self) -> typing.Any:
|
||||
return self._response.json()
|
||||
|
||||
def read(self) -> bytes:
|
||||
return self._loop.run_until_complete(self._response.read())
|
||||
|
||||
|
||||
@ -33,9 +33,7 @@ def test_basic_auth():
|
||||
response = client.get(url, auth=auth)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {
|
||||
"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="
|
||||
}
|
||||
assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}
|
||||
|
||||
|
||||
def test_basic_auth_in_url():
|
||||
@ -45,9 +43,7 @@ def test_basic_auth_in_url():
|
||||
response = client.get(url)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {
|
||||
"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="
|
||||
}
|
||||
assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}
|
||||
|
||||
|
||||
def test_basic_auth_on_session():
|
||||
@ -58,9 +54,7 @@ def test_basic_auth_on_session():
|
||||
response = client.get(url)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {
|
||||
"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="
|
||||
}
|
||||
assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}
|
||||
|
||||
|
||||
def test_custom_auth():
|
||||
@ -74,4 +68,4 @@ def test_custom_auth():
|
||||
response = client.get(url, auth=auth)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {"auth": "Token 123"}
|
||||
assert response.json() == {"auth": "Token 123"}
|
||||
|
||||
@ -42,7 +42,7 @@ def test_set_cookie():
|
||||
response = client.get(url, cookies=cookies)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {"cookies": "example-name=example-value"}
|
||||
assert response.json() == {"cookies": "example-name=example-value"}
|
||||
|
||||
|
||||
def test_set_cookie_with_cookiejar():
|
||||
@ -77,7 +77,7 @@ def test_set_cookie_with_cookiejar():
|
||||
response = client.get(url, cookies=cookies)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {"cookies": "example-name=example-value"}
|
||||
assert response.json() == {"cookies": "example-name=example-value"}
|
||||
|
||||
|
||||
def test_set_cookie_with_cookies_model():
|
||||
@ -93,7 +93,7 @@ def test_set_cookie_with_cookies_model():
|
||||
response = client.get(url, cookies=cookies)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {"cookies": "example-name=example-value"}
|
||||
assert response.json() == {"cookies": "example-name=example-value"}
|
||||
|
||||
|
||||
def test_get_cookie():
|
||||
@ -114,7 +114,7 @@ def test_cookie_persistence():
|
||||
with Client(dispatch=MockDispatch()) as client:
|
||||
response = client.get("http://example.org/echo_cookies")
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {"cookies": None}
|
||||
assert response.json() == {"cookies": None}
|
||||
|
||||
response = client.get("http://example.org/set_cookie")
|
||||
assert response.status_code == 200
|
||||
@ -123,4 +123,4 @@ def test_cookie_persistence():
|
||||
|
||||
response = client.get("http://example.org/echo_cookies")
|
||||
assert response.status_code == 200
|
||||
assert json.loads(response.text) == {"cookies": "example-name=example-value"}
|
||||
assert response.json() == {"cookies": "example-name=example-value"}
|
||||
|
||||
@ -205,9 +205,8 @@ async def test_cross_domain_redirect():
|
||||
url = "https://example.com/cross_domain"
|
||||
headers = {"Authorization": "abc"}
|
||||
response = await client.get(url, headers=headers)
|
||||
data = json.loads(response.content.decode())
|
||||
assert response.url == URL("https://example.org/cross_domain_target")
|
||||
assert "authorization" not in data["headers"]
|
||||
assert "authorization" not in response.json()["headers"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@ -216,9 +215,8 @@ async def test_same_domain_redirect():
|
||||
url = "https://example.org/cross_domain"
|
||||
headers = {"Authorization": "abc"}
|
||||
response = await client.get(url, headers=headers)
|
||||
data = json.loads(response.content.decode())
|
||||
assert response.url == URL("https://example.org/cross_domain_target")
|
||||
assert data["headers"]["authorization"] == "abc"
|
||||
assert response.json()["headers"]["authorization"] == "abc"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@ -227,9 +225,8 @@ async def test_body_redirect():
|
||||
url = "https://example.org/redirect_body"
|
||||
data = b"Example request body"
|
||||
response = await client.post(url, data=data)
|
||||
data = json.loads(response.content.decode())
|
||||
assert response.url == URL("https://example.org/redirect_body_target")
|
||||
assert data == {"body": "Example request body"}
|
||||
assert response.json() == {"body": "Example request body"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Loading…
Reference in New Issue
Block a user