Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/trunk/cherrypy/test/test_http.py

Revision 1915 (checked in by fumanchu, 6 months ago)

Merging the 598-sendall branch. This fixes #598, #783, and perhaps #589.

  • Property svn:eol-style set to native
Line 
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
10
11 def encode_multipart_formdata(files):
12     """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, body
31
32
33 def setup_server():
34    
35     class Root:
36         def index(self, *args, **kwargs):
37             return "Hello world!"
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
65
66 class HTTPTests(helper.CPWebCase):
67    
68     def test_sockets(self):
69         # 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("127.0.0.1:%s" % self.PORT)
74         else:
75             c = httplib.HTTPConnection("127.0.0.1:%s" % self.PORT)
76         c.request("POST", "/")
77         self.assertEqual(c.getresponse().status, 411)
78    
79     def test_post_multipart(self):
80         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('127.0.0.1:%s' % self.PORT)
91         else:
92             c = httplib.HTTP('127.0.0.1:%s' % 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)
105
106
107 if __name__ == '__main__':
108     setup_server()
109     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets