def setUp(self): context = pybald.configure(config_file="tests/sample_project/project.py") def map(urls): urls.connect("home", r"/") self.app = Router(routes=map, controllers=[])
def test_404(self): "Return a 404 response" pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[SampleController]) app = ErrorMiddleware(app) try: resp = Request.blank('/not_there').get_response(app) except Exception as err: self.fail("Exception Generated or Fell Through Error Handler {0}".format(err)) self.assertEqual(resp.status_code, 404) self.assertEqual(str(resp.text), not_found_response)
def test_stack_trace(self): "When in debug mode, throw an Exception and generate a stack trace" pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[SampleController]) app = ErrorMiddleware(app) try: resp = Request.blank('/throw_exception').get_response(app) except Exception as err: self.fail("Exception Generated or Fell Through Error Handler {0}".format(err)) self.assertEqual(resp.status_code, 500) self.assertEqual(stack_trace_head, str(resp.text)[:len(stack_trace_head)])
def test_url_param_injection(self): '''Test that rendering a url parameter escapes any dangerous html''' pybald.configure(config_object=test_conf) app = Router(routes=map, controllers=[XssController]) app = ErrorMiddleware(app) resp = Request.blank( '/test_xss_url_param?url_parameter=%3C/script%3E%3Cscript%3Ealert%28document.cookie%29%3C/script%3E' ).get_response(app) self.assertEqual( resp.body, b'\n<div></script><script>alert(document.cookie)</script></div>\n' )
def test_non_stack_trace(self): "When *NOT* in debug mode, throw an Exception and return a generic error" test_w_debug_conf = test_conf.copy() test_w_debug_conf.update(dict(debug=False)) pybald.configure(config_object=test_w_debug_conf) app = Router(routes=map, controllers=[SampleController]) app = ErrorMiddleware(app) try: resp = Request.blank('/throw_exception').get_response(app) except Exception as err: self.fail("Exception Generated or Fell Through Error Handler {0}".format(err)) self.assertEqual(resp.status_code, 500) self.assertEqual(str(resp.text), general_fault_response)
def setUp(self): pybald.configure(config_object={'debug': False}) def map(urls): urls.connect('test1', r'/test1', controller='test1') urls.connect('test2', r'/test2', controller='test2', action='index') urls.connect('method_test1', r'/method', controller='test2', action='get', conditions=dict(method=["GET"])) urls.connect('method_test2', r'/method', controller='test2', action='delete', conditions=dict(method=["DELETE"])) # test3 is a 404 missing url urls.connect('test4', r'/test4', controller='test4', action='index') urls.connect('test5', r'/test5', controller='test2', action='_iminvalid') urls.redirect('/here', '/there', _redirect_code='302 Found') class Test1Controller(object): def index(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test1"] class Test2Controller(object): def index(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test2"] def delete(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test2_delete"] def get(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test2_get"] def _iminvalid(self, environ, start_response): start_response('200 OK', [('Content-Type','text/plain')]) return ["test5_invalid_action"] self.app = Router(routes=map, controllers=[Test1Controller, Test2Controller])
from pybald.core.db_middleware import DbMiddleware # load the error controller so that the ErrorMiddleware # has somewhere to route the request if an error occurs from jotbits.app.controllers.error_controller import ErrorController # from quiz_site.app.models.session import Session # from quiz_site.app.models.user import User # The main application pipeline # Include all WSGI middleware here. The order of # web transaction will flow from the bottom of this list # to the top, and then back out. The pybald Router # should usually be the first item listed. # ---------------------------------- app = Router(routes=my_project.app.urls.map) # app = UserManager(app, user_class=User) # app = SessionManager(app, session_class=Session) app = DbMiddleware(app) app = ErrorMiddleware(app, error_controller=ErrorController) # ---------------------------------- # ↑↑↑ ↓↓↓ # ↑↑↑ ↓↓↓ # Request Response # apache / mod_wsgi looks for 'application' application = app # called directly enable the interactive debugger # requires python paste be installed
class BucketlistController(Controller): @action def list(self, req): return "Bucketlist:list logic here!" @action def create(self, req): return "Bucketlist:create logic here!" @action def detail(self, req): return "Bucketlist:detail for bucketlist ID {} here!".format( "req.id") @action def update(self, req): return "Bucketlist:update for bucketlist ID {} here!".format( "req.id") @action def destroy(self, req): return "Bucketlist:destroy for bucketlist ID {} here!".format( "req.id") app = Router(routes=map, controllers=[UserController, BucketlistController]) if __name__ == "__main__": context.start(app)
def map(urls): urls.connect('home', r'/', controller='sample') urls.connect('variable_test', r'/variable_test/{variable}', controller='sample', action='variable_test') # errors urls.connect('throw_exception', r'/throw_exception', controller='sample', action='throw_exception') class SampleModel(models.Model): text = models.Column(models.Text) class SampleController(Controller): @action def index(self, req): self.sample_variable = "Testing" @action def throw_exception(self, req): raise Exception("This is a test exception") app = Router(routes=map, controllers=context.controller_registry) app = ErrorMiddleware(app) if __name__ == "__main__": context.start(app)