Skip to content

Commit be9acfa

Browse files
committed
Make sure we handle quotes correctly in the trace logger
1 parent 1345b36 commit be9acfa

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

elasticsearch/connection/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def log_request_success(self, method, full_url, path, body, status_code, respons
3939
def _pretty_json(data):
4040
# pretty JSON in tracer curl logs
4141
try:
42-
return json.dumps(json.loads(data), sort_keys=True, indent=2, separators=(',', ': '))
42+
return json.dumps(json.loads(data), sort_keys=True, indent=2, separators=(',', ': ')).replace("'", r'\u0027')
4343
except (ValueError, TypeError):
4444
# non-json data or a bulk request
4545
return data

test_elasticsearch/test_connection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,19 @@ def test_failed_request_logs_and_traces(self, logger, tracer):
128128
@patch('elasticsearch.connection.base.tracer')
129129
@patch('elasticsearch.connection.base.logger')
130130
def test_success_logs_and_traces(self, logger, tracer):
131-
con = self._get_mock_connection(response_body='{"answer": 42}')
132-
status, data = con.perform_request('GET', '/', {'param': 42}, '{}')
131+
con = self._get_mock_connection(response_body='''{"answer": "that's it!"}''')
132+
status, data = con.perform_request('GET', '/', {'param': 42}, '''{"question": "what's that?"}''')
133133

134134
# trace request
135135
self.assertEquals(1, tracer.info.call_count)
136136
self.assertEquals(
137-
"curl -XGET 'http://localhost:9200/?pretty&param=42' -d '{}'",
137+
"""curl -XGET 'http://localhost:9200/?pretty&param=42' -d '{\n "question": "what\\u0027s that?"\n}'""",
138138
tracer.info.call_args[0][0] % tracer.info.call_args[0][1:]
139139
)
140140
# trace response
141141
self.assertEquals(1, tracer.debug.call_count)
142142
self.assertTrue(re.match(
143-
'#\[200\] \(0.[0-9]{3}s\)\n#\{\n# "answer": 42\n#\}',
143+
'#\[200\] \(0.[0-9]{3}s\)\n#\{\n# "answer": "that\\\\u0027s it!"\n#\}',
144144
tracer.debug.call_args[0][0] % tracer.debug.call_args[0][1:]
145145
))
146146

@@ -154,11 +154,11 @@ def test_success_logs_and_traces(self, logger, tracer):
154154
self.assertEquals(2, logger.debug.call_count)
155155
req, resp = logger.debug.call_args_list
156156
self.assertEquals(
157-
'> {}',
157+
'> {"question": "what\'s that?"}',
158158
req[0][0] % req[0][1:]
159159
)
160160
self.assertEquals(
161-
'< {"answer": 42}',
161+
'< {"answer": "that\'s it!"}',
162162
resp[0][0] % resp[0][1:]
163163
)
164164

0 commit comments

Comments
 (0)