Skip to content

Commit 89487f6

Browse files
committed
Support for bytes data in HTTP network report section.
1 parent 8bde174 commit 89487f6

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

arjuna-samples/arjex/test/pkg/httpauto/check_http_03_content.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def check_multipart_file(request):
3737
post2 = Http.service().post(url, content=Http.content.file('fname', "sample.txt", headers={'X-A': 'b'}))
3838
assert post2.status_code == 200
3939

40+
post3 = Http.service().post(url, content=Http.content.file('fname', "black.png", headers={'X-A': 'b'}))
41+
assert post2.status_code == 200
42+
4043
@test
4144
def check_multipart_files_and_fields(request):
4245
url = "http://httpbin.org/post"
@@ -47,4 +50,12 @@ def check_multipart_files_and_fields(request):
4750
{'a': 7, 'b': 9},
4851
Http.field('c', 'something')
4952
))
53+
assert r.status_code == 200
54+
55+
r = Http.service().post(url, content=Http.content.multipart(
56+
{'a': 1, 'b': 3},
57+
Http.field('fname', "black.png", is_file=True),
58+
{'a': 7, 'b': 9},
59+
Http.field('c', 'something')
60+
))
5061
assert r.status_code == 200

arjuna/core/importer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def import_module(mod_name, *, prefix="", optional=False):
2626
except ModuleNotFoundError:
2727
if not optional:
2828
raise
29-
return mod
29+
else:
30+
return mod
3031

3132
def import_name_in_module(*, mod_name, name, prefix="", optional=False):
3233
mod = import_module(mod_name, prefix=prefix, optional=optional)

arjuna/tpi/httpauto/request.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def content(self):
135135
@classmethod
136136
def repr_as_str(cls, *, method, url, headers, content=None):
137137
if content:
138+
if isinstance(content, bytes):
139+
content = content.decode('utf-8', 'backslashreplace')
138140
content = '\n\n{}\n'.format(content)
139141
else:
140142
content = ""
@@ -147,7 +149,7 @@ def repr_as_str(cls, *, method, url, headers, content=None):
147149
def __str__(self):
148150
content = self.content
149151
if isinstance(content, bytes):
150-
content = content.decode()
152+
content = content.decode('utf-8', 'backslashreplace')
151153
return self.repr_as_str(
152154
method = self.__request.method,
153155
url = self.url,
@@ -225,9 +227,10 @@ def __build_request(self):
225227
try:
226228
if self.__content:
227229
data = self.__content.content
228-
named_matches = re.findall(_NAMED_PATTERN, data)
229-
if named_matches:
230-
raise HttpRequestCreationError("{} : {}".format(req_repr, "Found pending placeholders in request content: {}. Fix formatting logic to correctly send this request.".format(named_matches)))
230+
if type(data) is not bytes:
231+
named_matches = re.findall(_NAMED_PATTERN, data)
232+
if named_matches:
233+
raise HttpRequestCreationError("{} : {}".format(req_repr, "Found pending placeholders in request content: {}. Fix formatting logic to correctly send this request.".format(named_matches)))
231234
else:
232235
data = None
233236

0 commit comments

Comments
 (0)