Skip to content

Commit 7fdc164

Browse files
Accept mapping header inputs in client configuration
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 01caa6a commit 7fdc164

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

hyperbrowser/config.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Dict, Optional
2+
from typing import Dict, Mapping, Optional
33
import os
44

55
from .exceptions import HyperbrowserError
@@ -18,8 +18,8 @@ def __post_init__(self) -> None:
1818
raise HyperbrowserError("api_key must be a string")
1919
if not isinstance(self.base_url, str):
2020
raise HyperbrowserError("base_url must be a string")
21-
if self.headers is not None and not isinstance(self.headers, dict):
22-
raise HyperbrowserError("headers must be a dictionary of string pairs")
21+
if self.headers is not None and not isinstance(self.headers, Mapping):
22+
raise HyperbrowserError("headers must be a mapping of string pairs")
2323
self.api_key = self.api_key.strip()
2424
if not self.api_key:
2525
raise HyperbrowserError("api_key must not be empty")
@@ -34,9 +34,7 @@ def __post_init__(self) -> None:
3434
normalized_headers: Dict[str, str] = {}
3535
for key, value in self.headers.items():
3636
if not isinstance(key, str) or not isinstance(value, str):
37-
raise HyperbrowserError(
38-
"headers must be a dictionary of string pairs"
39-
)
37+
raise HyperbrowserError("headers must be a mapping of string pairs")
4038
normalized_headers[key] = value
4139
self.headers = normalized_headers
4240

tests/test_config.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from types import MappingProxyType
2+
13
import pytest
24

35
from hyperbrowser.config import ClientConfig
@@ -61,7 +63,7 @@ def test_client_config_rejects_non_string_values():
6163
with pytest.raises(HyperbrowserError, match="base_url must be a string"):
6264
ClientConfig(api_key="test-key", base_url=None) # type: ignore[arg-type]
6365

64-
with pytest.raises(HyperbrowserError, match="headers must be a dictionary"):
66+
with pytest.raises(HyperbrowserError, match="headers must be a mapping"):
6567
ClientConfig(api_key="test-key", headers="x=1") # type: ignore[arg-type]
6668

6769
with pytest.raises(HyperbrowserError, match="api_key must not be empty"):
@@ -86,5 +88,12 @@ def test_client_config_normalizes_headers_to_internal_copy():
8688

8789

8890
def test_client_config_rejects_non_string_header_pairs():
89-
with pytest.raises(HyperbrowserError, match="headers must be a dictionary"):
91+
with pytest.raises(HyperbrowserError, match="headers must be a mapping"):
9092
ClientConfig(api_key="test-key", headers={"X-Correlation-Id": 123}) # type: ignore[dict-item]
93+
94+
95+
def test_client_config_accepts_mapping_header_inputs():
96+
headers = MappingProxyType({"X-Correlation-Id": "abc123"})
97+
config = ClientConfig(api_key="test-key", headers=headers)
98+
99+
assert config.headers == {"X-Correlation-Id": "abc123"}

0 commit comments

Comments
 (0)