diff --git a/arjuna-samples/arjex/test/pkg/httpauto/check_http_03_content.py b/arjuna-samples/arjex/test/pkg/httpauto/check_http_03_content.py index ff8003a0..28b2a8fa 100644 --- a/arjuna-samples/arjex/test/pkg/httpauto/check_http_03_content.py +++ b/arjuna-samples/arjex/test/pkg/httpauto/check_http_03_content.py @@ -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" @@ -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 \ No newline at end of file diff --git a/arjuna/core/importer.py b/arjuna/core/importer.py index 1e3326bc..6f793a8a 100644 --- a/arjuna/core/importer.py +++ b/arjuna/core/importer.py @@ -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) diff --git a/arjuna/tpi/httpauto/request.py b/arjuna/tpi/httpauto/request.py index 09685c09..30820c8b 100644 --- a/arjuna/tpi/httpauto/request.py +++ b/arjuna/tpi/httpauto/request.py @@ -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 = "" @@ -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, @@ -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