def test_environ_builder_stream_switch(self): d = MultiDict(dict(foo=u'bar', blub=u'blah', hu=u'hum')) for use_tempfile in False, True: stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150) assert isinstance(stream, OutputType) != use_tempfile form = parse_form_data({'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary})[1] assert form == d
def test_environ_builder_stream_switch(self): d = MultiDict(dict(foo=u'bar', blub=u'blah', hu=u'hum')) for use_tempfile in False, True: stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150) self.assert_true(isinstance(stream, BytesIO) != use_tempfile) form = parse_form_data({'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary})[1] self.assert_strict_equal(form, d) stream.close()
def test_environ_builder_stream_switch(): d = MultiDict(dict(foo=u"bar", blub=u"blah", hu=u"hum")) for use_tempfile in False, True: stream, length, boundary = stream_encode_multipart(d, use_tempfile, threshold=150) assert isinstance(stream, BytesIO) != use_tempfile form = parse_form_data( { "wsgi.input": stream, "CONTENT_LENGTH": str(length), "CONTENT_TYPE": 'multipart/form-data; boundary="%s"' % boundary, } )[1] strict_eq(form, d) stream.close()
def test_environ_builder_stream_switch(): """EnvironBuilder stream switch""" from cStringIO import OutputType from werkzeug.test import stream_encode_multipart from werkzeug import url_decode, MultiDict, parse_form_data d = MultiDict(dict(foo=u'bar', blub=u'blah', hu=u'hum')) for use_tempfile in False, True: stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150) assert isinstance(stream, OutputType) != use_tempfile form = parse_form_data({'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary})[1] assert form == d
def test_environ_builder_stream_switch(): d = MultiDict(dict(foo="bar", blub="blah", hu="hum")) for use_tempfile in False, True: stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150 ) assert isinstance(stream, BytesIO) != use_tempfile form = parse_form_data( { "wsgi.input": stream, "CONTENT_LENGTH": str(length), "CONTENT_TYPE": f'multipart/form-data; boundary="{boundary}"', } )[1] assert form == d stream.close()
def test_environ_builder_unicode_file_mix(): for use_tempfile in False, True: f = FileStorage(BytesIO(u"\N{SNOWMAN}".encode("utf-8")), "snowman.txt") d = MultiDict(dict(f=f, s=u"\N{SNOWMAN}")) stream, length, boundary = stream_encode_multipart(d, use_tempfile, threshold=150) assert isinstance(stream, BytesIO) != use_tempfile _, form, files = parse_form_data( { "wsgi.input": stream, "CONTENT_LENGTH": str(length), "CONTENT_TYPE": 'multipart/form-data; boundary="%s"' % boundary, } ) strict_eq(form["s"], u"\N{SNOWMAN}") strict_eq(files["f"].name, "f") strict_eq(files["f"].filename, u"snowman.txt") strict_eq(files["f"].read(), u"\N{SNOWMAN}".encode("utf-8")) stream.close()
def test_chunked_encoding(monkeypatch, dev_server, send_length): stream, length, boundary = stream_encode_multipart({ "value": "this is text", "file": FileStorage( BytesIO(b"this is a file"), filename="test.txt", content_type="text/plain", ), }) headers = {"content-type": f"multipart/form-data; boundary={boundary}"} if send_length: headers["transfer-encoding"] = "chunked" headers["content-length"] = str(length) client = dev_server("data") # Small block size to produce multiple chunks. conn = client.connect(blocksize=128) conn.putrequest("POST", "/") conn.putheader("Transfer-Encoding", "chunked") conn.putheader("Content-Type", f"multipart/form-data; boundary={boundary}") # Sending the content-length header with chunked is invalid, but if # a client does send it the server should ignore it. Previously the # multipart parser would crash. Python's higher-level functions # won't send the header, which is why we use conn.put in this test. if send_length: conn.putheader("Content-Length", "invalid") conn.endheaders(stream, encode_chunked=True) r = conn.getresponse() data = json.load(r) r.close() assert data["form"]["value"] == "this is text" assert data["files"]["file"] == "this is a file" environ = data["environ"] assert environ["HTTP_TRANSFER_ENCODING"] == "chunked" assert "HTTP_CONTENT_LENGTH" not in environ assert environ["wsgi.input_terminated"]
def test_environ_builder_unicode_file_mix(self): for use_tempfile in False, True: f = FileStorage(BytesIO(u'\N{SNOWMAN}'.encode('utf-8')), 'snowman.txt') d = MultiDict(dict(f=f, s=u'\N{SNOWMAN}')) stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150) self.assert_true(isinstance(stream, BytesIO) != use_tempfile) _, form, files = parse_form_data({ 'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary }) self.assert_strict_equal(form['s'], u'\N{SNOWMAN}') self.assert_strict_equal(files['f'].name, 'f') self.assert_strict_equal(files['f'].filename, u'snowman.txt') self.assert_strict_equal(files['f'].read(), u'\N{SNOWMAN}'.encode('utf-8')) stream.close()
def test_environ_builder_unicode_file_mix(): for use_tempfile in False, True: f = FileStorage(BytesIO(br"\N{SNOWMAN}"), "snowman.txt") d = MultiDict(dict(f=f, s="\N{SNOWMAN}")) stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150 ) assert isinstance(stream, BytesIO) != use_tempfile _, form, files = parse_form_data( { "wsgi.input": stream, "CONTENT_LENGTH": str(length), "CONTENT_TYPE": f'multipart/form-data; boundary="{boundary}"', } ) assert form["s"] == "\N{SNOWMAN}" assert files["f"].name == "f" assert files["f"].filename == "snowman.txt" assert files["f"].read() == br"\N{SNOWMAN}" stream.close()
def test_environ_builder_unicode_file_mix(): for use_tempfile in False, True: f = FileStorage(BytesIO(u'\N{SNOWMAN}'.encode('utf-8')), 'snowman.txt') d = MultiDict(dict(f=f, s=u'\N{SNOWMAN}')) stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150) assert isinstance(stream, BytesIO) != use_tempfile _, form, files = parse_form_data({ 'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary }) strict_eq(form['s'], u'\N{SNOWMAN}') strict_eq(files['f'].name, 'f') strict_eq(files['f'].filename, u'snowman.txt') strict_eq(files['f'].read(), u'\N{SNOWMAN}'.encode('utf-8')) stream.close()
def test_environ_builder_unicode_file_mix(): for use_tempfile in False, True: f = FileStorage(BytesIO(u"\N{SNOWMAN}".encode("utf-8")), "snowman.txt") d = MultiDict(dict(f=f, s=u"\N{SNOWMAN}")) stream, length, boundary = stream_encode_multipart( d, use_tempfile, threshold=150 ) assert isinstance(stream, BytesIO) != use_tempfile _, form, files = parse_form_data( { "wsgi.input": stream, "CONTENT_LENGTH": str(length), "CONTENT_TYPE": 'multipart/form-data; boundary="%s"' % boundary, } ) strict_eq(form["s"], u"\N{SNOWMAN}") strict_eq(files["f"].name, "f") strict_eq(files["f"].filename, u"snowman.txt") strict_eq(files["f"].read(), u"\N{SNOWMAN}".encode("utf-8")) stream.close()
def test_environ_builder_stream_switch(): """EnvironBuilder stream switch""" from cStringIO import OutputType from werkzeug.test import stream_encode_multipart from werkzeug import url_decode, MultiDict, parse_form_data d = MultiDict(dict(foo=u'bar', blub=u'blah', hu=u'hum')) for use_tempfile in False, True: stream, length, boundary = stream_encode_multipart(d, use_tempfile, threshold=150) assert isinstance(stream, OutputType) != use_tempfile form = parse_form_data({ 'wsgi.input': stream, 'CONTENT_LENGTH': str(length), 'CONTENT_TYPE': 'multipart/form-data; boundary="%s"' % boundary })[1] assert form == d