Skip to content

Commit 4cdad74

Browse files
committed
Merge branch 'improved-wrapping-main' into update-wrapping-to-wrapt-for-sinks
2 parents 20f3b8e + b102ba4 commit 4cdad74

8 files changed

Lines changed: 303 additions & 3 deletions

File tree

aikido_zen/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def protect(mode="daemon"):
4747
import aikido_zen.sources.flask
4848
import aikido_zen.sources.quart
4949
import aikido_zen.sources.starlette
50-
import aikido_zen.sources.xml
51-
import aikido_zen.sources.lxml
50+
import aikido_zen.sources.xml_sources.xml
51+
import aikido_zen.sources.xml_sources.lxml
5252

5353
import aikido_zen.sources.gunicorn
5454
import aikido_zen.sources.uwsgi

aikido_zen/sources/xml_sources/__init__.py

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from aikido_zen.context import Context, get_current_context
2+
import aikido_zen.sources.xml_sources.lxml
3+
import lxml.etree as ET
4+
5+
# Sample XML string for testing
6+
XML_STRING = """<?xml version="1.0"?>
7+
<root>
8+
<child attr="chill" test="test1" smth="2" smthelse="2">
9+
<name smth="2" test="test2">Test Name</name>
10+
<value ok="boomer">42</value>
11+
</child>
12+
</root>
13+
"""
14+
15+
16+
def parse_xml(xml_string):
17+
"""Parse the given XML string and return the root element."""
18+
try:
19+
root = ET.fromstring(xml_string)
20+
return root
21+
except ET.ParseError as e:
22+
raise ValueError(f"Failed to parse XML: {e}")
23+
24+
25+
def set_context(body):
26+
Context(
27+
req={
28+
"REQUEST_METHOD": "GET",
29+
"HTTP_HEADER_1": "header 1 value",
30+
"HTTP_HEADER_2": "Header 2 value",
31+
"RANDOM_VALUE": "Random value",
32+
"HTTP_COOKIE": "sessionId=abc123xyz456;",
33+
"wsgi.url_scheme": "http",
34+
"HTTP_HOST": "localhost:8080",
35+
"PATH_INFO": "/hello",
36+
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
37+
"CONTENT_TYPE": "application/json",
38+
"REMOTE_ADDR": "198.51.100.23",
39+
"HTTP_USER_AGENT": "Mozilla/5.0",
40+
},
41+
body=body,
42+
source="django",
43+
).set_as_current_context()
44+
45+
46+
def test_parse_xml_with_random_context():
47+
"""Test the XML parsing function."""
48+
set_context("Body falalalala")
49+
root = parse_xml(XML_STRING)
50+
51+
# Check XML parser still works
52+
assert root[0].find("name").text == "Test Name"
53+
54+
# Check that no xml attribute was set on body
55+
context = get_current_context()
56+
assert context.xml == {}
57+
58+
59+
def test_parse_xml_with_set_context():
60+
"""Test the XML parsing function."""
61+
set_context(body=XML_STRING)
62+
root = parse_xml(XML_STRING)
63+
64+
# Check XML parser still works
65+
assert root[0].find("name").text == "Test Name"
66+
67+
# Check that no xml attribute was set on body
68+
context = get_current_context()
69+
assert context.xml == {
70+
"attr": {"chill"},
71+
"smth": {"2"},
72+
"smthelse": {"2"},
73+
"test": {"test1"},
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import aikido_zen.sources.xml_sources.xml
2+
from aikido_zen.context import Context, get_current_context
3+
4+
import pytest
5+
import xml.etree.ElementTree as ET
6+
7+
# Sample XML string for testing
8+
XML_STRING = """<?xml version="1.0"?>
9+
<root>
10+
<child attr="chill" test="test1" smth="2" smthelse="2">
11+
<name smth="2" test="test2">Test Name</name>
12+
<value ok="boomer">42</value>
13+
</child>
14+
</root>
15+
"""
16+
17+
18+
def parse_xml(xml_string):
19+
"""Parse the given XML string and return the root element."""
20+
try:
21+
root = ET.fromstring(xml_string)
22+
return root
23+
except ET.ParseError as e:
24+
raise ValueError(f"Failed to parse XML: {e}")
25+
26+
27+
def set_context(body):
28+
Context(
29+
req={
30+
"REQUEST_METHOD": "GET",
31+
"HTTP_HEADER_1": "header 1 value",
32+
"HTTP_HEADER_2": "Header 2 value",
33+
"RANDOM_VALUE": "Random value",
34+
"HTTP_COOKIE": "sessionId=abc123xyz456;",
35+
"wsgi.url_scheme": "http",
36+
"HTTP_HOST": "localhost:8080",
37+
"PATH_INFO": "/hello",
38+
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
39+
"CONTENT_TYPE": "application/json",
40+
"REMOTE_ADDR": "198.51.100.23",
41+
"HTTP_USER_AGENT": "Mozilla/5.0",
42+
},
43+
body=body,
44+
source="django",
45+
).set_as_current_context()
46+
47+
48+
def test_parse_xml_with_random_context():
49+
"""Test the XML parsing function."""
50+
set_context("Body falalalala")
51+
root = parse_xml(XML_STRING)
52+
53+
# Check XML parser still works
54+
assert root[0].find("name").text == "Test Name"
55+
56+
# Check that no xml attribute was set on body
57+
context = get_current_context()
58+
assert context.xml == {}
59+
60+
61+
def test_parse_xml_with_set_context():
62+
"""Test the XML parsing function."""
63+
set_context(body=XML_STRING)
64+
root = parse_xml(XML_STRING)
65+
66+
# Check XML parser still works
67+
assert root[0].find("name").text == "Test Name"
68+
69+
# Check that no xml attribute was set on body
70+
context = get_current_context()
71+
assert context.xml == {
72+
"attr": {"chill"},
73+
"smth": {"2"},
74+
"smthelse": {"2"},
75+
"test": {"test1"},
76+
}

0 commit comments

Comments
 (0)