Skip to content

Commit

Permalink
feat: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joffreybienvenu-infrabel committed Jan 9, 2024
1 parent 35a26d8 commit 26bf63f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 2 additions & 3 deletions airflow/providers/http/hooks/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"requests.auth.HTTPBasicAuth",
"requests.auth.HTTPProxyAuth",
"requests.auth.HTTPDigestAuth",
"aiohttp.BasicAuth"
"aiohttp.BasicAuth",
}
)

Expand Down Expand Up @@ -153,7 +153,6 @@ def load_connection_settings(self, *, headers: dict[Any, Any] | None = None) ->
self.base_url += f":{conn.port}"

conn_extra: dict = self._parse_extra(conn_extra=conn.extra_dejson)
print(conn_extra)
auth_args: list[str | None] = [conn.login, conn.password]
auth_kwargs: dict[str, Any] = conn_extra["auth_kwargs"]
auth_type: Any = (
Expand Down Expand Up @@ -490,7 +489,7 @@ async def run(
``aiohttp.ClientSession().get(json=obj)``.
"""
extra_options = extra_options or {}
headers, auth = await sync_to_async(self.load_connection_settings)(headers)
headers, auth = await sync_to_async(self.load_connection_settings)(headers=headers)

base_url = (self.base_url or "").rstrip("/")
endpoint = (endpoint or "").lstrip("/")
Expand Down
28 changes: 27 additions & 1 deletion tests/providers/http/hooks/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,32 @@ def test_connection_with_extra_auth_type_and_no_credentials(self, auth, mock_get
HttpHook().get_conn({})
auth.assert_called_once()

@mock.patch("airflow.providers.http.hooks.http.HttpHook.get_connection")
@mock.patch("tests.providers.http.hooks.test_http.CustomAuthBase.__init__")
def test_connection_with_string_headers_and_auth_kwargs(self, auth, mock_get_connection):
"""When passed via the UI, the 'headers' and 'auth_kwargs' fields' data is
saved as string.
"""
auth.return_value = None
conn = Connection(
conn_id="http_default",
conn_type="http",
login="username",
password="pass",
extra=r"""
{"auth_kwargs": "{\r\n \"endpoint\": \"http://localhost\"\r\n}",
"headers": "{\r\n \"some\": \"headers\"\r\n}"}
""",
)
mock_get_connection.return_value = conn

hook = HttpHook(auth_type=CustomAuthBase)
session = hook.get_conn({})

auth.assert_called_once_with("username", "pass", endpoint="http://localhost")
assert "auth_kwargs" not in session.headers
assert "some" in session.headers

@pytest.mark.parametrize("method", ["GET", "POST"])
def test_json_request(self, method, requests_mock):
obj1 = {"a": 1, "b": "abc", "c": [1, 2, {"d": 10}]}
Expand Down Expand Up @@ -602,7 +628,7 @@ async def test_async_post_request_with_error_code(self, aioresponse):
async def test_async_request_uses_connection_extra(self, aioresponse):
"""Test api call asynchronously with a connection that has extra field."""

connection_extra = {"bearer": "test"}
connection_extra = {"bearer": "test", "some": "header"}

aioresponse.post(
"http://test:8080/v1/test",
Expand Down

0 comments on commit 26bf63f

Please sign in to comment.