-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any | ||
|
||
import requests # type: ignore | ||
from spiffworkflow_connector_command.command_interface import ConnectorCommand | ||
from spiffworkflow_connector_command.command_interface import ConnectorProxyResponseDict | ||
|
||
from connector_http.http_request_base import HttpRequestBase | ||
|
||
|
||
class PatchRequest(ConnectorCommand, HttpRequestBase): | ||
def __init__( | ||
self, | ||
url: str, | ||
headers: dict[str, str] | None = None, | ||
data: dict[str, str] | None = None, | ||
basic_auth_username: str | None = None, | ||
basic_auth_password: str | None = None, | ||
): | ||
HttpRequestBase.__init__( | ||
self, url=url, headers=headers, basic_auth_username=basic_auth_username, basic_auth_password=basic_auth_password | ||
) | ||
|
||
self.data = data or {} | ||
|
||
def execute(self, _config: Any, _task_data: dict) -> ConnectorProxyResponseDict: | ||
return self.run_request(requests.patch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
from typing import Any | ||
from unittest.mock import patch | ||
|
||
from connector_http.commands.patch_request import PatchRequest | ||
|
||
|
||
class TestPatchRequest: | ||
def test_patch_html_from_url(self) -> None: | ||
request = PatchRequest(url="http://example.com") | ||
return_html = "<html>Hey</html>" | ||
with patch("requests.patch") 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 response["command_response"]["body"] == json.dumps({"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 | ||
|
||
def test_patch_json_from_url(self) -> None: | ||
request = PatchRequest(url="http://example.com") | ||
return_json = {"hey": "we_return"} | ||
with patch("requests.patch") as mock_request: | ||
mock_request.return_value.status_code = 200 | ||
mock_request.return_value.ok = True | ||
mock_request.return_value.headers = {"Content-Type": "application/json"} | ||
mock_request.return_value.text = json.dumps(return_json) | ||
response = request.execute(None, {}) | ||
assert mock_request.call_count == 1 | ||
|
||
assert response is not None | ||
assert response["command_response"]["body"] == json.dumps(return_json) | ||
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 | ||
|
||
def test_patch_can_handle_500(self, sleepless: Any) -> None: | ||
request = PatchRequest(url="http://example.com") | ||
return_json = {"error": "we_did_error"} | ||
with patch("requests.patch") as mock_request: | ||
mock_request.return_value.status_code = 500 | ||
mock_request.return_value.headers = {"Content-Type": "application/json"} | ||
mock_request.return_value.text = json.dumps(return_json) | ||
response = request.execute(None, {}) | ||
assert mock_request.call_count == 1 | ||
|
||
assert response is not None | ||
assert response["command_response"]["body"] == json.dumps(return_json) | ||
assert response["command_response"]["http_status"] == 500 | ||
assert response["command_response"]["mimetype"] == "application/json" | ||
assert response["error"] is not None | ||
assert response["spiff__logs"] is not None | ||
assert len(response["spiff__logs"]) > 0 |