Skip to content

Commit 76e0d3c

Browse files
changAlice Berard
authored and
Alice Berard
committed
[AIRFLOW-3162] Fix HttpHook URL parse error when port is specified (apache#4001)
1 parent 49e1c82 commit 76e0d3c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

airflow/hooks/http_hook.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ def run(self, endpoint, data=None, headers=None, extra_options=None):
9898

9999
session = self.get_conn(headers)
100100

101-
url = self.base_url + endpoint
101+
if not self.base_url.endswith('/') and not endpoint.startswith('/'):
102+
url = self.base_url + '/' + endpoint
103+
else:
104+
url = self.base_url + endpoint
105+
102106
req = None
103107
if self.method == 'GET':
104108
# GET uses params

tests/hooks/test_http_hook.py

+35
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ def get_airflow_connection(conn_id=None):
4242
)
4343

4444

45+
def get_airflow_connection_with_port(conn_id=None):
46+
return models.Connection(
47+
conn_id='http_default',
48+
conn_type='http',
49+
host='test.com',
50+
port=1234
51+
)
52+
53+
4554
class TestHttpHook(unittest.TestCase):
4655
"""Test get, post and raise_for_status"""
4756
def setUp(self):
@@ -69,6 +78,32 @@ def test_raise_for_status_with_200(self, m):
6978
resp = self.get_hook.run('v1/test')
7079
self.assertEquals(resp.text, '{"status":{"status": 200}}')
7180

81+
@requests_mock.mock()
82+
@mock.patch('requests.Request')
83+
def test_get_request_with_port(self, m, request_mock):
84+
from requests.exceptions import MissingSchema
85+
86+
with mock.patch(
87+
'airflow.hooks.base_hook.BaseHook.get_connection',
88+
side_effect=get_airflow_connection_with_port
89+
):
90+
expected_url = 'http://test.com:1234/some/endpoint'
91+
for endpoint in ['some/endpoint', '/some/endpoint']:
92+
93+
try:
94+
self.get_hook.run(endpoint)
95+
except MissingSchema:
96+
pass
97+
98+
request_mock.assert_called_once_with(
99+
mock.ANY,
100+
expected_url,
101+
headers=mock.ANY,
102+
params=mock.ANY
103+
)
104+
105+
request_mock.reset_mock()
106+
72107
@requests_mock.mock()
73108
def test_get_request_do_not_raise_for_status_if_check_response_is_false(self, m):
74109

0 commit comments

Comments
 (0)