class KobinHookTests(TestCase): def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello') @self.app.before_request def before(): self.before_counter += 1 @self.app.after_request def after(response): response.headers.add_header('Foo', 'Bar') return response def test_before_request(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} self.app.before_counter = 0 self.app._handle(test_env) self.assertEqual(self.before_counter, 1) def test_after_request(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} response = self.app._handle(test_env) self.assertIn(('Foo', 'Bar'), response.headerlist)
def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello') @self.app.before_request def before(): self.before_counter += 1 @self.app.before_request def before2(): self.before_counter += 1 @self.app.after_request def after(response): response.headers.add_header('Foo1', 'Bar1') return response @self.app.after_request def after2(response): response.headers.add_header('Foo2', 'Bar2') return response
def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None @self.app.route('/') def dummy_func(): return 'hello' @self.app.route('/test/{typed_id}') def typed_url_var(typed_id: int): return typed_id
def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello') @self.app.after_request def after_do_not_return_response(response): pass
def setUp(self): self.app = Kobin(config={'DEBUG': True}) self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello')
def test_constructor_headerlist_has_location(self): test_env = {'HTTP_HOST': 'localhost', 'SERVER_PROTOCOL': 'HTTP/1.1'} app = Kobin() app(test_env, lambda x, y: None) response = RedirectResponse('/hello') expected_content_type = ('Location', 'http://localhost/hello') self.assertIn(expected_content_type, response.headerlist)
def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None @self.app.route('/') def dummy_func(): return Response('hello') @self.app.route('/test/{typed_id}') def typed_url_var(typed_id: int): body = "type: {}, value: {}".format(type(typed_id), typed_id) return Response(body) @self.app.route('/test/raise500') def raise500(typed_id: int): 1 / 0 return Response("Don't reach here")
def test_lazy_reverse_router(self, app_mock): app = Kobin() @app.route('/', 'GET', 'top') def top(): return Response('Hello') app_mock.return_value = app actual = template_router_reverse('top') expected = '/' self.assertEqual(actual, expected)
def setup_models(app: Kobin) -> None: engine = create_engine(app.config.get("SQLALCHEMY_DATABASE_URI"), echo=True) Session = sessionmaker() Session.configure(bind=engine) session = Session() app.config["DB"] = { "METADATA": Base.metadata, "ENGINE": engine, "SESSION": session, }
class KobinTests(TestCase): def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None @self.app.route('/') def dummy_func(): return 'hello' @self.app.route('/test/{typed_id}') def typed_url_var(typed_id: int): return typed_id def test_route(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} actual = self.app._handle(test_env) expected = 'hello' self.assertEqual(actual, expected) def test_typed_url_var(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/10'} actual = self.app._handle(test_env) expected = 10 self.assertEqual(actual, expected) def test_404_not_found(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'} self.app._handle(test_env) actual = response._status_code expected = 404 self.assertEqual(actual, expected) def test_404_when_cast_error(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/not-integer'} self.app._handle(test_env) actual = response._status_code expected = 404 self.assertEqual(actual, expected) def test_handled_body_message_when_404_not_found(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'} actual = self.app._handle(test_env) expected = "Not found: /this_is_not_found" self.assertEqual(actual, expected) def test_wsgi(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} actual = self.app.wsgi(test_env, self.dummy_start_response) expected = [b'hello'] self.assertEqual(actual, expected)
class KobinHookTests(TestCase): def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello') @self.app.before_request def before(): self.before_counter += 1 @self.app.before_request def before2(): self.before_counter += 1 @self.app.after_request def after(response): response.headers.add_header('Foo1', 'Bar1') return response @self.app.after_request def after2(response): response.headers.add_header('Foo2', 'Bar2') return response def test_before_request(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} self.app.before_counter = 0 self.app._handle(test_env) self.assertEqual(self.before_counter, 2) def test_after_request(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} response = self.app._handle(test_env) self.assertIn(('Foo1', 'Bar1'), response.headerlist) self.assertIn(('Foo2', 'Bar2'), response.headerlist)
def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello') @self.app.before_request def before(): self.before_counter += 1 @self.app.after_request def after(response): response.headers.add_header('Foo', 'Bar') return response
class KobinAfterHookTests(TestCase): def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None self.before_counter = 0 @self.app.route('/') def dummy_func(): return Response('hello') @self.app.after_request def after_do_not_return_response(response): pass def test_after_request(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} response = self.app._handle(test_env) self.assertEqual('200 OK', response.status)
class KobinTests(TestCase): def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None @self.app.route('/') def dummy_func(): return Response('hello') @self.app.route('/test/{typed_id}') def typed_url_var(typed_id: int): body = "type: {}, value: {}".format(type(typed_id), typed_id) return Response(body) @self.app.route('/test/raise500') def raise500(typed_id: int): 1 / 0 return Response("Don't reach here") def test_route(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} response = self.app._handle(test_env) actual = response.body expected = [b'hello'] self.assertEqual(actual, expected) def test_typed_url_var(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/10'} response = self.app._handle(test_env) actual = response.body expected = [b"type: <class 'int'>, value: 10"] self.assertEqual(actual, expected) def test_404_not_found(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'} response = self.app._handle(test_env) actual = response._status_code expected = 404 self.assertEqual(actual, expected) def test_404_when_cast_error(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/not-integer'} response = self.app._handle(test_env) actual = response._status_code expected = 404 self.assertEqual(actual, expected) def test_response_status_when_500_raised(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/raise500'} response = self.app._handle(test_env) actual = response._status_code expected = 500 self.assertEqual(actual, expected) def test_response_body_when_500_raised_and_enable_debugging(self): self.app.config['DEBUG'] = True test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/raise500'} response = self.app._handle(test_env) actual = response.body expected = [b'Internal Server Error'] self.assertNotEqual(actual, expected) def test_response_body_when_500_raised_and_disable_debugging(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/raise500'} response = self.app._handle(test_env) actual = response.body expected = [b'Internal Server Error'] self.assertEqual(actual, expected) def test_handled_body_message_when_404_not_found(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'} response = self.app._handle(test_env) actual = response.body expected = [b"Not found: /this_is_not_found"] self.assertEqual(actual, expected) def test_wsgi(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} actual = self.app.wsgi(test_env, self.dummy_start_response) expected = [b'hello'] self.assertEqual(actual, expected)
def test_constructor_status_when_http11(self): test_env = {'HTTP_HOST': 'localhost', 'SERVER_PROTOCOL': 'HTTP/1.1'} app = Kobin() app(test_env, lambda x, y: None) response = RedirectResponse('/') self.assertEqual(response.status_code, 303)
from kobin import Kobin app = Kobin() @app.route('^/(?P<name>\w*)$') def hello(name: str) -> str: return "Hello {}!!".format(name) if __name__ == '__main__': app.run()
from kobin import Kobin, request, Response, JSONResponse app = Kobin() @app.route('/') def hello() -> Response: print(request.headers) return Response("Hello World!!") @app.route('/hello/{name}') def hello(name: str) -> JSONResponse: return JSONResponse({"name": name})
import os from kobin import Kobin, load_config_from_pyfile from . import views config = load_config_from_pyfile(os.environ.get('KOBIN_SETTINGS_FILE', 'app/config.py')) app = Kobin(config=config) views.setup_routing(app)
def setup_redis(app: Kobin) -> None: app.config['REDIS'] = redis.StrictRedis( host=app.config['REDIS_HOST'], port=app.config['REDIS_PORT'], db=app.config['REDIS_DB'], )
class KobinTests(TestCase): def setUp(self): self.app = Kobin() self.dummy_start_response = lambda x, y: None @self.app.route('/') def dummy_func(): return Response('hello') @self.app.route('/test/{typed_id}') def typed_url_var(typed_id: int): body = "type: {}, value: {}".format(type(typed_id), typed_id) return Response(body) @self.app.route('/test/raise500') def raise500(typed_id: int): 1 / 0 return Response("Don't reach here") def test_route(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} response = self.app._handle(test_env) actual = response.body expected = [b'hello'] self.assertEqual(actual, expected) def test_typed_url_var(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/10'} response = self.app._handle(test_env) actual = response.body expected = [b"type: <class 'int'>, value: 10"] self.assertEqual(actual, expected) def test_404_not_found(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'} response = self.app._handle(test_env) actual = response._status_code expected = 404 self.assertEqual(actual, expected) def test_404_when_cast_error(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/not-integer'} response = self.app._handle(test_env) actual = response._status_code expected = 404 self.assertEqual(actual, expected) def test_response_status_when_500_raised(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/raise500'} response = self.app._handle(test_env) actual = response._status_code expected = 500 self.assertEqual(actual, expected) def test_response_body_when_500_raised_and_enable_debugging(self): self.app.config['DEBUG'] = True test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/raise500'} response = self.app._handle(test_env) actual = response.body expected = [b'Internal Server Error'] self.assertNotEqual(actual, expected) def test_response_body_when_500_raised_and_disable_debugging(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/test/raise500'} response = self.app._handle(test_env) actual = response.body expected = [b'Internal Server Error'] self.assertEqual(actual, expected) def test_handled_body_message_when_404_not_found(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/this_is_not_found'} response = self.app._handle(test_env) actual = response.body expected = [b"Not found: /this_is_not_found"] self.assertEqual(actual, expected) def test_wsgi(self): test_env = {'REQUEST_METHOD': 'GET', 'PATH_INFO': '/'} actual = self.app(test_env, self.dummy_start_response) expected = [b'hello'] self.assertEqual(actual, expected)
from kobin import Kobin app = Kobin() @app.route('/') def hello(): return "Hello World" @app.route('/hoge') def hello(): return "Hello HOGEHOGE" if __name__ == '__main__': app.run()
def test_lazy_reverse_router_not_found(self, app_mock): app = Kobin() app_mock.return_value = app actual = template_router_reverse('top') expected = '' self.assertEqual(actual, expected)