Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)