Ticket #5527: util.py

File util.py, 855 bytes (added by tom.prince, 2 years ago)

twisted/web/test/util.py - (silly svn)

Line 
1# Copyright (c) Twisted Matrix Laboratories.
2# See LICENSE for details.
3
4"""
5General helpers for L{twisted.web} unit tests.
6"""
7
8from cStringIO import StringIO
9
10from twisted.internet.defer import succeed
11from twisted.web import server
12from twisted.trial.unittest import TestCase
13
14def render(resource, request):
15    written = StringIO()
16    request.write = written.write
17    result = resource.render(request)
18    if isinstance(result, str):
19        request.write(result)
20        request.finish()
21        return succeed(written.getvalue())
22    elif result is server.NOT_DONE_YET:
23        if request.finished:
24            return succeed(written.getvalue())
25        else:
26            d = request.notifyFinish()
27            d.addCallback(lambda _: written.getvalue())
28            return d
29    else:
30        raise ValueError("Unexpected return value: %r" % (result,))