Skip to content

Commit ae1bd44

Browse files
committed
Adjust transform classes for bytes based protocol
For the protocol implementation bytes are passed in the form of Responses to the transform callables. Therefore adjust the Transform classes to handle bytes now.
1 parent 2bc27a2 commit ae1bd44

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

gvm/transforms.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"""
88
from lxml import etree
99

10+
from gvm.protocols.core import Response
11+
1012
from .errors import GvmError, GvmResponseError, GvmServerError
1113
from .xml import Element, create_parser
1214

@@ -19,10 +21,10 @@ class EtreeTransform:
1921
def __init__(self):
2022
self._parser = create_parser()
2123

22-
def _convert_response(self, response: str) -> Element:
23-
return etree.XML(response, parser=self._parser)
24+
def _convert_response(self, response: Response) -> Element:
25+
return etree.XML(bytes(response), parser=self._parser)
2426

25-
def __call__(self, response: str) -> Element:
27+
def __call__(self, response: Response) -> Element:
2628
return self._convert_response(response)
2729

2830

@@ -46,7 +48,7 @@ class CheckCommandTransform(EtreeTransform):
4648
response was an error response
4749
"""
4850

49-
def __call__(self, response: str) -> str: # type: ignore[override]
51+
def __call__(self, response: Response) -> Response: # type: ignore[override]
5052
root = self._convert_response(response)
5153

5254
check_command_status(root)
@@ -60,7 +62,7 @@ class EtreeCheckCommandTransform(EtreeTransform):
6062
response was an error response
6163
"""
6264

63-
def __call__(self, response: str) -> Element:
65+
def __call__(self, response: Response) -> Element:
6466
root = self._convert_response(response)
6567

6668
check_command_status(root)

tests/transforms/test_check_command_transform.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_no_status_transform(self):
1616
transform = CheckCommandTransform()
1717

1818
with self.assertRaises(GvmError):
19-
transform("<foo/>")
19+
transform(b"<foo/>")
2020

2121
def test_no_success_300status_transform(self):
2222
transform = CheckCommandTransform()
@@ -25,7 +25,7 @@ def test_no_success_300status_transform(self):
2525
root.set("status", "300")
2626
root.set("status_text", "Foo error")
2727

28-
response = etree.tostring(root).decode("utf-8")
28+
response = etree.tostring(root)
2929

3030
with self.assertRaises(GvmError):
3131
transform(response)
@@ -37,7 +37,7 @@ def test_no_success_400status_transform(self):
3737
root.set("status", "400")
3838
root.set("status_text", "Foo error")
3939

40-
response = etree.tostring(root).decode("utf-8")
40+
response = etree.tostring(root)
4141

4242
with self.assertRaises(GvmResponseError):
4343
transform(response)
@@ -49,7 +49,7 @@ def test_no_success_500status_transform(self):
4949
root.set("status", "500")
5050
root.set("status_text", "Foo error")
5151

52-
response = etree.tostring(root).decode("utf-8")
52+
response = etree.tostring(root)
5353

5454
with self.assertRaises(GvmServerError):
5555
transform(response)
@@ -60,6 +60,6 @@ def test_success_response(self):
6060
root = etree.Element("foo_response")
6161
root.set("status", "200")
6262

63-
response = etree.tostring(root).decode("utf-8")
63+
response = etree.tostring(root)
6464

65-
self.assertEqual(transform(response), '<foo_response status="200"/>')
65+
self.assertEqual(transform(response), b'<foo_response status="200"/>')

tests/transforms/test_etree_check_command_transform.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_no_status_transform(self):
1616
transform = EtreeCheckCommandTransform()
1717

1818
with self.assertRaises(GvmError):
19-
transform("<foo/>")
19+
transform(b"<foo/>")
2020

2121
def test_no_success_status_transform(self):
2222
transform = EtreeCheckCommandTransform()
@@ -25,7 +25,7 @@ def test_no_success_status_transform(self):
2525
root.set("status", "400")
2626
root.set("status_text", "Foo error")
2727

28-
response = etree.tostring(root).decode("utf-8")
28+
response = etree.tostring(root)
2929

3030
with self.assertRaises(GvmError):
3131
transform(response)
@@ -36,7 +36,7 @@ def test_success_response(self):
3636
root = etree.Element("foo_response")
3737
root.set("status", "200")
3838

39-
response = etree.tostring(root).decode("utf-8")
39+
response = etree.tostring(root)
4040

4141
result = transform(response)
4242

tests/transforms/test_etree_transform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
class EtreeTransformTestCase(unittest.TestCase):
1414
def test_transform_response(self):
1515
transform = EtreeTransform()
16-
result = transform("<foo/>")
16+
result = transform(b"<foo/>")
1717

1818
self.assertTrue(etree.iselement(result))
1919

2020
def test_transform_more_complex_response(self):
2121
transform = EtreeTransform()
22-
result = transform('<foo id="bar"><lorem/><ipsum/></foo>')
22+
result = transform(b'<foo id="bar"><lorem/><ipsum/></foo>')
2323

2424
self.assertTrue(etree.iselement(result))
2525
self.assertEqual(result.tag, "foo")

0 commit comments

Comments
 (0)