Best Python code snippet using Kiwi_python
test_multipart.py
Source:test_multipart.py
...13 b"""------------a_BoUnDaRy7283067873172754$--\r\n"""14)15def test_multipart():16 app = App(None)17 content_type, body = app.encode_multipart([18 ('test', 'some value'),19 ('test', 'some other value'),20 ('foo', 'bar')21 ], [])22 data = parser(BytesIO(body), content_type)23 assert len(data.form) == 324 assert data.form == [25 ('test', 'some value'),26 ('test', 'some other value'),27 ('foo', 'bar')28 ]29def test_empty_multipart():30 app = App(None)31 content_type, body = app.encode_multipart([32 ('test', ''),33 ('test', ''),34 ('foo', 'bar')35 ], [])36 data = parser(BytesIO(body), content_type)37 assert len(data.form) == 138 assert data.form == [39 ('foo', 'bar')40 ]41def test_multipart_empty_files_empty_name():42 app = App(None)43 content_type, body = app.encode_multipart(44 [],45 [('test', "", b'', "application/octet")],46 )47 data = parser(BytesIO(body), content_type)48 assert data.form == []49 content_type, body = app.encode_multipart(50 [],51 [('test', "", b'', "application/octet"),52 ('test2', "", b'', "application/octet")],53 )54 data = parser(BytesIO(body), content_type)55 assert data.form == []56def test_multipart_empty_files_mixed():57 app = App(None)58 content_type, body = app.encode_multipart(59 [],60 [('test', "name", b'', "application/octet"),61 ('test2', "", b'content', "application/octet"),62 ('test3', "", b'', "application/octet")],63 )64 data = parser(BytesIO(body), content_type)65 assert len(data.form) == 266 _, file1 = data.form[0]67 _, file2 = data.form[1]68 assert file1.filename == "name"69 assert file2.read() == b'content'70 assert file2.filename != ''71def test_multipart_empty_filename_generation():72 app = App(None)73 content_type, body = app.encode_multipart(74 [],75 [('test', "", b'content', "application/octet"),76 ('test', "", b'content', "application/octet")]77 )78 data = parser(BytesIO(body), content_type)79 _, file1 = data.form[0]80 _, file2 = data.form[1]81 assert file1.filename != file2.filename82 assert file1.filename != ''83def test_multipart_files():84 app = App(None)85 content_type, body = app.encode_multipart(86 [('test', b'some value')],87 [('files', "baz-\xe9.png", b'abcdef', 'image/png'),88 ('files', "MyText.txt", b'ghi', 'text/plain')]89 )90 data = parser(BytesIO(body), content_type)91 uploaded = data.form[1:]92 assert len(uploaded) == 293 name, obj = uploaded[0]94 assert obj.filename == 'baz-é.png'95 assert obj.content_type == b'image/png'96 assert obj.read() == b'abcdef'97 name, obj = uploaded[1]98 assert obj.filename == 'MyText.txt'99 assert obj.content_type == b'text/plain'...
form.py
Source:form.py
...4 from cStringIO import StringIO as BytesIO5else:6 from io import BytesIO7from w3lib.util import unicode_to_str8def encode_multipart(data):9 r"""10 .. warning::11 This function is deprecated and will be removed in future.12 Please use ``urllib3.filepost.encode_multipart_formdata`` instead.13 Encode the given data to be used in a multipart HTTP POST.14 `data` is a dictionary where keys are the field name, and values are15 either strings or tuples as `(filename, content)` for file uploads.16 This code is based on :class:`distutils.command.upload`.17 Returns a `(body, boundary)` tuple where `body` is binary body value,18 and `boundary` is the boundary used (as native string).19 >>> import w3lib.form20 >>> w3lib.form.encode_multipart({'key': 'value'})21 ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key"\r\n\r\nvalue\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254')22 >>> w3lib.form.encode_multipart({'key1': 'value1', 'key2': 'value2'}) # doctest: +SKIP23 ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key2"\r\n\r\nvalue2\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254')24 >>> w3lib.form.encode_multipart({'somekey': ('path/to/filename', b'\xa1\xa2\xa3\xa4\r\n\r')})25 ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="somekey"; filename="path/to/filename"\r\n\r\n\xa1\xa2\xa3\xa4\r\n\r\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254')26 >>>27 """28 warnings.warn(29 "`w3lib.form.encode_multipart` function is deprecated and "30 "will be removed in future releases. Please use "31 "`urllib3.filepost.encode_multipart_formdata` instead.",32 DeprecationWarning33 )34 # Build up the MIME payload for the POST data35 boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254'36 sep_boundary = b'\r\n--' + boundary.encode('ascii')37 end_boundary = sep_boundary + b'--'38 body = BytesIO()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!