[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.

authorDavid Douard <david.douard@logilab.fr>
changeset7b63e5cd0400
branchdefault
phasedraft
hiddenyes
parent revision#48f0ff3e2a32 [wsgi] make sure request.content is available for consumption
child revision#0e848e3f2404 [web] implement cross origin resource sharing (CORS) (closes #2491768), #8c5265480d0d [web] implement cross origin resource sharing (CORS) (closes #2491768)
files modified by this revision
devtools/fake.py
devtools/testlib.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1394636564 -3600
# Wed Mar 12 16:02:44 2014 +0100
# Node ID 7b63e5cd0400f9b20c3bbfb3d3364cb51563fdbf
# Parent 48f0ff3e2a32982ab6881ff81e6561079f13762b
[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.

diff --git a/devtools/fake.py b/devtools/fake.py
@@ -86,24 +86,24 @@
1          if includeparams:
2              return url
3          return url.split('?', 1)[0]
4 
5      def set_request_header(self, header, value, raw=False):
6 -        """set an incoming HTTP header (For test purpose only)"""
7 +        """set an incoming HTTP header (for test purpose only)"""
8          if isinstance(value, basestring):
9              value = [value]
10 -        if raw: #
11 +        if raw:
12              # adding encoded header is important, else page content
13              # will be reconverted back to unicode and apart unefficiency, this
14              # may cause decoding problem (e.g. when downloading a file)
15              self._headers_in.setRawHeaders(header, value)
16 -        else: #
17 +        else:
18              self._headers_in.setHeader(header, value) #
19 
20      def get_response_header(self, header, default=None, raw=False):
21 -        """return output header (For test purpose only"""
22 -        if raw: #
23 +        """return output header (for test purpose only)"""
24 +        if raw:
25              return self.headers_out.getRawHeaders(header, [default])[0]
26          return self.headers_out.getHeader(header, default)
27 
28      def build_url_params(self, **kwargs):
29          # overriden to get predictable resultts
diff --git a/devtools/testlib.py b/devtools/testlib.py
@@ -225,19 +225,20 @@
30          """Context manager returning a client side connection for the user"""
31          with repoapi.ClientConnection(self._session) as cnx:
32              yield cnx
33 
34      @ contextmanager
35 -    def web_request(self, url=None, headers={}, **kwargs):
36 +    def web_request(self, url=None, headers={}, method='GET', **kwargs):
37          """Context manager returning a web request pre-linked to a client cnx
38 
39          To commit and rollback use::
40 
41              req.cnx.commit()
42              req.cnx.rolback()
43          """
44 -        req = self.requestcls(self._repo.vreg, url=url, headers=headers, form=kwargs)
45 +        req = self.requestcls(self._repo.vreg, url=url, headers=headers,
46 +                              method=method, form=kwargs)
47          clt_cnx = repoapi.ClientConnection(self._session)
48          req.set_cnx(clt_cnx)
49          with clt_cnx:
50              yield req
51 
@@ -267,10 +268,11 @@
52      * `admpassword`, password of the admin user
53      * `shell`, create and use shell environment
54      """
55      appid = 'data'
56      configcls = devtools.ApptestConfiguration
57 +    requestcls = fake.FakeRequest
58      tags = TestCase.tags | Tags('cubicweb', 'cw_repo')
59      test_db_id = DEFAULT_EMPTY_DB_ID
60      _cnxs = set() # establised connection
61                    # stay on connection for leak detection purpose
62 
@@ -296,12 +298,10 @@
63          while self._open_access:
64              self._open_access.pop().close()
65 
66      @deprecated('[3.19] explicitly use RepoAccess object in test instead')
67      def set_cnx(self, cnx):
68 -        """ """
69 -        # XXX we want to deprecate this
70          assert getattr(cnx, '_session', None) is not None
71          if cnx is self._admin_clt_cnx:
72              self._pop_custom_cnx()
73          else:
74              self._cnxs.add(cnx) # register the cnx to make sure it is removed
@@ -439,11 +439,10 @@
75          except dbapi.ProgrammingError:
76              pass # connection closed
77          finally:
78              self.session.set_cnxset() # ensure cnxset still set after commit
79 
80 -    requestcls = fake.FakeRequest
81      @deprecated('[3.19] explicitly use RepoAccess object in test instead')
82      def request(self, rollbackfirst=False, url=None, headers={}, **kwargs):
83          """return a web ui request"""
84          if rollbackfirst:
85              self.cnx.rollback()