Skip to content

Commit 90e5057

Browse files
whynick1Hongyi Wang
andauthored
Disallow default header to be overwritten (#577)
Co-authored-by: Hongyi Wang <[email protected]>
1 parent ba9ff98 commit 90e5057

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

pyiceberg/catalog/rest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,12 @@ def _refresh_token(self, session: Optional[Session] = None, initial_token: Optio
480480
session.headers[AUTHORIZATION_HEADER] = f"{BEARER_PREFIX} {token}"
481481

482482
def _config_headers(self, session: Session) -> None:
483+
header_properties = self._extract_headers_from_properties()
484+
session.headers.update(header_properties)
483485
session.headers["Content-type"] = "application/json"
484486
session.headers["X-Client-Version"] = ICEBERG_REST_SPEC_VERSION
485487
session.headers["User-Agent"] = f"PyIceberg/{__version__}"
486488
session.headers["X-Iceberg-Access-Delegation"] = "vended-credentials"
487-
header_properties = self._extract_headers_from_properties()
488-
session.headers.update(header_properties)
489489

490490
def _extract_headers_from_properties(self) -> Dict[str, str]:
491491
return {key[len(HEADER_PREFIX) :]: value for key, value in self.properties.items() if key.startswith(HEADER_PREFIX)}

tests/catalog/test_rest.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,36 +277,55 @@ def test_properties_sets_headers(requests_mock: Mocker) -> None:
277277
)
278278

279279
catalog = RestCatalog(
280-
"rest", uri=TEST_URI, warehouse="s3://some-bucket", **{"header.Content-Type": "application/vnd.api+json"}
280+
"rest",
281+
uri=TEST_URI,
282+
warehouse="s3://some-bucket",
283+
**{"header.Content-Type": "application/vnd.api+json", "header.Customized-Header": "some/value"},
281284
)
282285

283286
assert (
284-
catalog._session.headers.get("Content-type") == "application/vnd.api+json"
285-
), "Expected 'Content-Type' header to be 'application/vnd.api+json'"
286-
287+
catalog._session.headers.get("Content-type") == "application/json"
288+
), "Expected 'Content-Type' default header not to be overwritten"
287289
assert (
288-
requests_mock.last_request.headers["Content-type"] == "application/vnd.api+json"
290+
requests_mock.last_request.headers["Content-type"] == "application/json"
289291
), "Config request did not include expected 'Content-Type' header"
290292

293+
assert (
294+
catalog._session.headers.get("Customized-Header") == "some/value"
295+
), "Expected 'Customized-Header' header to be 'some/value'"
296+
assert (
297+
requests_mock.last_request.headers["Customized-Header"] == "some/value"
298+
), "Config request did not include expected 'Customized-Header' header"
299+
291300

292301
def test_config_sets_headers(requests_mock: Mocker) -> None:
293302
namespace = "leden"
294303
requests_mock.get(
295304
f"{TEST_URI}v1/config",
296-
json={"defaults": {"header.Content-Type": "application/vnd.api+json"}, "overrides": {}},
305+
json={
306+
"defaults": {"header.Content-Type": "application/vnd.api+json", "header.Customized-Header": "some/value"},
307+
"overrides": {},
308+
},
297309
status_code=200,
298310
)
299311
requests_mock.post(f"{TEST_URI}v1/namespaces", json={"namespace": [namespace], "properties": {}}, status_code=200)
300312
catalog = RestCatalog("rest", uri=TEST_URI, warehouse="s3://some-bucket")
301313
catalog.create_namespace(namespace)
302314

303315
assert (
304-
catalog._session.headers.get("Content-type") == "application/vnd.api+json"
305-
), "Expected 'Content-Type' header to be 'application/vnd.api+json'"
316+
catalog._session.headers.get("Content-type") == "application/json"
317+
), "Expected 'Content-Type' default header not to be overwritten"
306318
assert (
307-
requests_mock.last_request.headers["Content-type"] == "application/vnd.api+json"
319+
requests_mock.last_request.headers["Content-type"] == "application/json"
308320
), "Create namespace request did not include expected 'Content-Type' header"
309321

322+
assert (
323+
catalog._session.headers.get("Customized-Header") == "some/value"
324+
), "Expected 'Customized-Header' header to be 'some/value'"
325+
assert (
326+
requests_mock.last_request.headers["Customized-Header"] == "some/value"
327+
), "Create namespace request did not include expected 'Customized-Header' header"
328+
310329

311330
def test_token_400(rest_mock: Mocker) -> None:
312331
rest_mock.post(

0 commit comments

Comments
 (0)