Package cherrypy :: Package test :: Module test_mime
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_mime

  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 -def setup_server():
7 8 class Root: 9 10 def multipart(self, parts): 11 return repr(parts)
12 multipart.exposed = True 13 14 def multipart_form_data(self, **kwargs): 15 return repr(list(sorted(kwargs.items()))) 16 multipart_form_data.exposed = True 17 18 def flashupload(self, Filedata, Upload, Filename): 19 return ("Upload: %s, Filename: %s, Filedata: %r" % 20 (Upload, Filename, Filedata.file.read())) 21 flashupload.exposed = True 22 23 cherrypy.config.update({'server.max_request_body_size': 0}) 24 cherrypy.tree.mount(Root()) 25 26 27 # Client-side code # 28 29 from cherrypy.test import helper 30
31 -class MultipartTest(helper.CPWebCase):
32 setup_server = staticmethod(setup_server) 33
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"> 37 <html> 38 <head> 39 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> 40 </head> 41 <body bgcolor="#ffffff" text="#000000"> 42 43 This is the <strong>HTML</strong> version 44 </body> 45 </html> 46 """) 47 body = '\r\n'.join([ 48 "--123456789", 49 "Content-Type: text/plain; charset='ISO-8859-1'", 50 "Content-Transfer-Encoding: 7bit", 51 "", 52 text_part, 53 "--123456789", 54 "Content-Type: text/html; charset='ISO-8859-1'", 55 "", 56 html_part, 57 "--123456789--"]) 58 headers = [ 59 ('Content-Type', 'multipart/mixed; boundary=123456789'), 60 ('Content-Length', str(len(body))), 61 ] 62 self.getPage('/multipart', headers, "POST", body) 63 self.assertBody(repr([text_part, html_part]))
64
65 - def test_multipart_form_data(self):
66 body='\r\n'.join(['--X', 67 'Content-Disposition: form-data; name="foo"', 68 '', 69 'bar', 70 '--X', 71 # Test a param with more than one value. 72 # See http://www.cherrypy.org/ticket/1028 73 'Content-Disposition: form-data; name="baz"', 74 '', 75 '111', 76 '--X', 77 'Content-Disposition: form-data; name="baz"', 78 '', 79 '333', 80 '--X--']) 81 self.getPage('/multipart_form_data', method='POST', 82 headers=[("Content-Type", "multipart/form-data;boundary=X"), 83 ("Content-Length", str(len(body))), 84 ], 85 body=body), 86 self.assertBody(repr([('baz', [ntou('111'), ntou('333')]), ('foo', ntou('bar'))]))
87 88
89 -class SafeMultipartHandlingTest(helper.CPWebCase):
90 setup_server = staticmethod(setup_server) 91
92 - def test_Flash_Upload(self):
93 headers = [ 94 ('Accept', 'text/*'), 95 ('Content-Type', 'multipart/form-data; ' 96 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'), 97 ('User-Agent', 'Shockwave Flash'), 98 ('Host', 'www.example.com:54583'), 99 ('Content-Length', '499'), 100 ('Connection', 'Keep-Alive'), 101 ('Cache-Control', 'no-cache'), 102 ] 103 filedata = ntob('<?xml version="1.0" encoding="UTF-8"?>\r\n' 104 '<projectDescription>\r\n' 105 '</projectDescription>\r\n') 106 body = (ntob( 107 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' 108 'Content-Disposition: form-data; name="Filename"\r\n' 109 '\r\n' 110 '.project\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' 115 '\r\n') 116 + filedata + 117 ntob('\r\n' 118 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' 119 'Content-Disposition: form-data; name="Upload"\r\n' 120 '\r\n' 121 'Submit Query\r\n' 122 # Flash apps omit the trailing \r\n on the last line: 123 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--' 124 )) 125 self.getPage('/flashupload', headers, "POST", body) 126 self.assertBody("Upload: Submit Query, Filename: .project, " 127 "Filedata: %r" % filedata)
128