from cherrypy import _cphttptools
import cherrypy
import unittest
import cStringIO as StringIO
from turbogears import controllers

def createRequest(request, method="GET", protocol="HTTP/1.1", 
    headers={}, rfile=None, clientAddress="127.0.0.1",
    remoteHost="localhost", scheme="http"):
    if not rfile:
        rfile = StringIO.StringIO("")
    headerList = [(key, value) for key, value in headers.items()]
    cherrypy.request.purge__()
    cherrypy.response.purge__()
    _cphttptools.Request(clientAddress, remoteHost,
        " ".join((method, request, protocol)), headerList, rfile, scheme)

def _return_directly(tg_format, output, template):
    return output

def call(method, *args, **kw):
    orig_proc_output = controllers._process_output
    controllers._process_output = _return_directly
    try:
        return method(*args, **kw)
    finally:
        controllers._process_output = orig_proc_output
    
__all__ = ["createRequest", "call"]