1 """Tests for various MIME issues, including the safe_multipart Tool."""
2
3 import cherrypy
4 from cherrypy._cpcompat import ntob, ntou, sorted
5
6
8
9 class Root:
10
11 def multipart(self, parts):
12 return repr(parts)
13 multipart.exposed = True
14
15 def multipart_form_data(self, **kwargs):
16 return repr(list(sorted(kwargs.items())))
17 multipart_form_data.exposed = True
18
19 def flashupload(self, Filedata, Upload, Filename):
20 return ("Upload: %s, Filename: %s, Filedata: %r" %
21 (Upload, Filename, Filedata.file.read()))
22 flashupload.exposed = True
23
24 cherrypy.config.update({'server.max_request_body_size': 0})
25 cherrypy.tree.mount(Root())
26
27
28
29
30 from cherrypy.test import helper
31
32
34 setup_server = staticmethod(setup_server)
35
37 text_part = ntou("This is the text version")
38 html_part = ntou(
39 """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
40 <html>
41 <head>
42 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
43 </head>
44 <body bgcolor="#ffffff" text="#000000">
45
46 This is the <strong>HTML</strong> version
47 </body>
48 </html>
49 """)
50 body = '\r\n'.join([
51 "--123456789",
52 "Content-Type: text/plain; charset='ISO-8859-1'",
53 "Content-Transfer-Encoding: 7bit",
54 "",
55 text_part,
56 "--123456789",
57 "Content-Type: text/html; charset='ISO-8859-1'",
58 "",
59 html_part,
60 "--123456789--"])
61 headers = [
62 ('Content-Type', 'multipart/mixed; boundary=123456789'),
63 ('Content-Length', str(len(body))),
64 ]
65 self.getPage('/multipart', headers, "POST", body)
66 self.assertBody(repr([text_part, html_part]))
67
95
96
98 setup_server = staticmethod(setup_server)
99
101 headers = [
102 ('Accept', 'text/*'),
103 ('Content-Type', 'multipart/form-data; '
104 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'),
105 ('User-Agent', 'Shockwave Flash'),
106 ('Host', 'www.example.com:54583'),
107 ('Content-Length', '499'),
108 ('Connection', 'Keep-Alive'),
109 ('Cache-Control', 'no-cache'),
110 ]
111 filedata = ntob('<?xml version="1.0" encoding="UTF-8"?>\r\n'
112 '<projectDescription>\r\n'
113 '</projectDescription>\r\n')
114 body = (ntob(
115 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
116 'Content-Disposition: form-data; name="Filename"\r\n'
117 '\r\n'
118 '.project\r\n'
119 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
120 'Content-Disposition: form-data; '
121 'name="Filedata"; filename=".project"\r\n'
122 'Content-Type: application/octet-stream\r\n'
123 '\r\n')
124 + filedata +
125 ntob('\r\n'
126 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n'
127 'Content-Disposition: form-data; name="Upload"\r\n'
128 '\r\n'
129 'Submit Query\r\n'
130
131 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--'
132 ))
133 self.getPage('/flashupload', headers, "POST", body)
134 self.assertBody("Upload: Submit Query, Filename: .project, "
135 "Filedata: %r" % filedata)
136