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

Changeset 1906

Show
Ignore:
Timestamp:
03/07/08 11:03:00
Author:
fumanchu
Message:

Backported the (improved) test_post_multipart to the 598 branch.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/598-sendall/cherrypy/test/test.py

    r1802 r1906  
    371371        'test_encoding', 
    372372        'test_etags', 
     373        'test_http', 
    373374        'test_httpauth', 
    374375        'test_httplib', 
  • branches/598-sendall/cherrypy/test/test_http.py

    r1767 r1906  
    1 """Tests for managing HTTP issues (malformed requests, etc). 
    2  
    3 Some of these tests check timeouts, etcetera, and therefore take a long 
    4 time to run. Therefore, this module should probably not be included in 
    5 the 'comprehensive' test suite (test.py). 
    6 """ 
     1"""Tests for managing HTTP issues (malformed requests, etc).""" 
    72 
    83from cherrypy.test import test 
     
    116import httplib 
    127import cherrypy 
     8import 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 
    1331 
    1432 
     
    1937            return "Hello world!" 
    2038        index.exposed = True 
     39         
     40        def post_multipart(self, file): 
     41            """Return a summary ("a * 1000000\nb * 1000000") 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 
    2158     
    2259    cherrypy.tree.mount(Root()) 
    23     cherrypy.config.update({'environment': 'test_suite'}) 
     60    cherrypy.config.update({'environment': 'test_suite', 
     61                            'server.max_request_body_size': 30000000}) 
    2462 
    2563 
     
    3573        c.request("POST", "/") 
    3674        self.assertEqual(c.getresponse().status, 411) 
     75     
     76    def test_post_multipart(self): 
     77        alphabet = "abcdefghijklmnopqrstuvwxyz" 
     78        # generate file contents for a large post 
     79        contents = "".join([c * 1000000 for c in alphabet]) 
     80         
     81        # encode as multipart form data 
     82        files=[('file', 'file.txt', contents)] 
     83        content_type, body = encode_multipart_formdata(files) 
     84         
     85        # post file 
     86        if self.scheme == 'https': 
     87            c = httplib.HTTPS('127.0.0.1:%s' % self.PORT) 
     88        else: 
     89            c = httplib.HTTP('127.0.0.1:%s' % self.PORT) 
     90        c.putrequest('POST', '/post_multipart') 
     91        c.putheader('Content-Type', content_type) 
     92        c.putheader('Content-Length', str(len(body))) 
     93        c.endheaders() 
     94        c.send(body) 
     95         
     96        errcode, errmsg, headers = c.getreply() 
     97        self.assertEqual(errcode, 200) 
     98         
     99        response_body = c.file.read() 
     100        self.assertEquals(", ".join(["%s * 1000000" % c for c in alphabet]), 
     101                          response_body) 
    37102 
    38103 

Hosted by WebFaction

Log in as guest/cpguest to create tickets