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
Provenance
- Commit
2116d44- Tree
956c20b- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
a284d0b
5 files changed+20 added−22 removed
modified:httpcore/models.py+7 added
1 import asyncio2 import cgi3 import email.message4+import json5 import typing6 import urllib.request7 from collections.abc import MutableMapping8 from http.cookiejar import Cookie, CookieJar764 unmodified lines773 774 if message:775 raise HttpError(message)776 777+ def json(self) -> typing.Any:778+ return json.loads(self.content.decode("utf-8"))779+780 @property781 def cookies(self) -> "Cookies":782 if not hasattr(self, "_cookies"):783 assert self.request is not None57 unmodified lines841 842 def raise_for_status(self) -> None:843 return self._response.raise_for_status()844 845+ def json(self) -> typing.Any:846+ return self._response.json()847+848 def read(self) -> bytes:849 return self._loop.run_until_complete(self._response.read())850 851 def stream(self) -> typing.Iterator[bytes]:199 unmodified linesmodified:tests/client/test_auth.py+4 added-10 removed
31 unmodified lines32 with Client(dispatch=MockDispatch()) as client:33 response = client.get(url, auth=auth)34 35 assert response.status_code == 20036- assert json.loads(response.text) == {37- "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="38- }36+ assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}37 38 39 def test_basic_auth_in_url():40 url = "https://tomchristie:password123@example.org/"1 unmodified line42 with Client(dispatch=MockDispatch()) as client:43 response = client.get(url)44 45 assert response.status_code == 20048- assert json.loads(response.text) == {49- "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="50- }46+ assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}47 48 49 def test_basic_auth_on_session():50 url = "https://example.org/"2 unmodified lines53 with Client(dispatch=MockDispatch(), auth=auth) as client:54 response = client.get(url)55 56 assert response.status_code == 20061- assert json.loads(response.text) == {62- "auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="63- }57+ assert response.json() == {"auth": "Basic dG9tY2hyaXN0aWU6cGFzc3dvcmQxMjM="}58 59 60 def test_custom_auth():61 url = "https://example.org/"5 unmodified lines67 with Client(dispatch=MockDispatch()) as client:68 response = client.get(url, auth=auth)69 70 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 lines41 with Client(dispatch=MockDispatch()) as client:42 response = client.get(url, cookies=cookies)43 44 assert response.status_code == 20045- assert json.loads(response.text) == {"cookies": "example-name=example-value"}45+ assert response.json() == {"cookies": "example-name=example-value"}46 47 48 def test_set_cookie_with_cookiejar():49 """26 unmodified lines76 with Client(dispatch=MockDispatch()) as client:77 response = client.get(url, cookies=cookies)78 79 assert response.status_code == 20080- assert json.loads(response.text) == {"cookies": "example-name=example-value"}80+ assert response.json() == {"cookies": "example-name=example-value"}81 82 83 def test_set_cookie_with_cookies_model():84 """7 unmodified lines92 with Client(dispatch=MockDispatch()) as client:93 response = client.get(url, cookies=cookies)94 95 assert response.status_code == 20096- assert json.loads(response.text) == {"cookies": "example-name=example-value"}96+ assert response.json() == {"cookies": "example-name=example-value"}97 98 99 def test_get_cookie():100 url = "http://example.org/set_cookie"12 unmodified lines113 """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}118 119 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"123 124 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 lines204 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"]210 211 212 @pytest.mark.asyncio213 async 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"220 221 222 @pytest.mark.asyncio223 async 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"}230 231 232 @pytest.mark.asyncio233 async def test_cannot_redirect_streaming_body():8 unmodified linesmodified:README.md+1 added-1 removed
126 unmodified lines127 * `.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