Skip to content

Commit

Permalink
ensure headers are canoncically formatted (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
johns31459 authored Sep 1, 2023
1 parent 36db5c2 commit 55934bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/crowdstrike/foundry/function/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def map_dict_to_dataclass(d: Dict, dc) -> [None, dataclass]:
return dc


def _canonize_header(h: str) -> str:
"""
Converts a header key into its canonical version.
:param h: Header key.
:return: Canonized version.
"""
canon = ''
upper = True
for c in h:
if upper:
canon += c.upper()
else:
canon += c.lower()

upper = c == '-'
return canon


@dataclass
class APIError:
"""
Expand Down Expand Up @@ -102,6 +120,11 @@ def from_json_payload(payload: str) -> 'Request':
req_dict = json.loads(payload)
req = map_dict_to_dataclass(req_dict, Request())
req.params = map_dict_to_dataclass(req_dict.get('params', None), Params())
if req.params.header is not None and len(req.params.header) > 0:
h = {}
for k, v in req.params.header.items():
h[_canonize_header(k)] = v
req.params.header = h
return req


Expand Down
6 changes: 5 additions & 1 deletion tests/crowdstrike/foundry/function/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def test_from_json_payload(self):
method='GET',
params=Params(
header={
'xyz': ['a', 'b'],
'Accepts': ['application/json'],
'Content-Type': ['application/json'],
'Xyz': ['a', 'b'],
},
query={
'ijk': ['4', '5', '6'],
Expand All @@ -56,6 +58,8 @@ def test_from_json_payload(self):
'method': 'GET',
'params': {
'header': {
'accepts': ['application/json'],
'ContENt-type': ['application/json'],
'xyz': ['a', 'b'],
},
'query': {
Expand Down

0 comments on commit 55934bd

Please sign in to comment.