1 """Tests for various MIME issues, including the safe_multipart Tool."""
4 from cherrypy
._cpcompat
import ntob
, ntou
, sorted
10 def multipart(self
, parts
):
12 multipart
.exposed
= True
14 def multipart_form_data(self
, **kwargs
):
15 return repr(list(sorted(kwargs
.items())))
16 multipart_form_data
.exposed
= True
18 def flashupload(self
, Filedata
, Upload
, Filename
):
19 return ("Upload: %r, Filename: %r, Filedata: %r" %
20 (Upload
, Filename
, Filedata
.file.read()))
21 flashupload
.exposed
= True
23 cherrypy
.config
.update({'server.max_request_body_size': 0})
24 cherrypy
.tree
.mount(Root())
29 from cherrypy
.test
import helper
31 class MultipartTest(helper
.CPWebCase
):
32 setup_server
= staticmethod(setup_server
)
34 def test_multipart(self
):
35 text_part
= ntou("This is the text version")
36 html_part
= ntou("""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
39 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
41 <body bgcolor="#ffffff" text="#000000">
43 This is the <strong>HTML</strong> version
49 "Content-Type: text/plain; charset='ISO-8859-1'",
50 "Content-Transfer-Encoding: 7bit",
54 "Content-Type: text/html; charset='ISO-8859-1'",
59 ('Content-Type', 'multipart/mixed; boundary=123456789'),
60 ('Content-Length', str(len(body
))),
62 self
.getPage('/multipart', headers
, "POST", body
)
63 self
.assertBody(repr([text_part
, html_part
]))
65 def test_multipart_form_data(self
):
66 body
='\r\n'.join(['--X',
67 'Content-Disposition: form-data; name="foo"',
71 # Test a param with more than one value.
72 # See http://www.cherrypy.org/ticket/1028
73 'Content-Disposition: form-data; name="baz"',
77 'Content-Disposition: form-data; name="baz"',
81 self
.getPage('/multipart_form_data', method
='POST',
82 headers
=[("Content-Type", "multipart/form-data;boundary=X"),
83 ("Content-Length", str(len(body
))),
86 self
.assertBody(repr([('baz', [u
'111', u
'333']), ('foo', u
'bar')]))
89 class SafeMultipartHandlingTest(helper
.CPWebCase
):
90 setup_server
= staticmethod(setup_server
)
92 def test_Flash_Upload(self
):
95 ('Content-Type', 'multipart/form-data; '
96 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'),
97 ('User-Agent', 'Shockwave Flash'),
98 ('Host', 'www.example.com:8080'),
99 ('Content-Length', '499'),
100 ('Connection', 'Keep-Alive'),
101 ('Cache-Control', 'no-cache'),
103 filedata
= ntob('<?xml version="1.0" encoding="UTF-8"?>\r\n'
104 '<projectDescription>\r\n'
105 '</projectDescription>\r\n')
107 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
108 'Content-Disposition: form-data; name="Filename"\r\n'
111 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
112 'Content-Disposition: form-data; '
113 'name="Filedata"; filename=".project"\r\n'
114 'Content-Type: application/octet-stream\r\n'
118 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
119 'Content-Disposition: form-data; name="Upload"\r\n'
122 # Flash apps omit the trailing \r\n on the last line:
123 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--'
125 self
.getPage('/flashupload', headers
, "POST", body
)
126 self
.assertBody("Upload: u'Submit Query', Filename: u'.project', "
127 "Filedata: %r" % filedata
)