From e916060866ed70125e7607099e826efa9110f2d9 Mon Sep 17 00:00:00 2001 From: dblock Date: Mon, 23 Oct 2023 15:42:52 -0400 Subject: [PATCH] Added unit test for Urllib3AWSV4SignerAuth adding headers. Signed-off-by: dblock --- .../test_urllib3_http_connection.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test_opensearchpy/test_connection/test_urllib3_http_connection.py b/test_opensearchpy/test_connection/test_urllib3_http_connection.py index b3ae0bca..854e22e4 100644 --- a/test_opensearchpy/test_connection/test_urllib3_http_connection.py +++ b/test_opensearchpy/test_connection/test_urllib3_http_connection.py @@ -179,6 +179,29 @@ def test_http_auth_list(self): con.headers, ) + @pytest.mark.skipif( + sys.version_info < (3, 6), reason="Urllib3AWSV4SignerAuth requires python3.6+" + ) + @patch( + "urllib3.HTTPConnectionPool.urlopen", + return_value=Mock(status=200, headers=HTTPHeaderDict({}), data=b"{}"), + ) + def test_aws_signer_as_http_auth_adds_headers(self, mock_open): + from opensearchpy.helpers.signer import Urllib3AWSV4SignerAuth + + auth = Urllib3AWSV4SignerAuth(self.mock_session(), "us-west-2") + con = Urllib3HttpConnection(http_auth=auth, headers={"x": "y"}) + con.perform_request("GET", "/") + self.assertEqual(mock_open.call_count, 1) + headers = mock_open.call_args[1]["headers"] + self.assertEqual(headers["x"], "y") + self.assertTrue( + headers["Authorization"].startswith("AWS4-HMAC-SHA256 Credential=") + ) + self.assertIn("X-Amz-Date", headers) + self.assertIn("X-Amz-Security-Token", headers) + self.assertIn("X-Amz-Content-SHA256", headers) + @pytest.mark.skipif( sys.version_info < (3, 6), reason="Urllib3AWSV4SignerAuth requires python3.6+" )