Skip to content

Commit b5047f9

Browse files
committed
Update api discovery logic to use new Headers object
1 parent b9757ab commit b5047f9

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

aikido_zen/api_discovery/get_api_info_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from .get_api_info import get_api_info
3+
from ..helpers.headers import Headers
34

45

56
class Context:
@@ -16,7 +17,8 @@ def __init__(
1617
self.path = path
1718
self.body = body
1819
self.xml = xml
19-
self.headers = {"CONTENT_TYPE": content_type}
20+
self.headers = Headers()
21+
self.headers.store_header("CONTENT_TYPE", content_type)
2022
self.query = query
2123
self.cookies = {}
2224

@@ -154,7 +156,7 @@ def test_auth_get_api_info(monkeypatch):
154156
},
155157
content_type="application/json",
156158
)
157-
context1.headers["AUTHORIZATION"] = "Bearer token"
159+
context1.headers.store_header("AUTHORIZATION", "Bearer token")
158160
api_info = get_api_info(context1)
159161
assert api_info == {
160162
"body": {

aikido_zen/api_discovery/get_auth_type_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import pytest
22
from .get_auth_types import get_auth_types
3+
from ..helpers.headers import Headers
34

45

56
class Context:
67
def __init__(self, headers={}, cookies={}):
7-
self.headers = headers
8+
self.headers = Headers()
9+
for k, v in headers.items():
10+
self.headers.store_header(k, v)
811
self.cookies = cookies
912

1013

aikido_zen/api_discovery/get_auth_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Usefull export : get_auth_types"""
22

3+
from aikido_zen.helpers.headers import Headers
34
from aikido_zen.helpers.is_http_auth_scheme import is_http_auth_scheme
45

56
common_api_key_header_names = [
@@ -26,13 +27,13 @@
2627

2728
def get_auth_types(context):
2829
"""Get the authentication type of the API request."""
29-
if not isinstance(context.headers, dict):
30+
if not isinstance(context.headers, Headers):
3031
return None
3132

3233
result = []
3334

3435
# Check the Authorization header
35-
auth_header = context.headers.get("AUTHORIZATION")
36+
auth_header = context.headers.get_header("AUTHORIZATION")
3637
if isinstance(auth_header, str):
3738
auth_header_type = get_authorization_header_type(auth_header)
3839
if auth_header_type:

aikido_zen/api_discovery/get_body_data_type.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Exports get_body_data_type"""
22

3+
from aikido_zen.helpers.headers import Headers
4+
35
JSON_CONTENT_TYPES = [
46
"application/json",
57
"application/vnd.api+json",
@@ -8,12 +10,12 @@
810
]
911

1012

11-
def get_body_data_type(headers):
13+
def get_body_data_type(headers: Headers):
1214
"""Gets the type of body data from headers"""
13-
if not isinstance(headers, dict) or headers is None:
15+
if not isinstance(headers, Headers) or headers is None:
1416
return
1517

16-
content_type = headers.get("CONTENT_TYPE")
18+
content_type = headers.get_header("CONTENT_TYPE")
1719
if not content_type:
1820
return
1921

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
import pytest
22
from .get_body_data_type import get_body_data_type
3+
from ..helpers.headers import Headers
34

45

56
def test_get_body_data_type():
6-
assert get_body_data_type({"CONTENT_TYPE": "application/json"}) == "json"
7-
assert get_body_data_type({"CONTENT_TYPE": "application/vnd.api+json"}) == "json"
8-
assert get_body_data_type({"CONTENT_TYPE": "application/csp-report"}) == "json"
9-
assert get_body_data_type({"CONTENT_TYPE": "application/x-json"}) == "json"
10-
assert (
11-
get_body_data_type({"CONTENT_TYPE": "application/x-www-form-urlencoded"})
12-
== "form-urlencoded"
13-
)
14-
assert get_body_data_type({"CONTENT_TYPE": "multipart/form-data"}) == "form-data"
15-
assert get_body_data_type({"CONTENT_TYPE": "text/xml"}) == "xml"
16-
assert get_body_data_type({"CONTENT_TYPE": "text/html"}) is None
17-
assert (
18-
get_body_data_type({"CONTENT_TYPE": ["application/json", "text/html"]})
19-
== "json"
20-
)
21-
assert get_body_data_type({"x-test": "abc"}) is None
22-
assert get_body_data_type(None) is None # Testing invalid input
23-
assert get_body_data_type({}) is None
7+
headers = Headers()
8+
headers.store_header("CONTENT_TYPE", "application/json")
9+
assert get_body_data_type(headers) == "json"
10+
11+
headers = Headers()
12+
headers.store_header("CONTENT_TYPE", "application/vnd.api+json")
13+
assert get_body_data_type(headers) == "json"
14+
15+
headers = Headers()
16+
headers.store_header("CONTENT_TYPE", "application/csp-report")
17+
assert get_body_data_type(headers) == "json"
18+
19+
headers = Headers()
20+
headers.store_header("CONTENT_TYPE", "application/x-json")
21+
assert get_body_data_type(headers) == "json"
22+
23+
headers = Headers()
24+
headers.store_header("CONTENT_TYPE", "application/x-www-form-urlencoded")
25+
assert get_body_data_type(headers) == "form-urlencoded"
26+
27+
headers = Headers()
28+
headers.store_header("CONTENT_TYPE", "multipart/form-data")
29+
assert get_body_data_type(headers) == "form-data"
30+
31+
headers = Headers()
32+
headers.store_header("CONTENT_TYPE", "text/xml")
33+
assert get_body_data_type(headers) == "xml"
34+
35+
headers = Headers()
36+
headers.store_header("CONTENT_TYPE", "text/html")
37+
assert get_body_data_type(headers) is None
38+
39+
headers = Headers()
40+
headers.store_header("CONTENT_TYPE", "application/json, text/html")
41+
assert get_body_data_type(headers) == "json"
42+
43+
headers = Headers()
44+
headers.store_header("x-test", "abc")
45+
assert get_body_data_type(headers) is None
46+
47+
headers = Headers()
48+
assert get_body_data_type(headers) is None # Testing invalid input
49+
50+
headers = Headers()
51+
assert get_body_data_type(headers) is None

0 commit comments

Comments
 (0)