Skip to content

Commit 52a7cfe

Browse files
[7.x] Fix Connection.log_*() body parameter type
Co-authored-by: Seth Michael Larson <[email protected]>
1 parent cde2a53 commit 52a7cfe

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

elasticsearch/connection/base.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ class Connection(object):
8888
method: str,
8989
full_url: str,
9090
path: str,
91-
body: str,
91+
body: Optional[bytes],
9292
status_code: int,
93-
response: bytes,
93+
response: str,
9494
duration: float,
9595
) -> None: ...
9696
def log_request_fail(
9797
self,
9898
method: str,
9999
full_url: str,
100100
path: str,
101-
body: str,
101+
body: Optional[bytes],
102102
duration: float,
103103
status_code: Optional[int] = ...,
104-
response: Optional[bytes] = ...,
104+
response: Optional[str] = ...,
105105
exception: Optional[Exception] = ...,
106106
) -> None: ...
107107
def _raise_error(self, status_code: int, raw_data: str) -> NoReturn: ...

elasticsearch/connection/http_requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def perform_request(
166166
method,
167167
url,
168168
prepared_request.path_url,
169-
body,
169+
orig_body,
170170
time.time() - start,
171171
exception=e,
172172
)

test_elasticsearch/test_connection.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from requests.auth import AuthBase
2828
from platform import python_version
2929

30+
import pytest
31+
3032
from elasticsearch.exceptions import (
3133
TransportError,
3234
ConflictError,
@@ -754,6 +756,19 @@ def test_uncompressed_body_logged(self, logger):
754756
self.assertEqual('> {"example": "body"}', req[0][0] % req[0][1:])
755757
self.assertEqual("< {}", resp[0][0] % resp[0][1:])
756758

759+
con = self._get_mock_connection(
760+
connection_params={"http_compress": True},
761+
status_code=500,
762+
response_body=b'{"hello":"world"}',
763+
)
764+
with pytest.raises(TransportError):
765+
con.perform_request("GET", "/", body=b'{"example": "body2"}')
766+
767+
self.assertEqual(4, logger.debug.call_count)
768+
_, _, req, resp = logger.debug.call_args_list
769+
self.assertEqual('> {"example": "body2"}', req[0][0] % req[0][1:])
770+
self.assertEqual('< {"hello":"world"}', resp[0][0] % resp[0][1:])
771+
757772
def test_defaults(self):
758773
con = self._get_mock_connection()
759774
request = self._get_request(con, "GET", "/")

0 commit comments

Comments
 (0)