Skip to content
Myr

Add response.json()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent a284d0b · Browse files · 5 files+20 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+20 added−22 removed

  • tests:+12 added−21 removed
  • other:+8 added−1 removed

5 files+20 added−22 removed

modified:httpcore/models.py+7 added
1import asyncio2import cgi3import email.message4import json5import typing6import urllib.request7from collections.abc import MutableMapping8from http.cookiejar import Cookie, CookieJar
764 unmodified lines
773774 if message:775 raise HttpError(message)776777 def json(self) -> typing.Any:778 return json.loads(self.content.decode("utf-8"))779780 @property781 def cookies(self) -> "Cookies":782 if not hasattr(self, "_cookies"):783 assert self.request is not None
57 unmodified lines
841842 def raise_for_status(self) -> None:843 return self._response.raise_for_status()844845 def json(self) -> typing.Any:846 return self._response.json()847848 def read(self) -> bytes:849 return self._loop.run_until_complete(self._response.read())850851 def stream(self) -> typing.Iterator[bytes]:
199 unmodified lines
modified:tests/client/test_auth.py+4 added-10 removed
31 unmodified lines
32 with Client(dispatch=MockDispatch()) as client:33 response = client.get(url, auth=auth)3435 assert response.status_code == 20036 assert json.loads(response.text) == {37 "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="38 }36 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}373839def test_basic_auth_in_url():40 url = "https://tomchristie:password123@example.org/"
1 unmodified line
42 with Client(dispatch=MockDispatch()) as client:43 response = client.get(url)4445 assert response.status_code == 20048 assert json.loads(response.text) == {49 "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="50 }46 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}474849def test_basic_auth_on_session():50 url = "https://example.org/"
2 unmodified lines
53 with Client(dispatch=MockDispatch(), auth=auth) as client:54 response = client.get(url)5556 assert response.status_code == 20061 assert json.loads(response.text) == {62 "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="63 }57 assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}585960def test_custom_auth():61 url = "https://example.org/"
5 unmodified lines
67 with Client(dispatch=MockDispatch()) as client:68 response = client.get(url, auth=auth)6970 assert response.status_code == 20077 assert json.loads(response.text) == {"auth": "Token 123"}71 assert response.json() == {"auth": "Token 123"}
modified:tests/client/test_cookies.py+5 added-5 removed
40 unmodified lines
41 with Client(dispatch=MockDispatch()) as client:42 response = client.get(url, cookies=cookies)4344 assert response.status_code == 20045 assert json.loads(response.text) == {"cookies": "example-name=example-value"}45 assert response.json() == {"cookies": "example-name=example-value"}464748def test_set_cookie_with_cookiejar():49 """
26 unmodified lines
76 with Client(dispatch=MockDispatch()) as client:77 response = client.get(url, cookies=cookies)7879 assert response.status_code == 20080 assert json.loads(response.text) == {"cookies": "example-name=example-value"}80 assert response.json() == {"cookies": "example-name=example-value"}818283def test_set_cookie_with_cookies_model():84 """
7 unmodified lines
92 with Client(dispatch=MockDispatch()) as client:93 response = client.get(url, cookies=cookies)9495 assert response.status_code == 20096 assert json.loads(response.text) == {"cookies": "example-name=example-value"}96 assert response.json() == {"cookies": "example-name=example-value"}979899def test_get_cookie():100 url = "http://example.org/set_cookie"
12 unmodified lines
113 """114 with Client(dispatch=MockDispatch()) as client:115 response = client.get("http://example.org/echo_cookies")116 assert response.status_code == 200117 assert json.loads(response.text) == {"cookies": None}117 assert response.json() == {"cookies": None}118119 response = client.get("http://example.org/set_cookie")120 assert response.status_code == 200121 assert response.cookies["example-name"] == "example-value"122 assert client.cookies["example-name"] == "example-value"123124 response = client.get("http://example.org/echo_cookies")125 assert response.status_code == 200126 assert json.loads(response.text) == {"cookies": "example-name=example-value"}126 assert response.json() == {"cookies": "example-name=example-value"}
modified:tests/client/test_redirects.py+3 added-6 removed
203 unmodified lines
204 client = AsyncClient(dispatch=MockDispatch())205 url = "https://example.com/cross_domain"206 headers = {"Authorization": "abc"}207 response = await client.get(url, headers=headers)208 data = json.loads(response.content.decode())208 assert response.url == URL("https://example.org/cross_domain_target")210 assert "authorization" not in data["headers"]209 assert "authorization" not in response.json()["headers"]210211212@pytest.mark.asyncio213async def test_same_domain_redirect():214 client = AsyncClient(dispatch=MockDispatch())215 url = "https://example.org/cross_domain"216 headers = {"Authorization": "abc"}217 response = await client.get(url, headers=headers)219 data = json.loads(response.content.decode())218 assert response.url == URL("https://example.org/cross_domain_target")221 assert data["headers"]["authorization"] == "abc"219 assert response.json()["headers"]["authorization"] == "abc"220221222@pytest.mark.asyncio223async def test_body_redirect():224 client = AsyncClient(dispatch=MockDispatch())225 url = "https://example.org/redirect_body"226 data = b"Example request body"227 response = await client.post(url, data=data)230 data = json.loads(response.content.decode())228 assert response.url == URL("https://example.org/redirect_body_target")232 assert data == {"body": "Example request body"}229 assert response.json() == {"body": "Example request body"}230231232@pytest.mark.asyncio233async def test_cannot_redirect_streaming_body():
8 unmodified lines
modified:README.md+1 added-1 removed
126 unmodified lines
127* `.request` - **Request**128* `.cookies` - **Cookies**129* `.history` - **List[Response]**130* `def .raise_for_status()` - **None**131* `def .json()` - **Any** *TODO*131* `def .json()` - **Any**132* `def .read()` - **bytes**133* `def .stream()` - **bytes iterator**134* `def .raw()` - **bytes iterator**135* `def .close()` - **None**
115 unmodified lines