Skip to content

Commit 83a1864

Browse files
committed
[http-query] fix: add method to parse headers and key/value data (#92)
1 parent 6fc5ec7 commit 83a1864

File tree

5 files changed

+86
-26
lines changed

5 files changed

+86
-26
lines changed

http-query/src/helpers/__init__.py

Whitespace-only changes.

http-query/src/helpers/helpers.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class HTTPHelpers:
2+
@staticmethod
3+
def parse_headers(headers_str):
4+
if isinstance(headers_str, list):
5+
return headers_str
6+
headers_list = []
7+
for kv in headers_str.split(","):
8+
if "=" in kv:
9+
k, v = kv.split("=", 1)
10+
headers_list.append({"key": k.strip(), "value": v.strip()})
11+
return headers_list
12+
13+
@staticmethod
14+
def parse_parts(parts_str):
15+
if isinstance(parts_str, list):
16+
return parts_str
17+
parts_list = []
18+
for kv in parts_str.split("&"):
19+
if "=" in kv:
20+
k, v = kv.split("=", 1)
21+
parts_list.append({"key": k.strip(), "value": v.strip()})
22+
return parts_list
23+
24+
@staticmethod
25+
def request_data_parts_body(request_data):
26+
parts = HTTPHelpers.parse_parts(
27+
request_data["injection"]["inject_content"]["parts"]
28+
)
29+
keys = list(map(lambda p: p["key"], parts))
30+
values = list(map(lambda p: p["value"], parts))
31+
return dict(zip(keys, values))
32+
33+
@staticmethod
34+
def response_parsing(response):
35+
success = 200 <= response.status_code < 300
36+
success_status = "SUCCESS" if success else "ERROR"
37+
return {
38+
"url": response.url,
39+
"code": response.status_code,
40+
"status": success_status,
41+
"message": response.text,
42+
}

http-query/src/openaev_http.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
HTTP_RAW_PUT_CONTRACT,
1212
HttpContracts,
1313
)
14+
from helpers.helpers import HTTPHelpers
1415
from pyoaev.helpers import OpenAEVConfigHelper, OpenAEVInjectorHelper
1516

1617

@@ -43,24 +44,6 @@ def __init__(self):
4344
self.config, open("img/icon-http.png", "rb")
4445
)
4546

46-
@staticmethod
47-
def _request_data_parts_body(request_data):
48-
parts = request_data["injection"]["inject_content"]["parts"]
49-
keys = list(map(lambda p: p["key"], parts))
50-
values = list(map(lambda p: p["value"], parts))
51-
return dict(zip(keys, values))
52-
53-
@staticmethod
54-
def _response_parsing(response):
55-
success = 200 <= response.status_code < 300
56-
success_status = "SUCCESS" if success else "ERROR"
57-
return {
58-
"url": response.url,
59-
"code": response.status_code,
60-
"status": success_status,
61-
"message": response.text,
62-
}
63-
6447
def attachments_to_files(self, request_data):
6548
documents = request_data["injection"].get("inject_documents", [])
6649
attachments = list(filter(lambda d: d["document_attached"] is True, documents))
@@ -73,7 +56,9 @@ def attachments_to_files(self, request_data):
7356

7457
def http_execution(self, data: Dict):
7558
# Build headers
76-
inject_headers = data["injection"]["inject_content"].get("headers", [])
59+
inject_headers = HTTPHelpers.parse_headers(
60+
data["injection"]["inject_content"].get("headers", [])
61+
)
7762
headers = {}
7863
for header_definition in inject_headers:
7964
headers[header_definition["key"]] = header_definition["value"]
@@ -93,35 +78,35 @@ def http_execution(self, data: Dict):
9378
# Get
9479
if inject_contract == HTTP_GET_CONTRACT:
9580
response = session.get(url=url, headers=headers)
96-
return self._response_parsing(response)
81+
return HTTPHelpers.response_parsing(response)
9782
# Post
9883
if inject_contract == HTTP_RAW_POST_CONTRACT:
9984
body = data["injection"]["inject_content"]["body"]
10085
response = session.post(
10186
url=url, headers=headers, data=body, files=http_files
10287
)
103-
return self._response_parsing(response)
88+
return HTTPHelpers.response_parsing(response)
10489
# Put
10590
if inject_contract == HTTP_RAW_PUT_CONTRACT:
10691
body = data["injection"]["inject_content"]["body"]
10792
response = session.put(
10893
url=url, headers=headers, data=body, files=http_files
10994
)
110-
return self._response_parsing(response)
95+
return HTTPHelpers.response_parsing(response)
11196
# Form Post
11297
if inject_contract == HTTP_FORM_POST_CONTRACT:
113-
body = self._request_data_parts_body(data)
98+
body = HTTPHelpers.request_data_parts_body(data)
11499
response = session.post(
115100
url=url, headers=headers, data=body, files=http_files
116101
)
117-
return self._response_parsing(response)
102+
return HTTPHelpers.response_parsing(response)
118103
# Form Put
119104
if inject_contract == HTTP_FORM_PUT_CONTRACT:
120-
body = self._request_data_parts_body(data)
105+
body = HTTPHelpers.request_data_parts_body(data)
121106
response = session.put(
122107
url=url, headers=headers, data=body, files=http_files
123108
)
124-
return self._response_parsing(response)
109+
return HTTPHelpers.response_parsing(response)
125110
# Nothing supported
126111
return {
127112
"code": 400,

http-query/test/helpers/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from unittest import TestCase
2+
3+
from src.helpers.helpers import HTTPHelpers
4+
5+
6+
class HTTPHelpersTest(TestCase):
7+
def test_parse_headers_with_string(self):
8+
input_str = (
9+
"Content-Type=application/x-www-form-urlencoded,Accept=application/json"
10+
)
11+
expected = [
12+
{"key": "Content-Type", "value": "application/x-www-form-urlencoded"},
13+
{"key": "Accept", "value": "application/json"},
14+
]
15+
result = HTTPHelpers.parse_headers(input_str)
16+
self.assertEqual(result, expected)
17+
18+
19+
def test_parse_parts_with_string(self):
20+
input_str = "msg=test&user=alice"
21+
expected = [{"key": "msg", "value": "test"}, {"key": "user", "value": "savacano"}]
22+
result = HTTPHelpers.parse_parts(input_str)
23+
self.assertEqual(result, expected)
24+
25+
26+
def test_parse_headers_empty_string(self):
27+
result = HTTPHelpers.parse_headers("")
28+
self.assertEqual(result, [])
29+
30+
31+
def test_parse_parts_empty_string(self):
32+
result = HTTPHelpers.parse_parts("")
33+
self.assertEqual(result, [])

0 commit comments

Comments
 (0)