Home | Trees | Indices | Help |
|
---|
|
1 """Tests for managing HTTP issues (malformed requests, etc).""" 2 3 from cherrypy.test import test 4 test.prefer_parent_path() 5 6 import httplib 7 import cherrypy 8 import mimetypes 9 1012 """Return (content_type, body) ready for httplib.HTTP instance. 13 14 files: a sequence of (name, filename, value) tuples for multipart uploads. 15 """ 16 BOUNDARY = '________ThIs_Is_tHe_bouNdaRY_$' 17 L = [] 18 for key, filename, value in files: 19 L.append('--' + BOUNDARY) 20 L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % 21 (key, filename)) 22 ct = mimetypes.guess_type(filename)[0] or 'application/octet-stream' 23 L.append('Content-Type: %s' % ct) 24 L.append('') 25 L.append(value) 26 L.append('--' + BOUNDARY + '--') 27 L.append('') 28 body = '\r\n'.join(L) 29 content_type = 'multipart/form-data; boundary=%s' % BOUNDARY 30 return content_type, body31 32 38 index.exposed = True 39 40 def post_multipart(self, file): 41 """Return a summary ("a * 65536\nb * 65536") of the uploaded file.""" 42 contents = file.file.read() 43 summary = [] 44 curchar = "" 45 count = 0 46 for c in contents: 47 if c == curchar: 48 count += 1 49 else: 50 if count: 51 summary.append("%s * %d" % (curchar, count)) 52 count = 1 53 curchar = c 54 if count: 55 summary.append("%s * %d" % (curchar, count)) 56 return ", ".join(summary) 57 post_multipart.exposed = True 58 59 cherrypy.tree.mount(Root()) 60 cherrypy.config.update({'environment': 'test_suite', 61 'server.max_request_body_size': 30000000}) 62 63 64 from cherrypy.test import helper 6567140 141 142 if __name__ == '__main__': 143 setup_server() 144 helper.testmain() 14569 # By not including a Content-Length header, cgi.FieldStorage 70 # will hang. Verify that CP times out the socket and responds 71 # with 411 Length Required. 72 if self.scheme == "https": 73 c = httplib.HTTPSConnection('%s:%s' % (self.interface(), self.PORT)) 74 else: 75 c = httplib.HTTPConnection('%s:%s' % (self.interface(), self.PORT)) 76 c.request("POST", "/") 77 self.assertEqual(c.getresponse().status, 411)7880 alphabet = "abcdefghijklmnopqrstuvwxyz" 81 # generate file contents for a large post 82 contents = "".join([c * 65536 for c in alphabet]) 83 84 # encode as multipart form data 85 files=[('file', 'file.txt', contents)] 86 content_type, body = encode_multipart_formdata(files) 87 88 # post file 89 if self.scheme == 'https': 90 c = httplib.HTTPS('%s:%s' % (self.interface(), self.PORT)) 91 else: 92 c = httplib.HTTP('%s:%s' % (self.interface(), self.PORT)) 93 c.putrequest('POST', '/post_multipart') 94 c.putheader('Content-Type', content_type) 95 c.putheader('Content-Length', str(len(body))) 96 c.endheaders() 97 c.send(body) 98 99 errcode, errmsg, headers = c.getreply() 100 self.assertEqual(errcode, 200) 101 102 response_body = c.file.read() 103 self.assertEquals(", ".join(["%s * 65536" % c for c in alphabet]), 104 response_body)105107 if getattr(cherrypy.server, "using_apache", False): 108 print "skipped due to known Apache differences...", 109 return 110 111 # Test missing version in Request-Line 112 if self.scheme == 'https': 113 c = httplib.HTTPSConnection('%s:%s' % (self.interface(), self.PORT)) 114 else: 115 c = httplib.HTTPConnection('%s:%s' % (self.interface(), self.PORT)) 116 c._output('GET /') 117 c._send_output() 118 response = c.response_class(c.sock, strict=c.strict, method='GET') 119 response.begin() 120 self.assertEqual(response.status, 400) 121 self.assertEqual(response.fp.read(), "Malformed Request-Line") 122 c.close()123125 if self.scheme != 'https': 126 print "skipped (not running HTTPS)...", 127 return 128 129 # Try connecting without SSL. 130 conn = httplib.HTTPConnection('%s:%s' % (self.interface(), self.PORT)) 131 conn.putrequest("GET", "/", skip_host=True) 132 conn.putheader("Host", self.HOST) 133 conn.endheaders() 134 response = conn.response_class(conn.sock, method="GET") 135 response.begin() 136 self.assertEqual(response.status, 400) 137 self.body = response.read() 138 self.assertBody("The client sent a plain HTTP request, but this " 139 "server only speaks HTTPS on this port.")
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Tue Mar 19 14:12:27 2013 | http://epydoc.sourceforge.net |