Skip to content

Commit

Permalink
Support for bytes data in HTTP network report section.
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-verma committed Aug 31, 2021
1 parent 8bde174 commit 89487f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
11 changes: 11 additions & 0 deletions arjuna-samples/arjex/test/pkg/httpauto/check_http_03_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def check_multipart_file(request):
post2 = Http.service().post(url, content=Http.content.file('fname', "sample.txt", headers={'X-A': 'b'}))
assert post2.status_code == 200

post3 = Http.service().post(url, content=Http.content.file('fname', "black.png", headers={'X-A': 'b'}))
assert post2.status_code == 200

@test
def check_multipart_files_and_fields(request):
url = "http://httpbin.org/post"
Expand All @@ -47,4 +50,12 @@ def check_multipart_files_and_fields(request):
{'a': 7, 'b': 9},
Http.field('c', 'something')
))
assert r.status_code == 200

r = Http.service().post(url, content=Http.content.multipart(
{'a': 1, 'b': 3},
Http.field('fname', "black.png", is_file=True),
{'a': 7, 'b': 9},
Http.field('c', 'something')
))
assert r.status_code == 200
3 changes: 2 additions & 1 deletion arjuna/core/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def import_module(mod_name, *, prefix="", optional=False):
except ModuleNotFoundError:
if not optional:
raise
return mod
else:
return mod

def import_name_in_module(*, mod_name, name, prefix="", optional=False):
mod = import_module(mod_name, prefix=prefix, optional=optional)
Expand Down
11 changes: 7 additions & 4 deletions arjuna/tpi/httpauto/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def content(self):
@classmethod
def repr_as_str(cls, *, method, url, headers, content=None):
if content:
if isinstance(content, bytes):
content = content.decode('utf-8', 'backslashreplace')
content = '\n\n{}\n'.format(content)
else:
content = ""
Expand All @@ -147,7 +149,7 @@ def repr_as_str(cls, *, method, url, headers, content=None):
def __str__(self):
content = self.content
if isinstance(content, bytes):
content = content.decode()
content = content.decode('utf-8', 'backslashreplace')
return self.repr_as_str(
method = self.__request.method,
url = self.url,
Expand Down Expand Up @@ -225,9 +227,10 @@ def __build_request(self):
try:
if self.__content:
data = self.__content.content
named_matches = re.findall(_NAMED_PATTERN, data)
if named_matches:
raise HttpRequestCreationError("{} : {}".format(req_repr, "Found pending placeholders in request content: {}. Fix formatting logic to correctly send this request.".format(named_matches)))
if type(data) is not bytes:
named_matches = re.findall(_NAMED_PATTERN, data)
if named_matches:
raise HttpRequestCreationError("{} : {}".format(req_repr, "Found pending placeholders in request content: {}. Fix formatting logic to correctly send this request.".format(named_matches)))
else:
data = None

Expand Down

0 comments on commit 89487f6

Please sign in to comment.