Skip to content

Missing surrogatepass in urllib3 handler #1212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elasticsearch/connection/http_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def perform_request(
try:
response = self.session.send(prepared_request, **send_kwargs)
duration = time.time() - start
raw_data = response.text
raw_data = response.content.decode("utf-8", "surrogatepass")
except Exception as e:
self.log_request_fail(
method,
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch/connection/http_urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def perform_request(
method, url, body, retries=Retry(False), headers=request_headers, **kw
)
duration = time.time() - start
raw_data = response.data.decode("utf-8")
raw_data = response.data.decode("utf-8", "surrogatepass")
except Exception as e:
self.log_request_fail(
method, full_url, url, orig_body, time.time() - start, exception=e
Expand Down
22 changes: 18 additions & 4 deletions test_elasticsearch/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,24 @@ def test_uncompressed_body_logged(self, logger):
self.assertEquals('> {"example": "body"}', req[0][0] % req[0][1:])
self.assertEquals("< {}", resp[0][0] % resp[0][1:])

def test_surrogatepass_into_bytes(self):
buf = b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"
con = self._get_mock_connection(response_body=buf)
status, headers, data = con.perform_request("GET", "/")
self.assertEqual("你好\uda6a", data)


class TestRequestsConnection(TestCase):
def _get_mock_connection(
self, connection_params={}, status_code=200, response_body="{}"
self, connection_params={}, status_code=200, response_body=b"{}"
):
con = RequestsHttpConnection(**connection_params)

def _dummy_send(*args, **kwargs):
dummy_response = Mock()
dummy_response.headers = {}
dummy_response.status_code = status_code
dummy_response.text = response_body
dummy_response.content = response_body
dummy_response.request = args[0]
dummy_response.cookies = {}
_dummy_send.call_args = (args, kwargs)
Expand Down Expand Up @@ -653,7 +659,9 @@ def test_head_with_404_doesnt_get_logged(self, logger):
@patch("elasticsearch.connection.base.tracer")
@patch("elasticsearch.connection.base.logger")
def test_failed_request_logs_and_traces(self, logger, tracer):
con = self._get_mock_connection(response_body='{"answer": 42}', status_code=500)
con = self._get_mock_connection(
response_body=b'{"answer": 42}', status_code=500
)
self.assertRaises(
TransportError,
con.perform_request,
Expand All @@ -679,7 +687,7 @@ def test_failed_request_logs_and_traces(self, logger, tracer):
@patch("elasticsearch.connection.base.tracer")
@patch("elasticsearch.connection.base.logger")
def test_success_logs_and_traces(self, logger, tracer):
con = self._get_mock_connection(response_body="""{"answer": "that's it!"}""")
con = self._get_mock_connection(response_body=b"""{"answer": "that's it!"}""")
status, headers, data = con.perform_request(
"GET",
"/",
Expand Down Expand Up @@ -777,3 +785,9 @@ def test_url_prefix(self, tracer):
"curl -H 'Content-Type: application/json' -XGET 'http://localhost:9200/_search?pretty' -d '{\n \"answer\": 42\n}'",
tracer.info.call_args[0][0] % tracer.info.call_args[0][1:],
)

def test_surrogatepass_into_bytes(self):
buf = b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa"
con = self._get_mock_connection(response_body=buf)
status, headers, data = con.perform_request("GET", "/")
self.assertEqual("你好\uda6a", data)