Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

outgoing_webhook: Use json= to automatically set content-type.

Alex Vandiverd88e6fe3fa89
diff --git a/zerver/lib/outgoing_webhook.py b/zerver/lib/outgoing_webhook.py
index 75f56b64320..107e0f8724e 100644
--- a/zerver/lib/outgoing_webhook.py
+++ b/zerver/lib/outgoing_webhook.py
@@ -74,11 +74,9 @@ class GenericOutgoingWebhookService(OutgoingWebhookServiceInterface):
def send_data_to_server(self, base_url: str, request_data: Any) -> Response:
user_agent = "ZulipOutgoingWebhook/" + ZULIP_VERSION
headers = {
- "content-type": "application/json",
"User-Agent": user_agent,
}
- response = requests.request("POST", base_url, data=request_data, headers=headers)
- return response
+ return requests.request("POST", base_url, json=request_data, headers=headers)
def process_success(self, response_json: Dict[str, Any]) -> Optional[Dict[str, Any]]:
if "response_not_required" in response_json and response_json["response_not_required"]:
diff --git a/zerver/tests/test_outgoing_webhook_system.py b/zerver/tests/test_outgoing_webhook_system.py
index 02c3bd91ad6..89322f77db6 100644
--- a/zerver/tests/test_outgoing_webhook_system.py
+++ b/zerver/tests/test_outgoing_webhook_system.py
@@ -25,17 +25,15 @@ class ResponseMock:
self.text = content.decode()
-def request_exception_error(
- http_method: Any, final_url: Any, data: Any, **request_kwargs: Any
-) -> Any:
+def request_exception_error(http_method: Any, final_url: Any, **request_kwargs: Any) -> Any:
raise requests.exceptions.RequestException("I'm a generic exception :(")
-def timeout_error(http_method: Any, final_url: Any, data: Any, **request_kwargs: Any) -> Any:
+def timeout_error(http_method: Any, final_url: Any, **request_kwargs: Any) -> Any:
raise requests.exceptions.Timeout("Time is up!")
-def connection_error(http_method: Any, final_url: Any, data: Any, **request_kwargs: Any) -> Any:
+def connection_error(http_method: Any, final_url: Any, **request_kwargs: Any) -> Any:
raise requests.exceptions.ConnectionError()
@@ -144,8 +142,12 @@ The webhook got a response with status code *400*.""",
mock_event = self.mock_event(bot_user)
service_handler = GenericOutgoingWebhookService("token", bot_user, "service")
- with mock.patch("requests.request") as mock_request, self.assertLogs(level="WARNING") as m:
- final_response = do_rest_call("", "payload-stub", mock_event, service_handler)
+ with mock.patch("requests.sessions.Session.send") as mock_send, self.assertLogs(
+ level="WARNING"
+ ) as m:
+ final_response = do_rest_call(
+ "https://example.com/", "payload-stub", mock_event, service_handler
+ )
assert final_response is not None
self.assertEqual(
@@ -155,15 +157,14 @@ The webhook got a response with status code *400*.""",
],
)
- kwargs = mock_request.call_args[1]
- self.assertEqual(kwargs["data"], "payload-stub")
-
- user_agent = "ZulipOutgoingWebhook/" + ZULIP_VERSION
- headers = {
- "content-type": "application/json",
- "User-Agent": user_agent,
- }
- self.assertEqual(kwargs["headers"], headers)
+ mock_send.assert_called_once()
+ prepared_request = mock_send.call_args[0][0]
+ user_agent = "ZulipOutgoingWebhook/" + ZULIP_VERSION
+ headers = {
+ "Content-Type": "application/json",
+ "User-Agent": user_agent,
+ }
+ self.assertLessEqual(headers.items(), prepared_request.headers.items())
def test_error_handling(self) -> None:
bot_user = self.example_user("outgoing_webhook_bot")