[
https://issues.apache.org/jira/browse/AIRFLOW-3162?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16641081#comment-16641081
]
ASF GitHub Bot commented on AIRFLOW-3162:
-----------------------------------------
kaxil closed pull request #4001: [AIRFLOW-3162] Fix HttpHook URL parse error
when port is specified
URL: https://github.com/apache/incubator-airflow/pull/4001
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/airflow/hooks/http_hook.py b/airflow/hooks/http_hook.py
index caa89d3142..1e0c7b3058 100644
--- a/airflow/hooks/http_hook.py
+++ b/airflow/hooks/http_hook.py
@@ -98,7 +98,11 @@ def run(self, endpoint, data=None, headers=None,
extra_options=None):
session = self.get_conn(headers)
- url = self.base_url + endpoint
+ if not self.base_url.endswith('/') and not endpoint.startswith('/'):
+ url = self.base_url + '/' + endpoint
+ else:
+ url = self.base_url + endpoint
+
req = None
if self.method == 'GET':
# GET uses params
diff --git a/tests/hooks/test_http_hook.py b/tests/hooks/test_http_hook.py
index c8163322f9..e64e59fbca 100644
--- a/tests/hooks/test_http_hook.py
+++ b/tests/hooks/test_http_hook.py
@@ -42,6 +42,15 @@ def get_airflow_connection(conn_id=None):
)
+def get_airflow_connection_with_port(conn_id=None):
+ return models.Connection(
+ conn_id='http_default',
+ conn_type='http',
+ host='test.com',
+ port=1234
+ )
+
+
class TestHttpHook(unittest.TestCase):
"""Test get, post and raise_for_status"""
def setUp(self):
@@ -69,6 +78,32 @@ def test_raise_for_status_with_200(self, m):
resp = self.get_hook.run('v1/test')
self.assertEquals(resp.text, '{"status":{"status": 200}}')
+ @requests_mock.mock()
+ @mock.patch('requests.Request')
+ def test_get_request_with_port(self, m, request_mock):
+ from requests.exceptions import MissingSchema
+
+ with mock.patch(
+ 'airflow.hooks.base_hook.BaseHook.get_connection',
+ side_effect=get_airflow_connection_with_port
+ ):
+ expected_url = 'http://test.com:1234/some/endpoint'
+ for endpoint in ['some/endpoint', '/some/endpoint']:
+
+ try:
+ self.get_hook.run(endpoint)
+ except MissingSchema:
+ pass
+
+ request_mock.assert_called_once_with(
+ mock.ANY,
+ expected_url,
+ headers=mock.ANY,
+ params=mock.ANY
+ )
+
+ request_mock.reset_mock()
+
@requests_mock.mock()
def
test_get_request_do_not_raise_for_status_if_check_response_is_false(self, m):
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> HttpHook fails to parse URL when port is specified
> --------------------------------------------------
>
> Key: AIRFLOW-3162
> URL: https://issues.apache.org/jira/browse/AIRFLOW-3162
> Project: Apache Airflow
> Issue Type: Bug
> Reporter: Eric Chang
> Assignee: Eric Chang
> Priority: Minor
>
> https://github.com/apache/incubator-airflow/pull/3379 introduced a regression
> on HttpHook.get() where if a port is specified on
> airflow.models.Connection.port and the provided endpoint has no leading
> slash, the URL will fail to parse.
> With the connection:
>
> {{airflow.models.Connection(}}
> {{ conn_id='test_conn',}}
> {{ conn_type='http',}}
> {{ host='test.com',}}
> {{ port=1234}}
> {{)}}
> {{>>> hook = HttpHook(method='GET', http_conn_id='test_conn')}}
> {{>>> hook.get(endpoint='some/endpoint')}}
> {{----------------------------------------------------------------------}}
> {{Traceback (most recent call last):}}
> {{InvalidURL: Failed to parse: test.com:1234some/endpoint}}
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)