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

Changeset 1898

Show
Ignore:
Timestamp:
02/20/08 14:35:14
Author:
fumanchu
Message:

New test case from patch on #783 (File uploads corrupt when using built in SSL). Thanks to noah for providing it!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/test_http.py

    r1897 r1898  
    66import httplib 
    77import cherrypy 
     8import md5, mimetypes 
     9 
     10 
     11def 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 
    831 
    932 
     
    1437            return "Hello world!" 
    1538        index.exposed = True 
     39         
     40        def post_multipart(self, file): 
     41            # compute and return md5 of posted file 
     42            contents = file.file.read() 
     43            return md5.md5(contents).hexdigest() 
     44        post_multipart.exposed = True 
    1645     
    1746    cherrypy.tree.mount(Root()) 
     
    3059        c.request("POST", "/") 
    3160        self.assertEqual(c.getresponse().status, 411) 
     61     
     62    def test_post_multipart(self): 
     63        # generate file contents for a large post 
     64        contents = "abcdefghijklmnopqrstuvwxyz" * 1000000 
     65        post_md5 = md5.md5(contents).hexdigest() 
     66         
     67        # encode as multipart form data 
     68        files=[('file', 'file.txt', contents)] 
     69        content_type, body = encode_multipart_formdata(files) 
     70         
     71        # post file 
     72        if self.scheme == 'https': 
     73            c = httplib.HTTPS('127.0.0.1:%s' % self.PORT) 
     74        else: 
     75            c = httplib.HTTP('127.0.0.1:%s' % self.PORT) 
     76        c.putrequest('POST', '/post_multipart') 
     77        c.putheader('Content-Type', content_type) 
     78        c.putheader('Content-Length', str(len(body))) 
     79        c.endheaders() 
     80        c.send(body) 
     81         
     82        errcode, errmsg, headers = c.getreply() 
     83        self.assertEqual(errcode, 200) 
     84         
     85        response_body = c.file.read() 
     86        self.assertEquals(post_md5, response_body) 
    3287 
    3388 

Hosted by WebFaction

Log in as guest/cpguest to create tickets