[devtools] add a 'method' argument to RepoAccess.web_request
authorDavid Douard <david.douard@logilab.fr>
Wed, 12 Mar 2014 16:02:44 +0100
changeset 9570 14452b344d19
parent 9569 ac0bc417b55f
child 9571 aaf83cc07eed
[devtools] add a 'method' argument to RepoAccess.web_request so that one can easily forge a request with any HTTP method. Also a bunch of clone cleanups.
devtools/fake.py
devtools/testlib.py
--- a/devtools/fake.py	Fri Mar 14 11:38:14 2014 +0100
+++ b/devtools/fake.py	Wed Mar 12 16:02:44 2014 +0100
@@ -88,20 +88,20 @@
         return url.split('?', 1)[0]
 
     def set_request_header(self, header, value, raw=False):
-        """set an incoming HTTP header (For test purpose only)"""
+        """set an incoming HTTP header (for test purpose only)"""
         if isinstance(value, basestring):
             value = [value]
-        if raw: #
+        if raw:
             # adding encoded header is important, else page content
             # will be reconverted back to unicode and apart unefficiency, this
             # may cause decoding problem (e.g. when downloading a file)
             self._headers_in.setRawHeaders(header, value)
-        else: #
+        else:
             self._headers_in.setHeader(header, value) #
 
     def get_response_header(self, header, default=None, raw=False):
-        """return output header (For test purpose only"""
-        if raw: #
+        """return output header (for test purpose only)"""
+        if raw:
             return self.headers_out.getRawHeaders(header, [default])[0]
         return self.headers_out.getHeader(header, default)
 
--- a/devtools/testlib.py	Fri Mar 14 11:38:14 2014 +0100
+++ b/devtools/testlib.py	Wed Mar 12 16:02:44 2014 +0100
@@ -227,7 +227,7 @@
             yield cnx
 
     @ contextmanager
-    def web_request(self, url=None, headers={}, **kwargs):
+    def web_request(self, url=None, headers={}, method='GET', **kwargs):
         """Context manager returning a web request pre-linked to a client cnx
 
         To commit and rollback use::
@@ -235,7 +235,8 @@
             req.cnx.commit()
             req.cnx.rolback()
         """
-        req = self.requestcls(self._repo.vreg, url=url, headers=headers, form=kwargs)
+        req = self.requestcls(self._repo.vreg, url=url, headers=headers,
+                              method=method, form=kwargs)
         clt_cnx = repoapi.ClientConnection(self._session)
         req.set_cnx(clt_cnx)
         with clt_cnx:
@@ -269,6 +270,7 @@
     """
     appid = 'data'
     configcls = devtools.ApptestConfiguration
+    requestcls = fake.FakeRequest
     tags = TestCase.tags | Tags('cubicweb', 'cw_repo')
     test_db_id = DEFAULT_EMPTY_DB_ID
     _cnxs = set() # establised connection
@@ -298,8 +300,6 @@
 
     @deprecated('[3.19] explicitly use RepoAccess object in test instead')
     def set_cnx(self, cnx):
-        """ """
-        # XXX we want to deprecate this
         assert getattr(cnx, '_session', None) is not None
         if cnx is self._admin_clt_cnx:
             self._pop_custom_cnx()
@@ -441,7 +441,6 @@
         finally:
             self.session.set_cnxset() # ensure cnxset still set after commit
 
-    requestcls = fake.FakeRequest
     @deprecated('[3.19] explicitly use RepoAccess object in test instead')
     def request(self, rollbackfirst=False, url=None, headers={}, **kwargs):
         """return a web ui request"""