This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git


The following commit(s) were added to refs/heads/main by this push:
     new 90e50575 Disallow default header to be overwritten (#577)
90e50575 is described below

commit 90e50575f55a12193e2b4dafee0a3a4bf01fd78c
Author: Howie Wang <hong...@wepay.com>
AuthorDate: Wed Apr 3 12:23:02 2024 -0700

    Disallow default header to be overwritten (#577)
    
    Co-authored-by: Hongyi Wang <hongyi_w...@apple.com>
---
 pyiceberg/catalog/rest.py  |  4 ++--
 tests/catalog/test_rest.py | 37 ++++++++++++++++++++++++++++---------
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/pyiceberg/catalog/rest.py b/pyiceberg/catalog/rest.py
index 9f0d0544..81a9b09f 100644
--- a/pyiceberg/catalog/rest.py
+++ b/pyiceberg/catalog/rest.py
@@ -480,12 +480,12 @@ class RestCatalog(Catalog):
             session.headers[AUTHORIZATION_HEADER] = f"{BEARER_PREFIX} {token}"
 
     def _config_headers(self, session: Session) -> None:
+        header_properties = self._extract_headers_from_properties()
+        session.headers.update(header_properties)
         session.headers["Content-type"] = "application/json"
         session.headers["X-Client-Version"] = ICEBERG_REST_SPEC_VERSION
         session.headers["User-Agent"] = f"PyIceberg/{__version__}"
         session.headers["X-Iceberg-Access-Delegation"] = "vended-credentials"
-        header_properties = self._extract_headers_from_properties()
-        session.headers.update(header_properties)
 
     def _extract_headers_from_properties(self) -> Dict[str, str]:
         return {key[len(HEADER_PREFIX) :]: value for key, value in 
self.properties.items() if key.startswith(HEADER_PREFIX)}
diff --git a/tests/catalog/test_rest.py b/tests/catalog/test_rest.py
index 4956fffe..15ddb01b 100644
--- a/tests/catalog/test_rest.py
+++ b/tests/catalog/test_rest.py
@@ -277,23 +277,35 @@ def test_properties_sets_headers(requests_mock: Mocker) 
-> None:
     )
 
     catalog = RestCatalog(
-        "rest", uri=TEST_URI, warehouse="s3://some-bucket", 
**{"header.Content-Type": "application/vnd.api+json"}
+        "rest",
+        uri=TEST_URI,
+        warehouse="s3://some-bucket",
+        **{"header.Content-Type": "application/vnd.api+json", 
"header.Customized-Header": "some/value"},
     )
 
     assert (
-        catalog._session.headers.get("Content-type") == 
"application/vnd.api+json"
-    ), "Expected 'Content-Type' header to be 'application/vnd.api+json'"
-
+        catalog._session.headers.get("Content-type") == "application/json"
+    ), "Expected 'Content-Type' default header not to be overwritten"
     assert (
-        requests_mock.last_request.headers["Content-type"] == 
"application/vnd.api+json"
+        requests_mock.last_request.headers["Content-type"] == 
"application/json"
     ), "Config request did not include expected 'Content-Type' header"
 
+    assert (
+        catalog._session.headers.get("Customized-Header") == "some/value"
+    ), "Expected 'Customized-Header' header to be 'some/value'"
+    assert (
+        requests_mock.last_request.headers["Customized-Header"] == "some/value"
+    ), "Config request did not include expected 'Customized-Header' header"
+
 
 def test_config_sets_headers(requests_mock: Mocker) -> None:
     namespace = "leden"
     requests_mock.get(
         f"{TEST_URI}v1/config",
-        json={"defaults": {"header.Content-Type": "application/vnd.api+json"}, 
"overrides": {}},
+        json={
+            "defaults": {"header.Content-Type": "application/vnd.api+json", 
"header.Customized-Header": "some/value"},
+            "overrides": {},
+        },
         status_code=200,
     )
     requests_mock.post(f"{TEST_URI}v1/namespaces", json={"namespace": 
[namespace], "properties": {}}, status_code=200)
@@ -301,12 +313,19 @@ def test_config_sets_headers(requests_mock: Mocker) -> 
None:
     catalog.create_namespace(namespace)
 
     assert (
-        catalog._session.headers.get("Content-type") == 
"application/vnd.api+json"
-    ), "Expected 'Content-Type' header to be 'application/vnd.api+json'"
+        catalog._session.headers.get("Content-type") == "application/json"
+    ), "Expected 'Content-Type' default header not to be overwritten"
     assert (
-        requests_mock.last_request.headers["Content-type"] == 
"application/vnd.api+json"
+        requests_mock.last_request.headers["Content-type"] == 
"application/json"
     ), "Create namespace request did not include expected 'Content-Type' 
header"
 
+    assert (
+        catalog._session.headers.get("Customized-Header") == "some/value"
+    ), "Expected 'Customized-Header' header to be 'some/value'"
+    assert (
+        requests_mock.last_request.headers["Customized-Header"] == "some/value"
+    ), "Create namespace request did not include expected 'Customized-Header' 
header"
+
 
 def test_token_400(rest_mock: Mocker) -> None:
     rest_mock.post(

Reply via email to