Skip to content
Myr

Add response.reason

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 df84cde · Browse files · 3 files+18 added−1 removed
This commit

State

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

Diff stats

3 files+18 added−1 removed

  • tests:+7 added−0 removed
  • other:+11 added−1 removed

3 files+18 added−1 removed

modified:httpcore/connections.py+2 added-1 removed
66 unmodified lines
67 event = await self._receive_event()68 if isinstance(event, h11.InformationalResponse):69 event = await self._receive_event()70 assert isinstance(event, h11.Response)71 reason = event.reason.decode('latin1')72 status_code = event.status_code73 headers = event.headers74 body = self._body_iter()75 return Response(75 status_code=status_code, headers=headers, body=body, on_close=self._release76 status_code=status_code, reason=reason, headers=headers, body=body, on_close=self._release77 )7879 async def _body_iter(self) -> typing.AsyncIterator[bytes]:80 event = await self._receive_event()
52 unmodified lines
modified:httpcore/datastructures.py+9 added
1import http2import typing3from urllib.parse import urlsplit45from .decoders import (
116 unmodified lines
122 def __init__(123 self,124 status_code: int,125 *,126 reason: typing.Optional[str] = None,127 headers: typing.Sequence[typing.Tuple[bytes, bytes]] = (),128 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",129 on_close: typing.Callable = None,130 ):131 self.status_code = status_code132 if not reason:133 try:134 self.reason = http.HTTPStatus(status_code).phrase135 except ValueError as exc:136 self.reason = ""137 else:138 self.reason = reason139 self.headers = list(headers)140 self.on_close = on_close141 self.is_closed = False142 self.is_streamed = False
67 unmodified lines
modified:tests/test_responses.py+7 added
22 unmodified lines
23@pytest.mark.asyncio24async def test_request():25 response = await http.request("GET", "http://example.com")26 assert response.status_code == 20027 assert response.reason == "OK"28 assert response.body == b"Hello, world!"29 assert response.is_closed3031
80 unmodified lines
112 await response.close()113114 with pytest.raises(httpcore.ResponseClosed):115 await response.read()116117118def test_unknown_status_code():119 response = httpcore.Response(600)120 assert response.status_code == 600121 assert response.reason == ""