Skip to content

Commit

Permalink
Merge pull request #8 from sartography/add-json-content-type-header
Browse files Browse the repository at this point in the history
add-json-content-type-header
  • Loading branch information
jasquat authored Jun 12, 2024
2 parents 31e76be + 270c43c commit c540e66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
fail-fast: true
matrix:
python-version: [ "3.10", "3.11" ]
python-version: ["3.10", "3.11"]
runs-on: ubuntu-latest
steps:
- name: Check out repository
Expand Down
9 changes: 8 additions & 1 deletion src/connector_http/http_request_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import time
from collections.abc import Callable
from typing import Any

import requests # type: ignore
import xmltodict
Expand Down Expand Up @@ -71,7 +72,7 @@ def log(msg: str) -> None:

try:
log(f"Will call {self.url}")
arguments = {
arguments: dict[str, Any] = {
"url": self.url,
"headers": self.headers,
"auth": auth,
Expand All @@ -80,6 +81,12 @@ def log(msg: str) -> None:
if self.params is not None:
arguments["params"] = self.params
if self.data is not None:
if (
"Content-Type" not in self.headers
and "Content-type" not in self.headers
and "content-type" not in self.headers
):
arguments["headers"]["Content-Type"] = "application/json"
arguments["json"] = self.data
http_response = request_function(**arguments)
log(f"Did call {self.url}")
Expand Down
32 changes: 32 additions & 0 deletions tests/connector_http/unit/test_put_request_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@


class TestPutRequestV2:
expected_call_args = {
"url": "http://example.com",
"headers": {"Content-Type": "application/json"},
"auth": None,
"timeout": 300,
"json": {},
}

def test_put_html_from_url(self) -> None:
request = PutRequestV2(url="http://example.com")
return_html = "<html>Hey</html>"
Expand All @@ -15,6 +23,7 @@ def test_put_html_from_url(self) -> None:
mock_request.return_value.text = return_html
response = request.execute(None, {})
assert mock_request.call_count == 1
assert mock_request.call_args_list[0].kwargs == self.expected_call_args

assert response["command_response"]["body"] == {"raw_response": return_html}
assert response["command_response"]["http_status"] == 200
Expand All @@ -33,6 +42,7 @@ def test_put_json_from_url(self) -> None:
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 1
assert mock_request.call_args_list[0].kwargs == self.expected_call_args

assert response is not None
assert response["command_response"]["body"] == return_json
Expand All @@ -51,6 +61,7 @@ def test_put_can_handle_500(self, sleepless: Any) -> None:
mock_request.return_value.text = json.dumps(return_json)
response = request.execute(None, {})
assert mock_request.call_count == 1
assert mock_request.call_args_list[0].kwargs == self.expected_call_args

assert response is not None
assert response["command_response"]["body"] == return_json
Expand All @@ -59,3 +70,24 @@ def test_put_can_handle_500(self, sleepless: Any) -> None:
assert response["error"] is not None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

def test_put_does_not_change_content_type(self) -> None:
request = PutRequestV2(url="http://example.com", headers={"Content-Type": "application/xml"})
return_html = "<html>Hey</html>"
with patch("requests.put") as mock_request:
mock_request.return_value.status_code = 200
mock_request.return_value.ok = True
mock_request.return_value.text = return_html
response = request.execute(None, {})
assert mock_request.call_count == 1
assert mock_request.call_args_list[0].kwargs == {
**self.expected_call_args,
**{"headers": {"Content-Type": "application/xml"}},
}

assert response["command_response"]["body"] == {"raw_response": return_html}
assert response["command_response"]["http_status"] == 200
assert response["command_response"]["mimetype"] == "application/json"
assert response["error"] is None
assert response["spiff__logs"] is not None
assert len(response["spiff__logs"]) > 0

0 comments on commit c540e66

Please sign in to comment.