Add CubicWebRequestBase.content (closes #2742453)
authorJulien Cristau <julien.cristau@logilab.fr>
Tue, 12 Mar 2013 12:34:07 +0100
changeset 8752 e19f4bba89cd
parent 8751 1a97d46701e7
child 8759 cd68cd879def
Add CubicWebRequestBase.content (closes #2742453) This is the body of the HTTP request (stream)
etwist/request.py
etwist/test/data/views.py
etwist/test/unittest_server.py
web/request.py
wsgi/request.py
--- a/etwist/request.py	Thu Jan 17 14:55:07 2013 +0100
+++ b/etwist/request.py	Tue Mar 12 12:34:07 2013 +0100
@@ -39,6 +39,7 @@
                 self.form[key] = (name, stream)
             else:
                 self.form[key] = (unicode(name, self.encoding), stream)
+        self.content = self._twreq.content # stream
 
     def http_method(self):
         """returns 'POST', 'GET', 'HEAD', etc."""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etwist/test/data/views.py	Tue Mar 12 12:34:07 2013 +0100
@@ -0,0 +1,29 @@
+# copyright 2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+"""only for unit tests !"""
+
+from cubicweb.view import View
+from cubicweb.predicates import match_http_method
+
+class PutView(View):
+    __regid__ = 'put'
+    __select__ = match_http_method('PUT')
+    binary = True
+
+    def call(self):
+        self.w(self._cw.content.read())
--- a/etwist/test/unittest_server.py	Thu Jan 17 14:55:07 2013 +0100
+++ b/etwist/test/unittest_server.py	Tue Mar 12 12:34:07 2013 +0100
@@ -19,6 +19,7 @@
 import os, os.path as osp, glob
 
 from cubicweb.devtools.testlib import CubicWebTC
+from cubicweb.devtools.httptest import CubicWebServerTC
 from cubicweb.etwist.server import host_prefixed_baseurl
 
 
@@ -53,6 +54,13 @@
         self._check('http://localhost:8080/hg/', 'code.cubicweb.org',
                     'http://localhost:8080/hg/')
 
+
+class ETwistHTTPTC(CubicWebServerTC):
+    def test_put_content(self):
+        body = 'hop'
+        response = self.web_request('?vid=put', method='PUT', body=body)
+        self.assertEqual(body, response.body)
+
 if __name__ == '__main__':
     from logilab.common.testlib import unittest_main
     unittest_main()
--- a/web/request.py	Thu Jan 17 14:55:07 2013 +0100
+++ b/web/request.py	Tue Mar 12 12:34:07 2013 +0100
@@ -23,6 +23,7 @@
 import random
 import base64
 import urllib
+from StringIO import StringIO
 from hashlib import sha1 # pylint: disable=E0611
 from Cookie import SimpleCookie
 from calendar import timegm
@@ -114,6 +115,8 @@
             self._headers_in.addRawHeader(k, v)
         #: form parameters
         self.setup_params(form)
+        #: received body
+        self.content = StringIO()
         #: dictionary that may be used to store request data that has to be
         #: shared among various components used to publish the request (views,
         #: controller, application...)
--- a/wsgi/request.py	Thu Jan 17 14:55:07 2013 +0100
+++ b/wsgi/request.py	Tue Mar 12 12:34:07 2013 +0100
@@ -38,13 +38,14 @@
 
 
 class CubicWebWsgiRequest(CubicWebRequestBase):
-    """most of this code COMES FROM DJANO
+    """most of this code COMES FROM DJANGO
     """
 
     def __init__(self, environ, vreg):
         self.environ = environ
         self.path = environ['PATH_INFO']
         self.method = environ['REQUEST_METHOD'].upper()
+        self.content = environ['wsgi.input']
 
         headers_in = dict((normalize_header(k[5:]), v) for k, v in self.environ.items()
                           if k.startswith('HTTP_'))