dstandish 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_r361814771
 
 

 ##########
 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:
   ok i was confused by the fact that you are looking for `NoMockAddress` error 
it's a little confusing but i now understand that's how requests mock is 
designed.
   
   but anyway, so you are trying to test the test here.  i think you should 
chop this.  just test your feature, don't test the functionality of requests 
mock.  fine to do it locally to satisfy yourself that it's working right, but i 
don't think it belongs in here permanently.
   
   that said... this test-of-the-test is not working right anyway, because on 
line 368 if you replace `obj2` with `obj1`, the test still passes.  this is 
because you aren't mocking `airflow.hooks.base_hook.BaseHook.get_connection` at 
this point of the code, so it is finding no match, irrespective of the object 
you supply.  
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to