def test_encode_multipart_data_files_using_boundary_as_init_parameter( self): files = {"file_upload": "tests/resources/file.pdf"} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer("custom_boundary") body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=custom_boundary") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=custom_boundary") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body)
def test_encode_multipart_data_files_none(self): files = {"file_upload": "tests/resources/file.pdf"} data = {"param1": "value1", "param2": None} r = MultiPartRenderer() body, content_type = r.encode_params(data, files, boundary="custom_boundary") self.assertEqual(content_type, "multipart/form-data; boundary=custom_boundary") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"null", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) body, content_type = r.encode_params(data, files) self.assertEqual( content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"null", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body)
def test_encode_multipart_data_and_no_files(self): files = {"file_upload": "tests/resources/file.pdf"} r = MultiPartRenderer() body, content_type = r.encode_params(None, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body)
def test_encode_multipart_data_and_no_files(self): files = {"file_upload": "tests/resources/file.pdf"} r = MultiPartRenderer() body, content_type = r.encode_params(None, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body)
def test_encode_multipart_no_data_and_files(self): data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, None) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body)
def test_encode_multipart_no_data_and_files(self): data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, None) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body)
def test_encode_multipart_data_files_none_csharp(self): files = {"file_upload": "tests/resources/file.pdf"} data = {"param1": "value1", "param2": None} r = MultiPartRenderer() body, content_type = r.encode_params(data, files, boundary="custom_boundary", output_str="csharp") self.assertEqual(content_type, "multipart/form-data; boundary=custom_boundary") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"Null", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body)
def test_encode_multipart_data_files_as_3tuple_parameter(self): filename, stream = guess_filename_stream("tests/resources/file.pdf") files = {"file_upload1": (filename, stream, "application/xxx")} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/xxx", body)
def test_encode_multipart_data_files_as_3tuple_parameter(self): filename, stream = guess_filename_stream("tests/resources/file.pdf") files = {"file_upload1": (filename, stream, "application/xxx")} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/xxx", body)
def clean_file(self, file_stream, filename, profile_id=None, analyze=False, analyze_format=ANALYZE_FORMAT_LIST_METADATA): """ Sends a file to Metashield Cleanup through its API to be cleaned and returns a response with the cleanId. :param file_stream: The stream/content of file :param filename: The file name :param profile_id: The profile id to be used. Default: None :param analyze: Sets if metadata should be analyzed :param analyze_format: Analysis format to be used on metadata analysis :return: Response of the request. If is correct, cleanId and analysisId attributes are inside data parameter. """ headers = self.default_headers() if profile_id is not None: headers[self.PROFILE_ID_11PATHS_HEADER_NAME] = profile_id if analyze: headers[self.ANALYZE_11PATHS_HEADER_NAME] = "True" if analyze_format not in self.VALID_METADATA_FORMATS: raise ValueError( "analyze_format must be a valid metadata format") else: headers[ self.ANALYZE_FORMAT_11PATHS_HEADER_NAME] = analyze_format return self.post(self.API_CLEAN_FILE_URL, headers=headers, files={"file": (filename, file_stream)}, renderer=MultiPartRenderer())
def test_encode_multipart_data_files(self): files = {"file_upload": "tests/resources/file.pdf", "file_upload2": "tests/resources/file.png"} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) self.assertIn(b"file_upload2", body) self.assertIn(b"file.png", body) self.assertIn(b"Content-Type: image/png", body)
def __anonymous_upload(self, document_hash, zip_file_path, document_type): zip_file = open(zip_file_path, 'rb').read() zip_file_name = uuid.uuid4().hex return self.post(url_path=_Route.BASE_URL + document_type + _Route.ANONYMOUS_UPLOAD, body_params={_Route.PARAM_HASH: document_hash}, files={_Route.PARAM_FILE: (zip_file_name, zip_file)}, renderers=MultiPartRenderer())
def test_encode_multipart_data_files(self): files = {"file_upload": "tests/resources/file.pdf", "file_upload2": "tests/resources/file.png"} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) self.assertIn(b"file_upload2", body) self.assertIn(b"file.png", body) self.assertIn(b"Content-Type: image/png", body)
def test_encode_multipart_data_as_2tuple_files(self): files = {"file_upload": "tests/resources/file.pdf", "file_upload2": "tests/resources/file.png"} data = {"param1": ("value1", "myContentType"), "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY") self.assertIn( b'------------ThIs_Is_tHe_bouNdaRY\r\nContent-Disposition: form-data; name="param1"\r\nContent-Type: myContentType\r\n\r\nvalue1\r\n', body) self.assertIn( b'------------ThIs_Is_tHe_bouNdaRY\r\nContent-Disposition: form-data; name="param2"\r\n\r\nvalue2\r\n', body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) self.assertIn(b"file_upload2", body) self.assertIn(b"file.png", body) self.assertIn(b"Content-Type: image/png", body)
def upload(self, file_path, password=None): file_content = open(file_path, 'rb').read() file_name = uuid.uuid4().hex body_params = {} if password: body_params[_Route.PARAM_PASSWORD] = password return self.post(url_path=_Route.UPLOAD, files={_Route.PARAM_FILE: (file_name, file_content)}, body_params=body_params, renderers=MultiPartRenderer())
def test_encode_multipart_data_as_2tuple_files(self): files = {"file_upload": "tests/resources/file.pdf", "file_upload2": "tests/resources/file.png"} data = {"param1": ("value1", "myContentType"), "param2": "value2"} r = MultiPartRenderer() body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=----------ThIs_Is_tHe_bouNdaRY_$") self.assertIn( b'------------ThIs_Is_tHe_bouNdaRY_$\r\nContent-Disposition: form-data; name="param1"\r\nContent-Type: myContentType\r\n\r\nvalue1\r\n', body, ) self.assertIn( b'------------ThIs_Is_tHe_bouNdaRY_$\r\nContent-Disposition: form-data; name="param2"\r\n\r\nvalue2\r\n', body, ) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) self.assertIn(b"file_upload2", body) self.assertIn(b"file.png", body) self.assertIn(b"Content-Type: image/png", body)
def _http_request(self, method, url_path, headers=None, query_params=None, body_params=None, files=None, **kwargs): """ Method to do http requests. :param method: :param url_path: :param headers: :param body_params: :param query_params: :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 1-tuple ``('filepath')``, 2-tuple ``('filepath', 'content_type')`` or a 3-tuple ``('filepath', 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. :param update_content_type: (bool) Update headers before performig the request, adding the Content-Type value according to the rendered body. By default: True. :return: """ host = kwargs.get('host', self.host) proxy = kwargs.get('proxy', self.proxy) renderer = kwargs.get('renderer', MultiPartRenderer() if files else self.default_renderer) prefix_url_path = kwargs.get('prefix_url_path', self.prefix_url_path) authentication_instances = kwargs.get('authentication_instances', self.authentication_instances) url_path_format = kwargs.get('url_path_format', self.url_path_format) update_content_type = kwargs.get('update_content_type', True) redirect = kwargs.get('redirect', False) if headers is None: headers = self.default_headers() context = HttpRequestContext( host=host, proxy=proxy, method=method, prefix_url_path=prefix_url_path, url_path=url_path, url_path_params=self.url_path_params, url_path_format=url_path_format, headers=headers, query_params=query_params, body_params=body_params, files=files, renderer=renderer, response_class=self.response_class, authentication_instances=authentication_instances, update_content_type=update_content_type, redirect=redirect ) res = self.http_request_from_context(context) self.cookie.update(res.cookie) return res
def test_encode_multipart_data_files_using_boundary_as_init_parameter(self): files = {"file_upload": "tests/resources/file.pdf"} data = {"param1": "value1", "param2": "value2"} r = MultiPartRenderer("custom_boundary") body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=custom_boundary") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body) body, content_type = r.encode_params(data, files) self.assertEqual(content_type, "multipart/form-data; boundary=custom_boundary") self.assertIn(b"param1", body) self.assertIn(b"value1", body) self.assertIn(b"param2", body) self.assertIn(b"value2", body) self.assertIn(b"file_upload", body) self.assertIn(b"file.pdf", body) self.assertIn(b"Content-Type: application/pdf", body)
def test_11paths_authentication_class_multiples_headers(self): auth = X11PathsAuthentication( app_id="2kNhWLEETQ46KWLnAg48", secret="lBc4BSeqCGkidJZXictc3yiHbKBS87hjE05YrswJ", utc="2017-01-27 08:27:44") context = HttpRequestContext( method="POST", url_path="/ExternalApi/CleanFile", renderer=MultiPartRenderer(), headers={ "X-11paths-profile-id": "77ed609a-1a9b-4c16-97c2-ba32f72f5499", "Content-Type": "multipart/form-data" }, files={"file": "tests/resources/file.png"}) res_context = auth.apply_authentication(context=context) self.assertEqual( "11PATHS 2kNhWLEETQ46KWLnAg48 8/fuEv9NLn41ikh96hRHMFGs1ww=", res_context.headers["Authorization"])
def upload_app(self, apk_file, tag_name=None): """ Upload app to Tacyt :param tag_name: put tag and apk :param apk_file: path to file apk :return: Response """ try: file_content = open(apk_file, "rb").read() file_name = uuid.uuid4().hex return self.post( url_path=self.API_UPLOAD_URL, files={"file": (file_name, file_content)}, renderers=MultiPartRenderer(), body_params={"tagName": tag_name}, ) except Exception as e: print(repr(e)) return None
def analyze_file(self, file_stream, filename, analyze_format=ANALYZE_FORMAT_LIST_METADATA): """ Sends a file to Metashield Cleanup through its API to be analyzed and returns a response with the analyzeId. :param file_stream: The stream/content of file :param filename: The file name :param analyze_format: Analysis format to be used on metadata analysis :return: Response of the request. If is correct, analysisId attribute is inside data parameter. """ headers = self.default_headers() if analyze_format is None or analyze_format not in self.VALID_METADATA_FORMATS: raise ValueError("analyze_format must be a valid metadata format") else: headers[self.ANALYZE_FORMAT_11PATHS_HEADER_NAME] = analyze_format return self.post(self.API_ANALYZE_FILE_URL, headers=headers, files={"file": (filename, file_stream)}, renderer=MultiPartRenderer())
def renderer(self, value): self._renderer = value or default_renderer if not self.files else MultiPartRenderer( )