def test_middleware_multiple_changes(self): class MyHandler(RequestHandler): middleware = [SessionMiddleware(), i18n.I18nMiddleware()] def get(self, **kwargs): locale = self.i18n.locale return Response(locale) app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler) ], config={ 'tipfy.sessions': { 'secret_key': 'secret', }, 'tipfy.i18n': { 'locale_request_lookup': [('args', 'lang'), ('session', '_locale')], } }) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'en_US') response = client.get('/?lang=pt_BR') self.assertEqual(response.data, 'pt_BR') response = client.get('/') self.assertEqual(response.data, 'pt_BR') response = client.get('/?lang=en_US') self.assertEqual(response.data, 'en_US') response = client.get('/') self.assertEqual(response.data, 'en_US')
def test_handle_exception_2(self): res = 'I fixed it!' class MyMiddleware(object): def handle_exception(self, handler, exception): raise ValueError() class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): raise ValueError() class ErrorHandler(RequestHandler): def handle_exception(self, exception): return Response(res) app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ], debug=False) app.error_handlers[500] = ErrorHandler client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, res)
def test_middleware_multiple_changes(self): class MyHandler(RequestHandler): middleware = [SessionMiddleware(), i18n.I18nMiddleware()] def get(self, **kwargs): locale = self.i18n.locale return Response(locale) app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler) ], config={ 'tipfy.sessions': { 'secret_key': 'secret', }, 'tipfy.i18n': { 'locale_request_lookup': [('args', 'lang'), ('session', '_locale')], } }) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'en_US') response = client.get('/?lang=pt_BR') self.assertEqual(response.data, 'pt_BR') response = client.get('/') self.assertEqual(response.data, 'pt_BR') response = client.get('/?lang=en_US') self.assertEqual(response.data, 'en_US') response = client.get('/') self.assertEqual(response.data, 'en_US')
def test_redirect_relative_uris(self): class MyHandler(RequestHandler): def get(self): return self.redirect(self.request.args.get('redirect')) app = Tipfy(rules=[ Rule('/foo/bar', name='test1', handler=MyHandler), Rule('/foo/bar/', name='test2', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/foo/bar/', query_string={'redirect': '/baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/baz') response = client.get('/foo/bar/', query_string={'redirect': './baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/foo/bar/baz') response = client.get('/foo/bar/', query_string={'redirect': '../baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz') response = client.get('/foo/bar', query_string={'redirect': '/baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/baz') response = client.get('/foo/bar', query_string={'redirect': './baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz') response = client.get('/foo/bar', query_string={'redirect': '../baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/baz')
def test_abort(self): class HandlerWith400(RequestHandler): def get(self, **kwargs): self.abort(400) class HandlerWith403(RequestHandler): def post(self, **kwargs): self.abort(403) class HandlerWith404(RequestHandler): def put(self, **kwargs): self.abort(404) app = Tipfy(rules=[ Rule('/400', endpoint='400', handler=HandlerWith400), Rule('/403', endpoint='403', handler=HandlerWith403), Rule('/404', endpoint='404', handler=HandlerWith404), ], debug=True) client = app.get_test_client() response = client.get('/400') self.assertEqual(response.status_code, 400) response = client.post('/403') self.assertEqual(response.status_code, 403) response = client.put('/404') self.assertEqual(response.status_code, 404)
def test_custom_error_handlers(self): class HomeHandler(RequestHandler): def get(self): return Response('') app = Tipfy([ Rule('/', handler=HomeHandler, name='home'), Rule('/broken', handler=BrokenHandler, name='broken'), ], debug=False) app.error_handlers = { 404: Handle404, 405: Handle405, 500: Handle500, } client = app.get_test_client() res = client.get('/nowhere') self.assertEqual(res.status_code, 404) self.assertEqual(res.data, '404 custom handler') res = client.put('/') self.assertEqual(res.status_code, 405) self.assertEqual(res.data, '405 custom handler') self.assertEqual(res.headers.get('Allow'), 'GET') res = client.get('/broken') self.assertEqual(res.status_code, 500) self.assertEqual(res.data, '500 custom handler')
def test_handle_exception_2(self): res = 'I fixed it!' class MyMiddleware(object): def handle_exception(self, handler, exception): raise ValueError() class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): raise ValueError() class ErrorHandler(RequestHandler): def handle_exception(self, exception): return Response(res) app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ], debug=False) app.error_handlers[500] = ErrorHandler client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, res)
def test_custom_error_handlers(self): app = Tipfy([ Rule('/', handler=AllMethodsHandler, name='home'), Rule('/broken', handler=BrokenHandler, name='broken'), ], debug=False) app.error_handlers = { 404: Handle404, 405: Handle405, 500: Handle500, } client = app.get_test_client() res = client.get('/nowhere') self.assertEqual(res.status_code, 404) self.assertEqual(res.data, '404 custom handler') res = client.put('/broken') self.assertEqual(res.status_code, 405) self.assertEqual(res.data, '405 custom handler') self.assertEqual(res.headers.get('Allow'), 'GET') res = client.get('/broken') self.assertEqual(res.status_code, 500) self.assertEqual(res.data, '500 custom handler')
def test_handler(self): rules = [ HandlerPrefix('resources.alternative_routing.', [ Rule('/', name='home', handler='HomeHandler'), Rule('/foo', name='home/foo', handler='HomeHandler:foo'), Rule('/bar', name='home/bar', handler='HomeHandler:bar'), Rule('/other/foo', name='other/foo', handler='OtherHandler:foo'), Rule('/other/bar', name='other/bar', handler='OtherHandler:bar'), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'home-get') response = client.get('/foo') self.assertEqual(response.data, 'home-foo') response = client.get('/bar') self.assertEqual(response.data, 'home-bar') response = client.get('/other/foo') self.assertEqual(response.data, 'other-foo') response = client.get('/other/bar') self.assertEqual(response.data, 'other-bar')
def test_redirect_relative_uris(self): class MyHandler(RequestHandler): def get(self): return self.redirect(self.request.args.get('redirect')) app = Tipfy(rules=[ Rule('/foo/bar', name='test1', handler=MyHandler), Rule('/foo/bar/', name='test2', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/foo/bar/', query_string={'redirect': '/baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/baz') response = client.get('/foo/bar/', query_string={'redirect': './baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/foo/bar/baz') response = client.get('/foo/bar/', query_string={'redirect': '../baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz') response = client.get('/foo/bar', query_string={'redirect': '/baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/baz') response = client.get('/foo/bar', query_string={'redirect': './baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz') response = client.get('/foo/bar', query_string={'redirect': '../baz'}) self.assertEqual(response.headers['Location'], 'http://localhost/baz')
def test_json(self): class JsonHandler(RequestHandler): def get(self, **kwargs): return Response(self.request.json['foo']) app = Tipfy(rules=[ Rule('/', name='home', handler=JsonHandler), ], debug=True) data = json_encode({'foo': 'bar'}) client = app.get_test_client() response = client.get('/', content_type='application/json', data=data) self.assertEqual(response.data, 'bar')
def test_404(self): """No URL rules defined.""" app = Tipfy() client = app.get_test_client() # Normal mode. response = client.get('/') self.assertEqual(response.status_code, 404) # Debug mode. app.debug = True response = client.get('/') self.assertEqual(response.status_code, 404)
def test_501(self): """Method is not in app.allowed_methods.""" app = Tipfy() client = app.get_test_client() # Normal mode. response = client.open('/', method='CONNECT') self.assertEqual(response.status_code, 501) # Debug mode. app.debug = True response = client.open('/', method='CONNECT') self.assertEqual(response.status_code, 501)
def test_404(self): """No URL rules defined.""" app = Tipfy() client = app.get_test_client() # Normal mode. response = client.get('/') self.assertEqual(response.status_code, 404) # Debug mode. app.debug = True response = client.get('/') self.assertEqual(response.status_code, 404)
def test_501(self): """Method is not in app.allowed_methods.""" app = Tipfy() client = app.get_test_client() # Normal mode. response = client.open('/', method='CONNECT') self.assertEqual(response.status_code, 501) # Debug mode. app.debug = True response = client.open('/', method='CONNECT') self.assertEqual(response.status_code, 501)
def test_redirect_empty(self): class HandlerWithRedirect(RequestHandler): def get(self, **kwargs): return self.redirect('/', empty=True) app = Tipfy(rules=[ Rule('/', name='home', handler=AllMethodsHandler), Rule('/redirect-me', name='redirect', handler=HandlerWithRedirect), ]) client = app.get_test_client() response = client.get('/redirect-me', follow_redirects=False) self.assertEqual(response.data, '')
def test_500(self): """Handler import will fail.""" app = Tipfy(rules=[Rule('/', name='home', handler='non.existent.handler')]) client = app.get_test_client() # Normal mode. response = client.get('/') self.assertEqual(response.status_code, 500) # Debug mode. app.debug = True app.config['tipfy']['enable_debugger'] = False self.assertRaises(ImportError, client.get, '/')
def test_redirect_to_empty(self): class HandlerWithRedirectTo(RequestHandler): def get(self, **kwargs): return self.redirect_to('home', _empty=True) app = Tipfy(rules=[ Rule('/', name='home', handler=AllMethodsHandler), Rule('/redirect-me', name='redirect', handler=HandlerWithRedirectTo), ]) client = app.get_test_client() response = client.get('/redirect-me', follow_redirects=False) self.assertEqual(response.data, '')
def test_405_debug(self): class HomeHandler(RequestHandler): pass app = Tipfy(rules=[ Rule('/', endpoint='home', handler=HomeHandler), ], debug=True) client = app.get_test_client() for method in ALLOWED_METHODS: response = client.open('/', method=method) self.assertEqual(response.status_code, 405)
def test_500(self): """Handler import will fail.""" app = Tipfy( rules=[Rule('/', name='home', handler='non.existent.handler')]) client = app.get_test_client() # Normal mode. response = client.get('/') self.assertEqual(response.status_code, 500) # Debug mode. app.debug = True app.config['tipfy']['enable_debugger'] = False self.assertRaises(ImportError, client.get, '/')
def test_json(self): class JsonHandler(RequestHandler): def get(self, **kwargs): return Response(self.request.json['foo']) app = Tipfy(rules=[ Rule('/', name='home', handler=JsonHandler), ], debug=True) data = json_encode({'foo': 'bar'}) client = app.get_test_client() response = client.get('/', content_type='application/json', data=data) self.assertEqual(response.data, 'bar')
def test_handle_exception(self): app = Tipfy([ Rule('/', handler=AllMethodsHandler, name='home'), Rule('/broken', handler=BrokenHandler, name='broken'), Rule('/broken-but-fixed', handler=BrokenButFixedHandler, name='broken-but-fixed'), ], debug=False) client = app.get_test_client() response = client.get('/broken') self.assertEqual(response.status_code, 500) response = client.get('/broken-but-fixed') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'That was close!')
class TestBaseModel(unittest.TestCase): #--------------------------------------------------------------------------- def setUp(self): datastore_stub = apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map[ 'datastore_v3' ] datastore_stub.Clear() self.app = Tipfy() self.client = self.app.get_test_client() # plans self.plan_active = models.Plan( name='Plan Active', plan_key='ACTIVE', is_active=True, default=False ) self.plan_active.put() self.plan_inactive = models.Plan( name='Plan InActive', plan_key='INACTIVE', is_active=False, default=False ) self.plan_inactive.put() self.plan_default = models.Plan( name='Plan Default', plan_key='DEFAULT', is_active=True, default=True ) self.plan_default.put() # accounts self.user_1 = User( username='******', password='******', session_id='1', auth_id='own|user_1' ) self.user_1.put() self.user_2 = User( username='******', password='******', session_id='2', auth_id='own|user_2' ) self.user_2.put() self.account_active = models.Account.get_or_insert_plan(user=self.user_1)
def test_handler_prefix(self): rules = [ HandlerPrefix('resources.handlers.', [ Rule('/', name='home', handler='HomeHandler'), Rule('/defaults', name='defaults', handler='HandlerWithRuleDefaults', defaults={'foo': 'bar'}), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'Hello, World!') response = client.get('/defaults') self.assertEqual(response.data, 'bar')
def test_handle_exception(self): app = Tipfy([ Rule('/', handler=AllMethodsHandler, name='home'), Rule('/broken', handler=BrokenHandler, name='broken'), Rule('/broken-but-fixed', handler=BrokenButFixedHandler, name='broken-but-fixed'), ], debug=False) client = app.get_test_client() response = client.get('/broken') self.assertEqual(response.status_code, 500) response = client.get('/broken-but-fixed') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'That was close!')
def test_handle_exception_3(self): class MyMiddleware(object): def handle_exception(self, handler, exception): pass class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): raise ValueError() app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.status_code, 500)
def test_redirect(self): class HomeHandler(RequestHandler): def get(self, **kwargs): return Response('Home sweet home!') class HandlerWithRedirect(RequestHandler): def get(self, **kwargs): return self.redirect('/') app = Tipfy(rules=[ Rule('/', endpoint='home', handler=HomeHandler), Rule('/redirect-me', endpoint='redirect', handler=HandlerWithRedirect), ], debug=True) client = app.get_test_client() response = client.get('/redirect-me', follow_redirects=True) self.assertEqual(response.data, 'Home sweet home!')
def test_200(self): class MyHandler(RequestHandler): def delete(self, **kwargs): return Response('Method: %s' % self.request.method) get = head = options = post = put = trace = delete app = Tipfy(rules=[Rule('/', name='home', handler=MyHandler)]) client = app.get_test_client() for method in ALLOWED_METHODS: response = client.open('/', method=method) self.assertEqual(response.status_code, 200, method) if method == 'HEAD': self.assertEqual(response.data, '') else: self.assertEqual(response.data, 'Method: %s' % method)
def test_handle_exception_3(self): class MyMiddleware(object): def handle_exception(self, handler, exception): pass class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): raise ValueError() app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.status_code, 500)
def test_200(self): app = Tipfy(rules=[Rule('/', name='home', handler=AllMethodsHandler)]) client = app.get_test_client() for method in app.allowed_methods: response = client.open('/', method=method) self.assertEqual(response.status_code, 200, method) if method == 'HEAD': self.assertEqual(response.data, '') else: self.assertEqual(response.data, 'Method: %s' % method) # App Engine mode. self._set_dev_server_flag(True) response = client.get('/') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'Method: GET')
def test_200(self): app = Tipfy(rules=[Rule('/', name='home', handler=AllMethodsHandler)]) client = app.get_test_client() for method in app.allowed_methods: response = client.open('/', method=method) self.assertEqual(response.status_code, 200, method) if method == 'HEAD': self.assertEqual(response.data, '') else: self.assertEqual(response.data, 'Method: %s' % method) # App Engine mode. self._set_dev_server_flag(True) response = client.get('/') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'Method: GET')
def test_regex_converter(self): class TestHandler(RequestHandler): def get(self, **kwargs): return Response(kwargs.get('path')) app = Tipfy([ Rule('/<regex(".*"):path>', name='home', handler=TestHandler), ]) client = app.get_test_client() response = client.get('/foo') self.assertEqual(response.data, 'foo') response = client.get('/foo/bar') self.assertEqual(response.data, 'foo/bar') response = client.get('/foo/bar/baz') self.assertEqual(response.data, 'foo/bar/baz')
def test_function_handler(self): rules = [ HandlerPrefix('resources.alternative_routing.', [ Rule('/', name='home', handler='home'), Rule('/foo', name='home/foo', handler='foo'), Rule('/bar', name='home/bar', handler='bar'), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'home') response = client.get('/foo') self.assertEqual(response.data, 'foo') response = client.get('/bar') self.assertEqual(response.data, 'bar')
def test_abort(self): class HandlerWithAbort(RequestHandler): def get(self, **kwargs): self.abort(kwargs.get('status_code')) app = Tipfy(rules=[ Rule('/<int:status_code>', name='abort-me', handler=HandlerWithAbort), ]) client = app.get_test_client() response = client.get('/400') self.assertEqual(response.status_code, 400) response = client.get('/403') self.assertEqual(response.status_code, 403) response = client.get('/404') self.assertEqual(response.status_code, 404)
def test_regex_converter(self): class TestHandler(RequestHandler): def get(self, **kwargs): return Response(kwargs.get('path')) app = Tipfy([ Rule('/<regex(".*"):path>', name='home', handler=TestHandler), ]) client = app.get_test_client() response = client.get('/foo') self.assertEqual(response.data, 'foo') response = client.get('/foo/bar') self.assertEqual(response.data, 'foo/bar') response = client.get('/foo/bar/baz') self.assertEqual(response.data, 'foo/bar/baz')
def test_function_handler(self): rules = [ HandlerPrefix('resources.alternative_routing.', [ Rule('/', name='home', handler='home'), Rule('/foo', name='home/foo', handler='foo'), Rule('/bar', name='home/bar', handler='bar'), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'home') response = client.get('/foo') self.assertEqual(response.data, 'foo') response = client.get('/bar') self.assertEqual(response.data, 'bar')
def test_before_dispatch(self): res = 'Intercepted!' class MyMiddleware(object): def before_dispatch(self, handler): return Response(res) class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): return Response('default') app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, res)
def test_handler_prefix(self): rules = [ HandlerPrefix('resources.handlers.', [ Rule('/', name='home', handler='HomeHandler'), Rule('/defaults', name='defaults', handler='HandlerWithRuleDefaults', defaults={'foo': 'bar'}), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'Hello, World!') response = client.get('/defaults') self.assertEqual(response.data, 'bar')
def test_handle_exception(self): res = 'Catched!' class MyMiddleware(object): def handle_exception(self, handler, exception): return Response(res) class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): raise ValueError() app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, res)
def test_before_dispatch(self): res = 'Intercepted!' class MyMiddleware(object): def before_dispatch(self, handler): return Response(res) class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): return Response('default') app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, res)
def test_handle_exception(self): res = 'Catched!' class MyMiddleware(object): def handle_exception(self, handler, exception): return Response(res) class MyHandler(RequestHandler): middleware = [MyMiddleware()] def get(self, **kwargs): raise ValueError() app = Tipfy(rules=[ Rule('/', name='home', handler=MyHandler), ]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, res)
def test_abort(self): class HandlerWithAbort(RequestHandler): def get(self, **kwargs): self.abort(kwargs.get('status_code')) app = Tipfy(rules=[ Rule('/<int:status_code>', name='abort-me', handler=HandlerWithAbort), ]) client = app.get_test_client() response = client.get('/400') self.assertEqual(response.status_code, 400) response = client.get('/403') self.assertEqual(response.status_code, 403) response = client.get('/404') self.assertEqual(response.status_code, 404)
def test_handle_exception(self): class HandlerWithValueError(RequestHandler): def get(self, **kwargs): raise ValueError() class HandlerWithNotImplementedError(RequestHandler): def get(self, **kwargs): raise NotImplementedError() def handle_exception(self, exception=None, debug=True): return Response('I fixed it!') app = Tipfy(rules=[ Rule('/value-error', endpoint='value-error', handler=HandlerWithValueError), Rule('/not-implemented-error', endpoint='not-implemented-error', handler=HandlerWithNotImplementedError), ], debug=True) client = app.get_test_client() self.assertRaises(ValueError, client.get, '/value-error') response = client.get('/not-implemented-error') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'I fixed it!')
def test_handler(self): rules = [ HandlerPrefix('resources.alternative_routing.', [ Rule('/', name='home', handler='HomeHandler'), Rule('/foo', name='home/foo', handler='HomeHandler:foo'), Rule('/bar', name='home/bar', handler='HomeHandler:bar'), Rule('/other/foo', name='other/foo', handler='OtherHandler:foo'), Rule('/other/bar', name='other/bar', handler='OtherHandler:bar'), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'home-get') response = client.get('/foo') self.assertEqual(response.data, 'home-foo') response = client.get('/bar') self.assertEqual(response.data, 'home-bar') response = client.get('/other/foo') self.assertEqual(response.data, 'other-foo') response = client.get('/other/bar') self.assertEqual(response.data, 'other-bar')
def test_404_debug(self): # No URL rules defined. app = Tipfy(debug=True) client = app.get_test_client() response = client.get('/') self.assertEqual(response.status_code, 404)
def test_500(self): # Handler import will fail. app = Tipfy(rules=[Rule('/', name='home', handler='non.existent.handler')]) client = app.get_test_client() response = client.get('/') self.assertEqual(response.status_code, 500)
def test_500_debug(self): # Handler import will fail. app = Tipfy(rules=[Rule('/', name='home', handler='non.existent.handler')], debug=True) client = app.get_test_client() self.assertRaises(ImportError, client.get, '/')
def test_501_debug(self): # Method is not in ALLOWED_METHODS. app = Tipfy(debug=True) client = app.get_test_client() response = client.open('/', method='CONNECT') self.assertEqual(response.status_code, 501)
def setUp(self): app = Tipfy(rules=[Rule('/', endpoint='home', handler=HelloWorldHandler)]) self.client = app.get_test_client() gae_test_base.APP_ID = default_config['app_id']