baolsen commented on a change in pull request #6886: [AIRFLOW-6327] http_hook:
Accept json= parameter for payload
URL: https://github.com/apache/airflow/pull/6886#discussion_r361616709
##########
File path: tests/hooks/test_http_hook.py
##########
@@ -334,5 +338,35 @@ def test_connection_without_host(self,
mock_get_connection):
hook.get_conn({})
self.assertEqual(hook.base_url, 'http://')
+ @requests_mock.mock()
+ def test_post_json_request(self, mock_requests):
+ obj1 = {'a': 1, 'b': 'abc', 'c': [1, 2, {"d": 10}]}
+ obj2 = [1, 2, 3]
+
+ # Ensure that obj1 was encoded to JSON
+ def match_obj1(request):
+ return json.loads(request.text) == obj1
+
+ # Filters to catch posted JSON
+ mock_requests.post(
+ '//test:8080/v1/test',
+ status_code=200,
+ request_headers={'Content-Type': 'application/json'},
+ text='test_post_json_request',
+ additional_matcher=match_obj1
+ )
+
+ with mock.patch(
+ 'airflow.hooks.base_hook.BaseHook.get_connection',
+ side_effect=get_airflow_connection
+ ):
+ # Send obj1 as JSON and verify it matched the mock
+ resp = self.post_hook.run('v1/test', json=obj1)
+ self.assertEqual(resp.status_code, 200)
+ self.assertEqual(resp.text, 'test_post_json_request')
+ # Ensure that obj1 was what was sent
+ with self.assertRaises(requests_mock.exceptions.NoMockAddress):
+ resp = self.post_hook.run('v1/test', json=obj2)
Review comment:
I wanted to be sure that obj1 was encoded & posted correctly, not just that
a post was done.
So I needed to check the contents of the request payload - using
additional_matcher to ensure that what was posted matched. However since
request_mock is a bit confusing to me I wasnt convinced that additional_matcher
was going to be used. So I added a check to post obj2 the same way as obj1,
which raises an exception
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services