[Product-Developers] plone.testing: how to create a POST request containing a file
Sean Upton
sdupton at gmail.com
Fri May 11 16:33:54 UTC 2012
On Fri, May 11, 2012 at 8:02 AM, Kees Hink <keeshink at gmail.com> wrote:
> Progress: Using zope.publisher.browser.TestRequest is probably the way to
> go:
>
> >>> request = TestRequest(data = xmlfile.read())
> >>> request.method = 'POST'
AFAICT, you can still do functional testing of a POST using
zope.testrbrowser from plone.app.testing. This should be documented.
But for integration tests of the same thing (e.g. a browser view), you
do need a simulated request.
There are some reasons to consider using the native request/response
objects from Zope2 -- I cannot remember at the moment what they are,
but I think if you want a request to test both legacy skin code and
browser views as well, this is your better bet. This is similar to
what I typically do for simulated requests in integration testing (you
could use a StringIO for the POST body):
import sys
from StringIO import StringIO
from zope.interface import alsoProvides
from zope.publisher.browser import setDefaultSkin
from z3c.form.interfaces import IFormLayer
from ZPublisher.HTTPResponse import HTTPResponse
from ZPublisher.HTTPRequest import HTTPRequest
from my.product import IMyProductLayer # browser layer
def test_request(url=None, method='GET', streamin=sys.stdin,
streamout=sys.stdout):
"""
make request suitable for browser views and Zope2 security.
"""
response = HTTPResponse(stdout=streamout)
request = HTTPRequest(streamin,
{'SERVER_NAME' : 'localhost',
'SERVER_PORT' : '80',
'REQUEST_METHOD' : method, },
response)
request['ACTUAL_URL'] = url or 'http://nohost/plone/myform'
setDefaultSkin(request)
alsoProvides(request, IFormLayer) # suitable for testing z3c.form views
alsoProvides(request, IMyProductLayer) # product layer
return request
xml = '<this><that /></this>'
request = test_request(method='POST', streamin=StringIO(xml))
Sean
(all: feel free to use any of any code in this message under your
choice of MIT or GPL licenses).
More information about the Product-Developers
mailing list