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   
7 -def setup_server():
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 # Client-side code # 29 30 from cherrypy.test import helper 31 32
33 -class MultipartTest(helper.CPWebCase):
34 setup_server = staticmethod(setup_server) 35
36 - def test_multipart(self):
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
68 - def test_multipart_form_data(self):
69 body = '\r\n'.join([ 70 '--X', 71 'Content-Disposition: form-data; name="foo"', 72 '', 73 'bar', 74 '--X', 75 # Test a param with more than one value. 76 # See 77 # https://bitbucket.org/cherrypy/cherrypy/issue/1028 78 'Content-Disposition: form-data; name="baz"', 79 '', 80 '111', 81 '--X', 82 'Content-Disposition: form-data; name="baz"', 83 '', 84 '333', 85 '--X--' 86 ]) 87 self.getPage('/multipart_form_data', method='POST', 88 headers=[( 89 "Content-Type", "multipart/form-data;boundary=X"), 90 ("Content-Length", str(len(body))), 91 ], 92 body=body), 93 self.assertBody( 94 repr([('baz', [ntou('111'), ntou('333')]), ('foo', ntou('bar'))]))
95 96
97 -class SafeMultipartHandlingTest(helper.CPWebCase):
98 setup_server = staticmethod(setup_server) 99
100 - def test_Flash_Upload(self):
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 # Flash apps omit the trailing \r\n on the last line: 131 '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--' 132 )) 133 self.getPage('/flashupload', headers, "POST", body) 134 self.assertBody("Upload: Submit Query, Filename: .project, " 135 "Filedata: %r" % filedata)
136