from paste.fixture import TestApp from rhubarbtart import TartRootController, WSGIApp, request, response try: import cherrypy except ImportError: disabled = True cherrypy = None #This object contains one URL for each attribute in request class FakeCherryPy(TartRootController): """Each function in is designed to test the request variable it has the same name as""" def index(self): if cherrypy is None: return 'cannot test' # We can't check the actual identity of the threadlocal # objects, but we can check the identify of an attribute: assert id(cherrypy.request.environ) == id(request.environ) assert id(cherrypy.response.simpleCookie) == id(response.simpleCookie) return 'ok' index.exposed = True def configget(self): if cherrypy is None: return 'cannot test' assert cherrypy.config.get('in_testing') return 'ok' configget.exposed = True def redir(self): raise cherrypy.HTTPRedirect('/notfound') redir.exposed = True def notfound(self): raise cherrypy.NotFound() notfound.exposed = True wsgi_app = FakeCherryPy(fake_cherrypy=True) test_app = TestApp(wsgi_app) def test_index(): res = test_app.get('/') def test_fails(): test_app = TestApp(FakeCherryPy()) try: res = test_app.get('/') except (AssertionError, AttributeError): pass else: print res assert 0, "Last test should fail" def test_config(): extra_environ = {'paste.config': {'in_testing': True}} res = test_app.get('/configget', extra_environ=extra_environ) def test_redir(): res = test_app.get('/redir') assert res.status == 302 for name, value in res.headers: if name.lower() == 'location': assert value == 'http://localhost/notfound' break else: assert 0, "No location header: %r" % res.headers def test_notfound(): res = test_app.get('/notfound', status=404) res.mustcontain("The path '/notfound' was not found")