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

Changeset 1474

Show
Ignore:
Timestamp:
12/07/06 20:04:57
Author:
fumanchu
Message:

Moved persistent conn support into webtest.

Files:

Legend:

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

    r1465 r1474  
    6363class ConnectionTests(helper.CPWebCase): 
    6464     
    65     def connect_persistent(self, auto_open=False): 
    66         """Set our HTTP_CONN to an instance so it persists between requests.""" 
    67         if self.scheme == "https": 
    68             self.HTTP_CONN = httplib.HTTPSConnection(self.HOST, self.PORT) 
    69         else: 
    70             self.HTTP_CONN = httplib.HTTPConnection(self.HOST, self.PORT) 
    71         # Automatically re-connect? 
    72         self.HTTP_CONN.auto_open = auto_open 
    73         self.HTTP_CONN.connect() 
    74         return self.HTTP_CONN 
    75      
    7665    def test_HTTP11(self): 
    7766        if cherrypy.server.protocol_version != "HTTP/1.1": 
     
    8170        self.PROTOCOL = "HTTP/1.1" 
    8271         
    83         self.connect_persistent() 
     72        self.persistent = True 
    8473         
    8574        # Make the first request and assert there's no "Connection: close". 
     
    117106        self.PROTOCOL = "HTTP/1.1" 
    118107         
    119         self.connect_persistent() 
     108        self.persistent = True 
    120109         
    121110        # Make the first request and assert there's no "Connection: close". 
     
    175164             
    176165            # Make an initial request 
    177             conn = self.connect_persistent() 
     166            self.persistent = True 
     167            conn = self.HTTP_CONN 
    178168            conn.putrequest("GET", "/", skip_host=True) 
    179169            conn.putheader("Host", self.HOST) 
     
    218208             
    219209            # Make another request on a new socket, which should work 
    220             conn = self.connect_persistent() 
     210            self.persistent = True 
     211            conn = self.HTTP_CONN 
    221212            conn.putrequest("GET", "/", skip_host=True) 
    222213            conn.putheader("Host", self.HOST) 
     
    239230         
    240231        # Test pipelining. httplib doesn't support this directly. 
    241         conn = self.connect_persistent() 
     232        self.persistent = True 
     233        conn = self.HTTP_CONN 
    242234         
    243235        # Put request 1 
     
    275267        self.PROTOCOL = "HTTP/1.1" 
    276268         
    277         conn = self.connect_persistent() 
     269        self.persistent = True 
     270        conn = self.HTTP_CONN 
    278271         
    279272        # Try a page without an Expect request header first. 
     
    326319         
    327320        # Set our HTTP_CONN to an instance so it persists between requests. 
    328         self.connect_persistent() 
     321        self.persistent = True 
     322        conn = self.HTTP_CONN 
    329323         
    330324        # Make the first request and assert there's no "Connection: close". 
     
    362356         
    363357        # Set our HTTP_CONN to an instance so it persists between requests. 
    364         if self.scheme == "https": 
    365             conn = httplib.HTTPSConnection(self.HOST, self.PORT) 
    366         else: 
    367             conn = httplib.HTTPConnection(self.HOST, self.PORT) 
     358        self.persistent = True 
     359        conn = self.HTTP_CONN 
    368360         
    369361        # Try a normal chunked request (with extensions) 
     
    415407         
    416408        # Test a keep-alive HTTP/1.0 request. 
    417         self.connect_persistent() 
     409        self.persistent = True 
    418410         
    419411        self.getPage("/page3", headers=[("Connection", "Keep-Alive")]) 
  • trunk/cherrypy/test/webtest.py

    r1405 r1474  
    142142    HTTP_CONN = httplib.HTTPConnection 
    143143    PROTOCOL = "HTTP/1.1" 
     144     
     145    def set_persistent(self, on=True, auto_open=False): 
     146        """Make our HTTP_CONN persistent (or not). 
     147         
     148        If the 'on' argument is True (the default), then self.HTTP_CONN 
     149        will be set to an instance of httplib.HTTPConnection (or HTTPS 
     150        if self.scheme is "https"). This will then persist across requests. 
     151         
     152        We only allow for a single open connection, so if you call this 
     153        and we currently have an open connection, it will be closed. 
     154        """ 
     155        try: 
     156            self.HTTP_CONN.close() 
     157        except (TypeError, AttributeError): 
     158            pass 
     159         
     160        if self.scheme == "https": 
     161            cls = httplib.HTTPSConnection 
     162        else: 
     163            cls = httplib.HTTPConnection 
     164         
     165        if on: 
     166            self.HTTP_CONN = cls(self.HOST, self.PORT) 
     167            # Automatically re-connect? 
     168            self.HTTP_CONN.auto_open = auto_open 
     169            self.HTTP_CONN.connect() 
     170        else: 
     171            self.HTTP_CONN = cls 
     172     
     173    def _get_persistent(self): 
     174        return hasattr(self.HTTP_CONN, "__class__") 
     175    def _set_persistent(self, on=True): 
     176        self.set_persistent(on) 
     177    persistent = property(_get_persistent, _set_persistent) 
    144178     
    145179    def getPage(self, url, headers=None, method="GET", body=None, protocol=None): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets