python: Use requests.Response.text instead of decoding content.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-10-29 16:13:37 -07:00 committed by Tim Abbott
parent 5fb667cf9e
commit 1802a50cc9
3 changed files with 5 additions and 6 deletions

View File

@ -310,7 +310,7 @@ def do_rest_call(base_url: str,
"%(response)s\"", "%(response)s\"",
{'message_url': get_message_url(event), {'message_url': get_message_url(event),
'status_code': response.status_code, 'status_code': response.status_code,
'response': response.content.decode()}) 'response': response.text})
failure_message = f"Third party responded with {response.status_code}" failure_message = f"Third party responded with {response.status_code}"
fail_with_message(event, failure_message) fail_with_message(event, failure_message)
notify_bot_owner(event, response.status_code, response.content) notify_bot_owner(event, response.status_code, response.content)

View File

@ -1,4 +1,3 @@
import json
import subprocess import subprocess
from argparse import ArgumentParser from argparse import ArgumentParser
from typing import Any from typing import Any
@ -76,7 +75,7 @@ class Command(ZulipBaseCommand):
try: try:
response.raise_for_status() response.raise_for_status()
except requests.HTTPError: except requests.HTTPError:
content_dict = json.loads(response.content.decode("utf-8")) content_dict = response.json()
raise CommandError("Error: " + content_dict['msg']) raise CommandError("Error: " + content_dict['msg'])
if response.json()['created']: if response.json()['created']:

View File

@ -79,7 +79,7 @@ class DoRestCallTests(ZulipTestCase):
final_response = do_rest_call('', None, mock_event, service_handler) final_response = do_rest_call('', None, mock_event, service_handler)
assert final_response is not None assert final_response is not None
self.assertEqual(m.output, [f"WARNING:root:Message http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/ triggered an outgoing webhook, returning status code 500.\n Content of response (in quotes): \"{final_response.content.decode()}\""]) self.assertEqual(m.output, [f"WARNING:root:Message http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/ triggered an outgoing webhook, returning status code 500.\n Content of response (in quotes): \"{final_response.text}\""])
bot_owner_notification = self.get_last_message() bot_owner_notification = self.get_last_message()
self.assertEqual(bot_owner_notification.content, self.assertEqual(bot_owner_notification.content,
'''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook. '''[A message](http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/) triggered an outgoing webhook.
@ -101,7 +101,7 @@ The webhook got a response with status code *500*.''')
final_response = do_rest_call('', None, mock_event, service_handler) final_response = do_rest_call('', None, mock_event, service_handler)
assert final_response is not None assert final_response is not None
self.assertEqual(m.output, [f"WARNING:root:Message http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/ triggered an outgoing webhook, returning status code 400.\n Content of response (in quotes): \"{final_response.content.decode()}\""]) self.assertEqual(m.output, [f"WARNING:root:Message http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/ triggered an outgoing webhook, returning status code 400.\n Content of response (in quotes): \"{final_response.text}\""])
self.assertTrue(mock_fail.called) self.assertTrue(mock_fail.called)
@ -122,7 +122,7 @@ The webhook got a response with status code *400*.''')
final_response = do_rest_call('', 'payload-stub', mock_event, service_handler) final_response = do_rest_call('', 'payload-stub', mock_event, service_handler)
assert final_response is not None assert final_response is not None
self.assertEqual(m.output, [f"WARNING:root:Message http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/ triggered an outgoing webhook, returning status code {final_response.status_code}.\n Content of response (in quotes): \"{final_response.content.decode()}\""]) self.assertEqual(m.output, [f"WARNING:root:Message http://zulip.testserver/#narrow/stream/999-Verona/topic/Foo/near/ triggered an outgoing webhook, returning status code {final_response.status_code}.\n Content of response (in quotes): \"{final_response.text}\""])
kwargs = mock_request.call_args[1] kwargs = mock_request.call_args[1]
self.assertEqual(kwargs['data'], 'payload-stub') self.assertEqual(kwargs['data'], 'payload-stub')