Exemplo n.º 1
0
class TestMiddleware:
    def setup_method(self):
        self.app = App()
        self.app.bind('Environ', generate_wsgi())
        self.app.bind('Application', application)
        self.app.make('Environ')
        self.app.bind('StatusCode', '404 Not Found Error')
        self.app.bind('Request',
                      Request(self.app.make('Environ')).load_app(self.app))
        self.app.simple(Response(self.app))
        self.app.bind('Csrf', Csrf(self.app.make('Request')))
        self.app.bind('Route', Route(self.app.make('Environ')))

        self.app.bind('ViewClass', View(self.app))

        self.app.bind(
            'WebRoutes',
            [Get().route('/', 'TestController@show').middleware('test')])

        self.app.bind('HttpMiddleware', [MiddlewareHttpTest])
        self.app.bind('RouteMiddleware', {'test': MiddlewareTest})

        self.provider = RouteProvider()
        self.provider.app = self.app

    def test_route_middleware_runs(self):
        self.app.resolve(self.provider.boot)
        assert self.app.make('Request').path == '/test/middleware'

    def test_http_middleware_runs(self):
        self.app.resolve(self.provider.boot)
        assert self.app.make('Request').path == '/test/middleware'
        assert self.app.make('Request').environ['HTTP_TEST'] == 'test'
class TestStatusCode(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.app.bind('StatusCode', '404 Not Found')
        self.app.bind(
            'Request',
            Request(None).load_app(self.app).load_environ(generate_wsgi()))
        self.app.simple(Response(self.app))
        self.app.bind('ViewClass', View(self.app))
        self.app.bind('View', self.app.make('ViewClass').render)

    def test_provider_returns_none_on_200_OK(self):
        self.assertIsNone(StatusCodeProvider().load_app(self.app).boot())
Exemplo n.º 3
0
class TestMiddleware(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.app.bind('Environ', generate_wsgi())
        self.app.bind('Application', application)
        self.app.make('Environ')
        self.app.bind('StatusCode', None)
        self.app.bind('Request',
                      Request(self.app.make('Environ')).load_app(self.app))
        self.app.simple(Response(self.app))
        self.app.bind('Csrf', Csrf(self.app.make('Request')))
        self.app.bind('Route', Route(self.app.make('Environ')))

        self.app.bind('ViewClass', View(self.app))

        self.app.bind(
            'WebRoutes',
            [Get().route('/', 'TestController@show').middleware('test')])

        self.app.bind('HttpMiddleware', [MiddlewareHttpTest])

        self.app.bind('RouteMiddleware', {
            'test': MiddlewareTest,
            'throttle:1,2': MiddlewareValueTest
        })

        self.provider = RouteProvider()
        self.provider.app = self.app

    def test_route_middleware_runs(self):
        self.app.resolve(self.provider.boot)
        self.assertEqual(self.app.make('Request').path, '/test/middleware')

    def test_http_middleware_runs(self):
        self.app.resolve(self.provider.boot)
        self.assertEqual(self.app.make('Request').path, '/test/middleware')
        self.assertEqual(self.app.make('Request').environ['HTTP_TEST'], 'test')

    def test_route_middleware_can_pass_values(self):
        route = self.app.make('WebRoutes')[0]
        route.request = self.app.make('Request')
        route.list_middleware = ['throttle:1,2']
        route.run_middleware('before')
        self.assertEqual(route.request.value1, '1')
        self.assertEqual(route.request.value2, '2')
class TestServerErrorExceptionHook(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.app.bind('Container', self.app)
        self.app.bind(
            'Request',
            Request(None).load_app(self.app).load_environ(generate_wsgi()))
        self.app.simple(Response)
        self.app.bind('Application', MockApplicationConfig)
        self.app.bind('ViewClass', View(self.app))
        self.app.bind('View', self.app.make('ViewClass').render)

    def test_response_is_set_when_app_debug_is_true(self):
        self.assertIsNone(ServerErrorExceptionHook().load(self.app))

    def test_no_response_set_when_app_debug_is_false(self):
        application = MockApplicationConfig
        application.DEBUG = False
        self.app.bind('Application', application)
        self.assertIsNone(ServerErrorExceptionHook().load(self.app))
Exemplo n.º 5
0
class TestStatusCode:
    def setup_method(self):
        self.app = App()
        self.app.bind('StatusCode', '404 Not Found')
        self.app.bind(
            'Request',
            Request(None).load_app(self.app).load_environ(generate_wsgi()))
        self.app.simple(Response(self.app))
        self.app.bind('ViewClass', View(self.app))
        self.app.bind('View', self.app.make('ViewClass').render)
        self.provider = StatusCodeProvider().load_app(self.app).boot()

    def test_provider_returns_masonite_view(self):
        assert '404 Not Found' in self.app.make('Response')
        assert ('Content-Length',
                '1536') in self.app.make('Request').get_headers()

    def test_provider_returns_none_on_200_OK(self):
        self.provider = StatusCodeProvider().load_app(self.app).boot()
        assert self.provider is None
Exemplo n.º 6
0
class TestException:

    def setup_method(self):
        self.app = App()
        self.app.bind('Application', ApplicationMock)
        self.app.bind('Environ', generate_wsgi())
        self.app.bind('WebRoutes', [])
        self.app.bind('View', View(self.app).render)
        self.app.bind('Request', Request(generate_wsgi()).load_app(self.app))
        self.app.simple(Response(self.app))
        self.app.bind('StatusCode', None)
        self.app.bind('Storage', StorageMock)
        self.app.bind('ExceptionHandler', ExceptionHandler(self.app))
        self.app.bind('HookHandler', Hook(self.app))
        self.app.bind('Request', Request(generate_wsgi()).load_app(self.app))
        self.app.bind('ExceptionAttributeErrorHandler', MockExceptionHandler)

    def test_exception_renders_view(self):
        self.app.make('ExceptionHandler').load_exception(OSError)
        assert self.app.make('Response') is not None

    def test_exception_uses_custom_exception(self):
        try:
            self.app.false()
        except Exception as e:
            self.app.make('ExceptionHandler').load_exception(e)
        
        assert self.app.make('Request').header('test') == 'test'

    def test_custom_exception_when_not_registered(self):
        try:
            self.app.false()
        except Exception as e:
            self.app.make('ExceptionHandler').load_exception(e)

    def test_exception_returns_none_when_debug_is_false(self):
        self.app.make('Application').DEBUG = False
        assert self.app.make('ExceptionHandler').load_exception(KeyError) is None
Exemplo n.º 7
0
class TestRouteProvider:

    def setup_method(self):
        self.app = App()
        self.app.bind('Container', self.app)
        self.app.bind('Environ', generate_wsgi())
        self.app.bind('Application', application)
        self.app.bind('WebRoutes', [])
        self.app.bind('Route', Route(self.app.make('Environ')))
        self.app.bind('Request', Request(
            self.app.make('Environ')).load_app(self.app))
        self.app.simple(Response(self.app))
        self.app.bind('StatusCode', '404 Not Found')
        self.app.bind('HttpMiddleware', middleware.HTTP_MIDDLEWARE)
        view = View(self.app)
        self.app.bind('ViewClass', view)
        self.app.bind('View', view.render)
        self.provider = RouteProvider()
        self.provider.app = self.app

    def test_controller_that_returns_a_view(self):
        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes', [get('/view', ControllerTest.test)])
        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == 'test'

        self.app.make('Route').url = '/view/'
        self.app.bind('WebRoutes', [get('/view', ControllerTest.test)])
        
        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        # assert self.app.make('Response') == 'test'

    def test_controller_that_return_a_view_with_trailing_slash(self):

        self.app.make('Route').url = '/view/'
        self.app.bind('WebRoutes', [get('/view/', ControllerTest.test)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == 'test'

        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes', [get('/view/', ControllerTest.test)])

        print(self.app.providers)
        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == 'test'

    def test_match_route_returns_controller(self):
        self.app.make('Route').url = '/view'
        self.app.bind(
            'WebRoutes', [Match(['GET', 'POST']).route('/view', ControllerTest.returns_a_view)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == 'hey'

    def test_provider_runs_through_routes(self):
        self.app.make('Route').url = '/test'
        self.app.bind('WebRoutes', [get('/test', ControllerTest.test)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').header(
            'Content-Type') == 'text/html; charset=utf-8'

    def test_sets_request_params(self):
        self.app.make('Route').url = '/test/1'
        self.app.bind('WebRoutes', [get('/test/@id', ControllerTest.test)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').param('id') == '1'

    def test_url_with_dots_finds_route(self):
        self.app.make('Route').url = '/test/user.endpoint'
        self.app.bind(
            'WebRoutes', [get('/test/@endpoint', ControllerTest.test)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').param('endpoint') == 'user.endpoint'

    def test_view_returns_with_route_view(self):
        self.app.make('Route').url = '/test/route'
        self.app.bind('WebRoutes', [
            Get().view('/test/route', 'test', {'test': 'testing'})
        ])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == 'testing'

    def test_url_with_dashes_finds_route(self):
        self.app.make('Route').url = '/test/user-endpoint'
        self.app.bind(
            'WebRoutes', [get('/test/@endpoint', ControllerTest.test)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').param('endpoint') == 'user-endpoint'

    def test_param_returns_param(self):
        self.app.make('Route').url = '/test/1'
        self.app.bind('WebRoutes', [get('/test/@id', ControllerTest.param)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == '1'

    def test_custom_route_compiler_returns_param(self):
        self.app.make('Route').url = '/test/1'
        self.app.make('Route').compile('signed', r'([\w.-]+)')
        self.app.bind('WebRoutes', [get('/test/@id:signed', ControllerTest.param)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == '1'

    def test_route_subdomain_ignores_routes(self):
        self.app.make('Route').url = '/test'
        self.app.make('Environ')['HTTP_HOST'] = 'subb.domain.com'
        self.app.bind('WebRoutes', [get('/test', ControllerTest.test)])

        request = self.app.make('Request')
        request.activate_subdomains()

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Response') == 'Route not found. Error 404'

    def test_controller_returns_json_response_for_dict(self):
        self.app.make('Route').url = '/view'
        self.app.bind(
            'WebRoutes', [get('/view', ControllerTest.returns_a_dict)])

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').header(
            'Content-Type') == 'application/json; charset=utf-8'

    def test_route_runs_str_middleware(self):
        self.app.make('Route').url = '/view'
        self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE)
        self.app.bind('WebRoutes', [
            get('/view', ControllerTest.returns_a_dict).middleware('test')
        ]
        )

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').path == 'test/middleware/before/ran'

    def test_route_runs_middleware_with_list(self):
        self.app.make('Route').url = '/view'
        self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE)
        self.app.bind('WebRoutes', [
            get('/view', ControllerTest.returns_a_dict).middleware('middleware.test')
        ]
        )

        self.provider.boot(
            self.app.make('Route'),
            self.app.make('Request'),
            self.app.make(Response)
        )

        assert self.app.make('Request').path == 'test/middleware/before/ran'
        assert self.app.make('Request').attribute == True
Exemplo n.º 8
0
class TestRequest(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.request = Request(wsgi_request).key(
            'NCTpkICMlTXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY=').load_app(self.app)
        self.app.bind('Request', self.request)
        self.response = Response(self.app)
        self.app.simple(Response)

    def test_request_is_callable(self):
        """ Request should be callable """
        self.assertIsInstance(self.request, object)

    def test_request_input_should_return_input_on_get_request(self):
        self.assertEqual(self.request.input('application'), 'Masonite')
        self.assertEqual(self.request.input('application', 'foo'), 'Masonite')

    def test_request_input_should_return_default_when_not_exists(self):
        self.assertEqual(self.request.input('foo', 'bar'), 'bar')

    def test_request_all_should_return_params(self):
        self.assertEqual(self.request.all(), {'application': 'Masonite'})

    def test_request_all_without_internal_request_variables(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        self.assertEqual(self.request.all(), {
            '__token': 'testing',
            'application': 'Masonite'
        })
        self.assertEqual(self.request.all(internal_variables=False),
                         {'application': 'Masonite'})

    def test_request_has_should_return_bool(self):
        self.assertEqual(self.request.has('application'), True)
        self.assertEqual(self.request.has('shouldreturnfalse'), False)

    def test_request_has_should_accept_multiple_values(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        self.assertEqual(self.request.has('application'), True)
        self.assertEqual(self.request.has('shouldreturnfalse'), False)
        self.assertEqual(self.request.has('__token'), True)
        self.assertEqual(self.request.has('__token', 'shouldreturnfalse'),
                         False)
        self.assertEqual(self.request.has('__token', 'application'), True)
        self.assertEqual(
            self.request.has('__token', 'application', 'shouldreturnfalse'),
            False)

    def test_request_set_params_should_return_self(self):
        self.assertEqual(self.request.set_params({'value': 'new'}),
                         self.request)
        self.assertEqual(self.request.url_params, {'value': 'new'})

    def test_request_param_returns_parameter_set_or_false(self):
        self.request.set_params({'value': 'new'})
        self.assertEqual(self.request.param('value'), 'new')
        self.assertEqual(self.request.param('nullvalue'), False)

    def test_request_appends_cookie(self):
        self.assertEqual(self.request.cookie('appendcookie', 'value'),
                         self.request)
        assert 'appendcookie' in self.request.environ['HTTP_COOKIE']

    def test_request_input_can_get_dictionary_elements(self):
        self.request.request_variables = {
            "user": {
                "address": [{
                    "id": 1,
                    'street': 'A Street'
                }, {
                    "id": 2,
                    'street': 'B Street'
                }]
            }
        }
        self.assertEqual(self.request.input('user.address.*.id'), [1, 2])
        self.assertEqual(self.request.input('user.address.*.street'),
                         ['A Street', 'B Street'])

    def test_request_sets_and_gets_cookies(self):
        self.request.cookie('setcookie', 'value')
        self.assertEqual(self.request.get_cookie('setcookie'), 'value')

    def test_request_sets_expiration_cookie_2_months(self):
        self.request.cookies = []
        self.request.cookie('setcookie_expiration',
                            'value',
                            expires='2 months')

        time = cookie_expire_time('2 months')

        self.assertEqual(self.request.get_cookie('setcookie_expiration'),
                         'value')
        self.assertEqual(
            self.request.get_raw_cookie('setcookie_expiration')['expires'],
            time)

    def test_delete_cookie(self):
        self.request.cookies = []
        self.request.cookie('delete_cookie', 'value')

        self.assertEqual(self.request.get_cookie('delete_cookie'), 'value')
        self.request.delete_cookie('delete_cookie')
        self.assertFalse(self.request.get_cookie('delete_cookie'))

    def test_delete_cookie_with_wrong_key(self):
        self.request.cookies = []
        self.request.cookie('cookie', 'value')
        self.request.key('wrongkey_TXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY=')
        self.assertIsNone(self.request.get_cookie('cookie'))

    def test_redirect_returns_request(self):
        self.assertEqual(self.request.redirect('newurl'), self.request)
        self.assertEqual(self.request.redirect_url, '/newurl')

    def test_request_no_input_returns_false(self):
        self.assertEqual(self.request.input('notavailable'), False)

    def test_request_mini_field_storage_returns_single_value(self):
        storages = {'test': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test'), '1')

    def test_request_can_get_string_value(self):
        storages = {'test': 'value'}
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test'), 'value')

    def test_request_can_get_list_value(self):
        storages = {'test': ['foo', 'bar']}
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test'), ['foo', 'bar'])

    def test_request_mini_field_storage_doesnt_return_brackets(self):
        storages = {'test[]': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test'), '1')

    def test_request_mini_field_storage_index(self):
        storages = {'test[index]': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test[index]'), '1')

    def test_request_mini_field_storage_with_dot_notation(self):
        storages = {'test[index]': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test.index'), '1')

    def test_request_mini_field_storage_returns_a_list(self):
        storages = {
            'test':
            [MiniFieldStorage('key', '1'),
             MiniFieldStorage('key', '2')]
        }
        self.request._set_standardized_request_variables(storages)
        self.assertEqual(self.request.input('test'), ['1', '2'])

    def test_request_get_cookies_returns_cookies(self):
        self.assertEqual(self.request.get_cookies(), self.request.cookies)

    def test_request_set_user_sets_object(self):
        self.assertEqual(self.request.set_user(object), self.request)
        self.assertEqual(self.request.user_model, object)
        self.assertEqual(self.request.user(), object)

    def test_request_loads_app(self):
        app = App()
        app.bind('Request', self.request)
        app.make('Request').load_app(app)

        self.assertEqual(self.request.app(), app)
        self.assertEqual(app.make('Request').app(), app)

    def test_request_gets_input_from_container(self):
        container = App()
        container.bind('Application', application)
        container.bind('Providers', providers)
        container.bind('WSGI', object)
        container.bind('Environ', wsgi_request)

        for provider in container.make('Providers').PROVIDERS:
            provider().load_app(container).register()

        container.bind('Response', 'test')
        container.bind('WebRoutes', [
            Get().route('url', 'TestController@show'),
            Get().route('url/', 'TestController@show'),
            Get().route('url/@firstname', 'TestController@show'),
        ])

        container.bind('Response', 'Route not found. Error 404')

        for provider in container.make('Providers').PROVIDERS:
            located_provider = provider().load_app(container)

            container.resolve(located_provider.boot)

        self.assertEqual(
            container.make('Request').input('application'), 'Masonite')
        self.assertEqual(
            container.make('Request').all(), {'application': 'Masonite'})
        container.make('Request').environ['REQUEST_METHOD'] = 'POST'
        self.assertEqual(
            container.make('Request').environ['REQUEST_METHOD'], 'POST')
        self.assertEqual(
            container.make('Request').input('application'), 'Masonite')

    def test_redirections_reset(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', WEB_ROUTES)
        request = app.make('Request').load_app(app)

        request.redirect('test')

        self.assertEqual(request.redirect_url, '/test')

        request.reset_redirections()

        self.assertFalse(request.redirect_url)

        request.redirect_to('test')

        self.assertEqual(request.redirect_url, '/test')

        request.reset_redirections()

        self.assertFalse(request.redirect_url)

    def test_redirect_to_throws_exception_when_no_routes_found(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', WEB_ROUTES)
        request = app.make('Request').load_app(app)

        request.redirect_to('test')
        request.redirect(name='test')

        with pytest.raises(RouteException):
            request.redirect_to('notavailable')

        with pytest.raises(RouteException):
            request.redirect(name='notavailable')

    def test_request_has_subdomain_returns_bool(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        self.assertFalse(request.has_subdomain())
        self.assertIsNone(request.subdomain)

        request.environ['HTTP_HOST'] = 'test.localhost.com'

        request.header('TEST', 'set_this')
        self.assertEqual(request.header('TEST'), 'set_this')

        request.header('TEST', 'set_this', http_prefix=True)
        self.assertEqual(request.header('HTTP_TEST'), 'set_this')

    def test_redirect_compiles_url(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = '/test/url'

        self.assertEqual(request.compile_route_to_url(route), '/test/url')

    def test_redirect_compiles_url_with_1_slash(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = '/'

        self.assertEqual(request.compile_route_to_url(route), '/')

    def test_request_route_returns_url(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'TestController@show').name('test.id')
        ])
        request = app.make('Request').load_app(app)

        self.assertEqual(request.route('test.url'), '/test/url')
        self.assertEqual(request.route('test.id', {'id': 1}), '/test/url/1')
        self.assertEqual(request.route('test.id', [1]), '/test/url/1')

        with self.assertRaises(RouteException):
            self.assertTrue(request.route('not.exists', [1]))

    def test_request_route_returns_url_without_passing_args_with_current_param(
            self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'TestController@show').name('test.id')
        ])
        request = app.make('Request').load_app(app)
        request.url_params = {'id': 1}

        assert request.route('test.id') == '/test/url/1'

    def test_request_redirection(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'TestController@testing').name('test.id'),
            Get('/test/url/object', TestController.show).name('test.object')
        ])
        request = app.make('Request').load_app(app)

        self.assertEqual(
            request.redirect('/test/url/@id', {
                'id': 1
            }).redirect_url, '/test/url/1')
        request.redirect_url = None
        self.assertEqual(
            request.redirect(name='test.url').redirect_url, '/test/url')
        request.redirect_url = None
        self.assertEqual(
            request.redirect(name='test.id', params={
                'id': 1
            }).redirect_url, '/test/url/1')
        request.redirect_url = None
        self.assertEqual(
            request.redirect(controller='TestController@show').redirect_url,
            '/test/url')
        request.redirect_url = None
        self.assertEqual(
            request.redirect(controller=TestController.show).redirect_url,
            '/test/url/object')
        request.redirect_url = None
        self.assertEqual(
            request.redirect('some/url').redirect_url, '/some/url')

    def test_request_route_returns_full_url(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'TestController@show').name('test.id')
        ])
        request = app.make('Request').load_app(app)

        self.assertEqual(request.route('test.url', full=True),
                         'http://*****:*****@id'
        params = {
            'id': '1',
        }

        self.assertEqual(request.compile_route_to_url(route, params),
                         '/test/1')

    def test_redirect_compiles_url_with_list_parameters(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/@id'
        params = ['1']

        self.assertEqual(request.compile_route_to_url(route, params),
                         '/test/1')

        route = 'test/@id/@user/test/@slug'
        params = ['1', '2', '3']

        self.assertEqual(request.compile_route_to_url(route, params),
                         '/test/1/2/test/3')

    def test_redirect_compiles_url_with_multiple_parameters(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/@id/@test'
        params = {
            'id': '1',
            'test': 'user',
        }
        self.assertEqual(request.compile_route_to_url(route, params),
                         '/test/1/user')

    def test_request_compiles_custom_route_compiler(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/@id:signed'
        params = {
            'id': '1',
        }
        self.assertEqual(request.compile_route_to_url(route, params),
                         '/test/1')

    def test_redirect_compiles_url_with_http(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = "http://google.com"

        self.assertEqual(request.compile_route_to_url(route),
                         'http://google.com')

    def test_can_get_nully_value(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request._set_standardized_request_variables({
            "gateway": "RENDIMENTO",
            "request": {
                "user": "******"
            },
            "response": None,
            "description": "test only"
        })

        self.assertEqual(request.input('response'), None)

    def test_request_gets_correct_header(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        self.assertEqual(request.header('HTTP_UPGRADE_INSECURE_REQUESTS'), '1')
        self.assertEqual(request.header('RAW_URI'), '/')
        self.assertEqual(request.header('NOT_IN'), '')
        self.assertFalse('text/html' in request.header('NOT_IN'))

    def test_request_sets_correct_header(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.header('TEST', 'set_this')
        self.assertEqual(request.header('TEST'), 'set_this')

        request.header('TEST', 'set_this', http_prefix=True)
        self.assertEqual(request.header('HTTP_TEST'), 'set_this')

    def test_request_cant_set_multiple_headers(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.header('TEST', 'test_this')
        request.header('TEST', 'test_that')

        self.assertEqual(request.header('TEST'), 'test_that')

    def test_request_sets_headers_with_dictionary(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.header({
            'test_dict': 'test_value',
            'test_dict1': 'test_value1'
        })

        self.assertEqual(request.header('test_dict'), 'test_value')
        self.assertEqual(request.header('test_dict1'), 'test_value1')

        request.header({
            'test_dict': 'test_value',
            'test_dict1': 'test_value1'
        },
                       http_prefix=True)

        self.assertEqual(request.header('HTTP_test_dict'), 'test_value')
        self.assertEqual(request.header('HTTP_test_dict1'), 'test_value1')

    def test_request_gets_all_headers(self):
        app = App()
        app.bind('Request', Request(wsgi_request))
        request = app.make('Request').load_app(app)

        request.header('TEST1', 'set_this_item')
        self.assertEqual(request.get_headers(), [('TEST1', 'set_this_item')])

    def test_request_sets_str_status_code(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('StatusCode', 'TestController@show')
        request = app.make('Request').load_app(app)

        request.status('200 OK')
        self.assertEqual(request.get_status_code(), '200 OK')

    def test_request_sets_int_status_code(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        self.assertEqual(request.get_status_code(),
                         '500 Internal Server Error')

    def test_request_gets_int_status(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        self.assertEqual(request.get_status(), 500)

    def test_can_get_code_by_value(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        self.assertEqual(
            request._get_status_code_by_value('500 Internal Server Error'),
            500)

    def test_is_status_code(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        self.assertEqual(request.is_status(500), True)

    def test_request_sets_invalid_int_status_code(self):
        with self.assertRaises(InvalidHTTPStatusCode):
            app = App()
            app.bind('Request', self.request)
            request = app.make('Request').load_app(app)

            request.status(600)

    def test_request_sets_request_method(self):
        wsgi = generate_wsgi()
        wsgi['QUERY_STRING'] = '__method=PUT'
        request = Request(wsgi)

        assert request.has('__method')
        self.assertEqual(request.input('__method'), 'PUT')
        self.assertEqual(request.get_request_method(), 'PUT')

    def test_request_has_should_pop_variables_from_input(self):
        self.request.request_variables.update({'key1': 'test', 'key2': 'test'})
        self.request.pop('key1', 'key2')
        self.assertEqual(self.request.request_variables,
                         {'application': 'Masonite'})
        self.request.pop('shouldnotexist')
        self.assertEqual(self.request.request_variables,
                         {'application': 'Masonite'})
        self.request.pop('application')
        self.assertEqual(self.request.request_variables, {})

    def test_is_named_route(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'TestController@show').name('test.id')
        ])
        request = app.make('Request').load_app(app)

        request.path = '/test/url'
        assert request.is_named_route('test.url')

        request.path = '/test/url/1'
        assert request.is_named_route('test.id', {'id': 1})

    def test_route_exists(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'TestController@show').name('test.id')
        ])
        request = app.make('Request').load_app(app)

        self.assertEqual(request.route_exists('/test/url'), True)
        self.assertEqual(request.route_exists('/test/Not'), False)

    def test_request_url_from_controller(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            Get('/test/url', 'TestController@show').name('test.url'),
            Get('/test/url/@id', 'ControllerTest@show').name('test.id'),
            Get('/test/url/controller/@id',
                TestController.show).name('test.controller'),
        ])

        request = app.make('Request').load_app(app)

        self.assertEqual(request.url_from_controller('TestController@show'),
                         '/test/url')
        self.assertEqual(
            request.url_from_controller('ControllerTest@show', {'id': 1}),
            '/test/url/1')
        self.assertEqual(
            request.url_from_controller(TestController.show, {'id': 1}),
            '/test/url/controller/1')

    def test_contains_for_path_detection(self):
        self.request.path = '/test/path'
        self.assertTrue(self.request.contains('/test/*'))
        self.assertTrue(self.request.contains('/test/path'))
        self.assertFalse(self.request.contains('/test/wrong'))

    def test_contains_for_multiple_paths(self):
        self.request.path = '/test/path/5'
        self.assertTrue(self.request.contains('/test/*'))

    def test_contains_can_return_string(self):
        self.request.path = '/test/path/5'
        self.assertEqual(self.request.contains('/test/*', show='active'),
                         'active')
        self.assertEqual(self.request.contains('/test/not', show='active'), '')

    def test_contains_for_path_with_digit(self):
        self.request.path = '/test/path/1'
        self.assertTrue(self.request.contains('/test/path/*'))
        self.assertTrue(self.request.contains('/test/path/*:int'))

    def test_contains_for_path_with_digit_and_wrong_contains(self):
        self.request.path = '/test/path/joe'
        self.assertFalse(self.request.contains('/test/path/*:int'))

    def test_contains_for_path_with_alpha_contains(self):
        self.request.path = '/test/path/joe'
        self.assertTrue(self.request.contains('/test/path/*:string'))

    def test_contains_for_route_compilers(self):
        self.request.path = '/test/path/joe'
        self.assertTrue(self.request.contains('/test/path/*:signed'))

    def test_contains_multiple_asteriks(self):
        self.request.path = '/dashboard/user/edit/1'
        self.assertTrue(
            self.request.contains('/dashboard/user/*:string/*:int'))

    def test_request_can_get_input_as_properties(self):
        self.request.request_variables = {'test': 'hey'}
        self.assertEqual(self.request.test, 'hey')
        self.assertEqual(self.request.input('test'), 'hey')

    def test_request_can_get_param_as_properties(self):
        self.request.url_params = {'test': 'hey'}
        self.assertEqual(self.request.test, 'hey')
        self.assertEqual(self.request.param('test'), 'hey')

    def test_back_returns_correct_url(self):
        self.request.path = '/dashboard/create'
        self.request.back()
        self.assertEqual(self.request.redirect_url, '/dashboard/create')

        self.request.back(default='/home')
        self.assertEqual(self.request.redirect_url, '/home')

        self.request.request_variables = {'__back': '/login'}
        self.request.back(default='/home')
        self.assertEqual(self.request.redirect_url, '/login')

    def test_request_without(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        self.assertEqual(self.request.without('__token'),
                         {'application': 'Masonite'})

    def test_request_only_returns_specified_values(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        self.assertEqual(self.request.only('application'),
                         {'application': 'Masonite'})
        self.assertEqual(self.request.only('__token'), {'__token': 'testing'})

    def test_request_gets_only_clean_output(self):
        self.request._set_standardized_request_variables(
            {'key': '<img """><script>alert(\'hey\')</script>">'})
        self.assertEqual(
            self.request.input('key'),
            '&lt;img &quot;&quot;&quot;&gt;&lt;script&gt;alert(&#x27;hey&#x27;)&lt;/script&gt;&quot;&gt;'
        )
        self.assertEqual(self.request.input('key', clean=False),
                         '<img """><script>alert(\'hey\')</script>">')

    def test_request_cleans_all_optionally(self):
        self.request._set_standardized_request_variables(
            {'key': '<img """><script>alert(\'hey\')</script>">'})
        self.assertEqual(
            self.request.all()['key'],
            '&lt;img &quot;&quot;&quot;&gt;&lt;script&gt;alert(&#x27;hey&#x27;)&lt;/script&gt;&quot;&gt;'
        )
        self.assertEqual(
            self.request.all(clean=False)['key'],
            '<img """><script>alert(\'hey\')</script>">')

    def test_request_gets_input_with_dotdict(self):
        self.request.request_variables = {
            "key": {
                "user": "******",
                "name": "Joe",
                "address": {
                    "street": "address 1"
                }
            }
        }

        self.assertEqual(
            self.request.input('key')['address']['street'], 'address 1')
        self.assertEqual(self.request.input('key.address.street'), 'address 1')
        self.assertEqual(self.request.input('key.'), False)
        self.assertEqual(self.request.input('key.user'), '1')
        self.assertEqual(self.request.input('key.nothing'), False)
        self.assertEqual(self.request.input('key.nothing', default='test'),
                         'test')
Exemplo n.º 9
0
class TestRequest:
    def setup_method(self):
        self.app = App()
        self.request = Request(wsgi_request).key(
            'NCTpkICMlTXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY=').load_app(self.app)
        self.app.bind('Request', self.request)
        self.response = Response(self.app)
        self.app.simple(Response)

    def test_request_is_callable(self):
        """ Request should be callable """
        if callable(self.request):
            assert True

    def test_request_input_should_return_input_on_get_request(self):
        assert self.request.input('application') == 'Masonite'
        assert self.request.input('application', 'foo') == 'Masonite'

    def test_request_input_should_return_default_when_not_exists(self):
        assert self.request.input('foo', 'bar') == 'bar'

    def test_request_all_should_return_params(self):
        assert self.request.all() == {'application': 'Masonite'}

    def test_request_all_without_internal_request_variables(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        assert self.request.all() == {
            '__token': 'testing',
            'application': 'Masonite'
        }
        assert self.request.all(internal_variables=False) == {
            'application': 'Masonite'
        }

    def test_request_has_should_return_bool(self):
        assert self.request.has('application') == True
        assert self.request.has('shouldreturnfalse') == False

    def test_request_has_should_accept_multiple_values(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        assert self.request.has('application') == True
        assert self.request.has('shouldreturnfalse') == False
        assert self.request.has('__token') == True
        assert self.request.has('__token', 'shouldreturnfalse') == False
        assert self.request.has('__token', 'application') == True
        assert self.request.has('__token', 'application',
                                'shouldreturnfalse') == False

    def test_request_set_params_should_return_self(self):
        assert self.request.set_params({'value': 'new'}) == self.request
        assert self.request.url_params == {'value': 'new'}

    def test_request_param_returns_parameter_set_or_false(self):
        self.request.set_params({'value': 'new'})
        assert self.request.param('value') == 'new'
        assert self.request.param('nullvalue') == False

    def test_request_appends_cookie(self):
        assert self.request.cookie('appendcookie', 'value') == self.request
        assert 'appendcookie' in self.request.environ['HTTP_COOKIE']

    def test_request_sets_and_gets_cookies(self):
        self.request.cookie('setcookie', 'value')
        assert self.request.get_cookie('setcookie') == 'value'

    def test_request_sets_expiration_cookie_2_months(self):
        self.request.cookies = []
        self.request.cookie('setcookie_expiration',
                            'value',
                            expires='2 months')

        time = cookie_expire_time('2 months')

        assert self.request.get_cookie('setcookie_expiration') == 'value'
        assert 'Expires={0}'.format(time) in self.request.cookies[0][1]

    def test_delete_cookie(self):
        self.request.cookies = []
        self.request.cookie('delete_cookie', 'value')

        assert self.request.get_cookie('delete_cookie') == 'value'
        self.request.delete_cookie('delete_cookie')
        assert not self.request.get_cookie('delete_cookie')

    def test_delete_cookie_with_wrong_key(self):
        self.request.cookies = []
        self.request.cookie('cookie', 'value')
        self.request.key('wrongkey_TXie5te9nJniMj9aVbPM6lsjeq5iDZ0dqY=')
        assert self.request.get_cookie('cookie') is None

    def test_redirect_returns_request(self):
        assert self.request.redirect('newurl') == self.request
        assert self.request.redirect_url == '/newurl'

    def test_request_no_input_returns_false(self):
        assert self.request.input('notavailable') == False

    def test_request_mini_field_storage_returns_single_value(self):
        storages = {'test': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        assert self.request.input('test') == '1'

    def test_request_can_get_string_value(self):
        storages = {'test': 'value'}
        self.request._set_standardized_request_variables(storages)
        assert self.request.input('test') == 'value'

    def test_request_mini_field_storage_doesnt_return_brackets(self):
        storages = {'test[]': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        assert self.request.input('test') == '1'

    def test_request_mini_field_storage_index(self):
        storages = {'test[index]': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        assert self.request.input('test[index]') == '1'

    def test_request_mini_field_storage_with_dot_notation(self):
        storages = {'test[index]': [MiniFieldStorage('key', '1')]}
        self.request._set_standardized_request_variables(storages)
        assert self.request.input('test.index') == '1'

    def test_request_mini_field_storage_returns_a_list(self):
        storages = {
            'test':
            [MiniFieldStorage('key', '1'),
             MiniFieldStorage('key', '2')]
        }
        self.request._set_standardized_request_variables(storages)
        assert self.request.input('test') == ['1', '2']

    def test_request_get_cookies_returns_cookies(self):
        assert self.request.get_cookies() == self.request.cookies

    def test_request_set_user_sets_object(self):
        assert self.request.set_user(object) == self.request
        assert self.request.user_model == object
        assert self.request.user() == object

    def test_request_loads_app(self):
        app = App()
        app.bind('Request', self.request)
        app.make('Request').load_app(app)

        assert self.request.app() == app
        assert app.make('Request').app() == app

    def test_request_gets_input_from_container(self):
        container = App()
        container.bind('Application', application)
        container.bind('Providers', providers)
        container.bind('WSGI', object)
        container.bind('Environ', wsgi_request)

        for provider in container.make('Providers').PROVIDERS:
            provider().load_app(container).register()

        container.bind('Response', 'test')
        container.bind('WebRoutes', [
            Get().route('url', None),
            Get().route('url/', None),
            Get().route('url/@firstname', None),
        ])

        container.bind('Response', 'Route not found. Error 404')

        for provider in container.make('Providers').PROVIDERS:
            located_provider = provider().load_app(container)

            container.resolve(located_provider.boot)

        assert container.make('Request').input('application') == 'Masonite'
        assert container.make('Request').all() == {'application': 'Masonite'}
        container.make('Request').environ['REQUEST_METHOD'] = 'POST'
        assert container.make('Request').environ['REQUEST_METHOD'] == 'POST'
        assert container.make('Request').input('application') == 'Masonite'

    def test_redirections_reset(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', WEB_ROUTES)
        request = app.make('Request').load_app(app)

        request.redirect('test')

        assert request.redirect_url == '/test'

        request.reset_redirections()

        assert request.redirect_url is False

        request.redirect_to('test')

        assert request.redirect_url == '/test'

        request.reset_redirections()

        assert request.redirect_url is False

    def test_request_has_subdomain_returns_bool(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        assert request.has_subdomain() is False
        assert request.subdomain is None

        request.environ['HTTP_HOST'] = 'test.localhost.com'

        request.header('TEST', 'set_this')
        assert request.header('TEST') == 'set_this'

        request.header('TEST', 'set_this', http_prefix=True)
        assert request.header('HTTP_TEST') == 'set_this'

    def test_redirect_compiles_url(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = '/test/url'

        assert request.compile_route_to_url(route) == '/test/url'

    def test_redirect_compiles_url_with_1_slash(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = '/'

        assert request.compile_route_to_url(route) == '/'

    def test_request_route_returns_url(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            get('/test/url', None).name('test.url'),
            get('/test/url/@id', None).name('test.id')
        ])
        request = app.make('Request').load_app(app)

        assert request.route('test.url') == '/test/url'
        assert request.route('test.id', {'id': 1}) == '/test/url/1'

    def test_request_redirection(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            get('/test/url', 'TestController@show').name('test.url'),
            get('/test/url/@id', 'TestController@testing').name('test.id'),
            get('/test/url/object', TestController.show).name('test.object')
        ])
        request = app.make('Request').load_app(app)

        assert request.redirect('/test/url/@id', {
            'id': 1
        }).redirect_url == '/test/url/1'
        request.redirect_url = None
        assert request.redirect(name='test.url').redirect_url == '/test/url'
        request.redirect_url = None
        assert request.redirect(name='test.id', params={
            'id': 1
        }).redirect_url == '/test/url/1'
        request.redirect_url = None
        assert request.redirect(
            controller='TestController@show').redirect_url == '/test/url'
        request.redirect_url = None
        assert request.redirect(
            controller=TestController.show).redirect_url == '/test/url/object'
        request.redirect_url = None
        assert request.redirect('some/url').redirect_url == '/some/url'

    def test_request_route_returns_full_url(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            get('/test/url', None).name('test.url'),
            get('/test/url/@id', None).name('test.id')
        ])
        request = app.make('Request').load_app(app)

        assert request.route('test.url',
                             full=True) == 'http://localhost/test/url'

    def test_redirect_compiles_url_with_multiple_slashes(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/url/here'

        assert request.compile_route_to_url(route) == '/test/url/here'

    def test_redirect_compiles_url_with_trailing_slash(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/url/here/'

        assert request.compile_route_to_url(route) == '/test/url/here/'

    def test_redirect_compiles_url_with_parameters(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/@id'
        params = {
            'id': '1',
        }

        assert request.compile_route_to_url(route, params) == '/test/1'

    def test_redirect_compiles_url_with_multiple_parameters(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/@id/@test'
        params = {
            'id': '1',
            'test': 'user',
        }
        assert request.compile_route_to_url(route, params) == '/test/1/user'

    def test_request_compiles_custom_route_compiler(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = 'test/@id:signed'
        params = {
            'id': '1',
        }
        assert request.compile_route_to_url(route, params) == '/test/1'

    def test_redirect_compiles_url_with_http(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        route = "http://google.com"

        assert request.compile_route_to_url(route) == 'http://google.com'

    def test_request_gets_correct_header(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        assert request.header('HTTP_UPGRADE_INSECURE_REQUESTS') == '1'
        assert request.header('RAW_URI') == '/'
        assert request.header('NOT_IN') == ''
        assert not 'text/html' in request.header('NOT_IN')

    def test_request_sets_correct_header(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.header('TEST', 'set_this')
        assert request.header('TEST') == 'set_this'

        request.header('TEST', 'set_this', http_prefix=True)
        assert request.header('HTTP_TEST') == 'set_this'

    def test_request_cant_set_multiple_headers(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.header('TEST', 'test_this')
        request.header('TEST', 'test_that')

        assert request.header('TEST') == 'test_that'

    def test_request_sets_headers_with_dictionary(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.header({
            'test_dict': 'test_value',
            'test_dict1': 'test_value1'
        })

        assert request.header('test_dict') == 'test_value'
        assert request.header('test_dict1') == 'test_value1'

        request.header({
            'test_dict': 'test_value',
            'test_dict1': 'test_value1'
        },
                       http_prefix=True)

        assert request.header('HTTP_test_dict') == 'test_value'
        assert request.header('HTTP_test_dict1') == 'test_value1'

    def test_request_gets_all_headers(self):
        app = App()
        app.bind('Request', Request(wsgi_request))
        request = app.make('Request').load_app(app)

        request.header('TEST1', 'set_this_item')
        assert request.get_headers() == [('TEST1', 'set_this_item')]

    def test_request_sets_str_status_code(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('StatusCode', '404 Not Found')
        request = app.make('Request').load_app(app)

        request.status('200 OK')
        assert request.get_status_code() == '200 OK'

    def test_request_sets_int_status_code(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        assert request.get_status_code() == '500 Internal Server Error'

    def test_request_gets_int_status(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        assert request.get_status() == 500

    def test_can_get_code_by_value(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        assert request._get_status_code_by_value(
            '500 Internal Server Error') == 500

    def test_is_status_code(self):
        app = App()
        app.bind('Request', self.request)
        request = app.make('Request').load_app(app)

        request.status(500)
        assert request.is_status(500) == True

    def test_request_sets_invalid_int_status_code(self):
        with pytest.raises(InvalidHTTPStatusCode):
            app = App()
            app.bind('Request', self.request)
            request = app.make('Request').load_app(app)

            request.status(600)

    def test_request_sets_request_method(self):
        wsgi = generate_wsgi()
        wsgi['QUERY_STRING'] = '__method=PUT'
        request = Request(wsgi)

        assert request.has('__method')
        assert request.input('__method') == 'PUT'
        assert request.get_request_method() == 'PUT'

    def test_request_has_should_pop_variables_from_input(self):
        self.request.request_variables.update({'key1': 'test', 'key2': 'test'})
        self.request.pop('key1', 'key2')
        assert self.request.request_variables == {'application': 'Masonite'}
        self.request.pop('shouldnotexist')
        assert self.request.request_variables == {'application': 'Masonite'}
        self.request.pop('application')
        assert self.request.request_variables == {}

    def test_is_named_route(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            get('/test/url', None).name('test.url'),
            get('/test/url/@id', None).name('test.id')
        ])
        request = app.make('Request').load_app(app)

        request.path = '/test/url'
        assert request.is_named_route('test.url')

        request.path = '/test/url/1'
        assert request.is_named_route('test.id', {'id': 1})

    def test_route_exists(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            get('/test/url', None).name('test.url'),
            get('/test/url/@id', None).name('test.id')
        ])
        request = app.make('Request').load_app(app)

        assert request.route_exists('/test/url') == True
        assert request.route_exists('/test/Not') == False

    def test_request_url_from_controller(self):
        app = App()
        app.bind('Request', self.request)
        app.bind('WebRoutes', [
            get('/test/url', 'TestController@show').name('test.url'),
            get('/test/url/@id', 'ControllerTest@show').name('test.id'),
            get('/test/url/controller/@id',
                TestController.show).name('test.controller'),
        ])

        request = app.make('Request').load_app(app)

        assert request.url_from_controller(
            'TestController@show') == '/test/url'
        assert request.url_from_controller('ControllerTest@show',
                                           {'id': 1}) == '/test/url/1'
        assert request.url_from_controller(
            TestController.show, {'id': 1}) == '/test/url/controller/1'

    def test_contains_for_path_detection(self):
        self.request.path = '/test/path'
        assert self.request.contains('/test/*')
        assert self.request.contains('/test/path')
        assert not self.request.contains('/test/wrong')

    def test_contains_for_path_with_digit(self):
        self.request.path = '/test/path/1'
        assert self.request.contains('/test/path/*')
        assert self.request.contains('/test/path/*:int')

    def test_contains_for_path_with_digit_and_wrong_contains(self):
        self.request.path = '/test/path/joe'
        assert not self.request.contains('/test/path/*:int')

    def test_contains_for_path_with_alpha_contains(self):
        self.request.path = '/test/path/joe'
        assert self.request.contains('/test/path/*:string')

    def test_contains_for_route_compilers(self):
        self.request.path = '/test/path/joe'
        assert self.request.contains('/test/path/*:signed')

    def test_contains_multiple_asteriks(self):
        self.request.path = '/dashboard/user/edit/1'
        assert self.request.contains('/dashboard/user/*:string/*:int')

    def test_back_returns_correct_url(self):
        self.request.path = '/dashboard/create'
        self.request.back()
        assert self.request.redirect_url == '/dashboard/create'

        self.request.back(default='/home')
        assert self.request.redirect_url == '/home'

        self.request.request_variables = {'__back': '/login'}
        self.request.back(default='/home')
        assert self.request.redirect_url == '/login'

    def test_request_without(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        assert self.request.without('__token') == {'application': 'Masonite'}

    def test_request_only_returns_specified_values(self):
        self.request.request_variables.update({
            '__token': 'testing',
            'application': 'Masonite'
        })
        assert self.request.only('application') == {'application': 'Masonite'}
        assert self.request.only('__token') == {'__token': 'testing'}
Exemplo n.º 10
0
class TestRouteProvider(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.app.bind('Container', self.app)
        self.app.bind('Environ', generate_wsgi())
        self.app.bind('Application', application)
        self.app.bind('WebRoutes', [])
        self.app.bind('Route', Route(self.app.make('Environ')))
        self.app.bind('Request',
                      Request(self.app.make('Environ')).load_app(self.app))
        self.app.simple(Response(self.app))
        self.app.bind('StatusCode', None)
        self.app.bind('HttpMiddleware', middleware.HTTP_MIDDLEWARE)
        view = View(self.app)
        self.app.bind('ViewClass', view)
        self.app.bind('View', view.render)
        self.app.bind('Auth', Auth)
        self.app.bind('Csrf', Csrf(self.app.make('Request')))
        self.app.bind('AuthCookieDriver', AuthCookieDriver)
        self.app.bind('AuthManager', AuthManager(self.app).driver('cookie'))
        self.provider = RouteProvider()
        self.provider.app = self.app

    def test_controller_that_returns_a_view(self):
        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes', [Get('/view', ControllerTest.test)])
        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), 'test')

        self.app.make('Route').url = '/view/'
        self.app.bind('WebRoutes', [Get('/view', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        # self.assertEqual(self.app.make('Response'), 'test')

    def test_home_route(self):
        self.app.make('Route').url = '/'
        self.app.bind('WebRoutes', [Get('/', ControllerTest.test)])
        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), 'test')

    def test_base_route_hits_controller(self):
        self.app.make('Route').url = '/test'
        self.app.bind('WebRoutes', [Get('/@id', ControllerTest.test)])
        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        assert self.app.make('Request').param('id') == 'test'
        assert self.app.make('Response') == 'test'

    def test_no_base_route_returns_404(self):
        self.app.make('Route').url = '/'
        self.app.bind('WebRoutes', [Get('/test', ControllerTest.test)])
        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))
        print(self.app.make('Request').get_status_code())
        assert self.app.make('Request').is_status(404)

    def test_controller_that_return_a_view_with_trailing_slash(self):

        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes', [Get('/view', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), 'test')

        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes', [Get('/view', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), 'test')

    def test_match_route_returns_controller(self):
        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes', [
            Match(['GET', 'POST']).route('/view',
                                         ControllerTest.returns_a_view)
        ])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), 'hey')

    def test_provider_runs_through_routes(self):
        self.app.make('Route').url = '/test'
        self.app.bind('WebRoutes', [Get('/test', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(
            self.app.make('Request').header('Content-Type'),
            'text/html; charset=utf-8')

    def test_sets_request_params(self):
        self.app.make('Route').url = '/test/1'
        self.app.bind('WebRoutes', [Get('/test/@id', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Request').param('id'), '1')

    def test_can_use_resolving_params(self):
        self.app.make('Route').url = '/test/1'
        self.app.bind('WebRoutes',
                      [Get('/test/@id', ControllerTest.get_param)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Request').first, '1')

    def test_can_use_resolving_params_and_object(self):
        self.app.make('Route').url = '/test/1'
        self.app.bind('WebRoutes',
                      [Get('/test/@id', ControllerTest.get_param_and_object)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Request').first, '1')
        # self.assertEqual(isinstance(self.app.make('Request').view, View)

    def test_url_with_dots_finds_route(self):
        self.app.make('Route').url = '/test/user.endpoint'
        self.app.bind('WebRoutes',
                      [Get('/test/@endpoint', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(
            self.app.make('Request').param('endpoint'), 'user.endpoint')

    def test_view_returns_with_route_view(self):
        self.app.make('Route').url = '/test/route'
        self.app.bind('WebRoutes',
                      [Get().view('/test/route', 'test', {'test': 'testing'})])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), 'testing')

    def test_url_with_dashes_finds_route(self):
        self.app.make('Route').url = '/test/user-endpoint'
        self.app.bind('WebRoutes',
                      [Get('/test/@endpoint', ControllerTest.test)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(
            self.app.make('Request').param('endpoint'), 'user-endpoint')

    def test_param_returns_param(self):
        self.app.make('Route').url = '/test/1'
        self.app.bind('WebRoutes', [Get('/test/@id', ControllerTest.param)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), '1')

    def test_custom_route_compiler_returns_param(self):
        self.app.make('Route').url = '/test/1'
        self.app.make('Route').compile('signed', r'([\w.-]+)')
        self.app.bind('WebRoutes',
                      [Get('/test/@id:signed', ControllerTest.param)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'), '1')

    def test_route_subdomain_ignores_routes(self):
        self.app.make('Route').url = '/test'
        self.app.make('Environ')['HTTP_HOST'] = 'subb.domain.com'
        self.app.bind('WebRoutes', [Get('/test', ControllerTest.test)])

        request = self.app.make('Request')
        request.activate_subdomains()

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(self.app.make('Response'),
                         'Route not found. Error 404')

    def test_controller_returns_json_response_for_dict(self):
        self.app.make('Route').url = '/view'
        self.app.bind('WebRoutes',
                      [Get('/view', ControllerTest.returns_a_dict)])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(
            self.app.make('Request').header('Content-Type'),
            'application/json; charset=utf-8')

    def test_route_runs_str_middleware(self):
        self.app.make('Route').url = '/view'
        self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE)
        self.app.bind(
            'WebRoutes',
            [Get('/view', ControllerTest.returns_a_dict).middleware('test')])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(
            self.app.make('Request').path, 'test/middleware/before/ran')

    def test_route_runs_middleware_with_list(self):
        self.app.make('Route').url = '/view'
        self.app.bind('RouteMiddleware', middleware.ROUTE_MIDDLEWARE)
        self.app.bind('WebRoutes', [
            Get('/view',
                ControllerTest.returns_a_dict).middleware('middleware.test')
        ])

        self.provider.boot(self.app.make('Route'), self.app.make('Request'),
                           self.app.make(Response))

        self.assertEqual(
            self.app.make('Request').path, 'test/middleware/before/ran')
        self.assertEqual(self.app.make('Request').attribute, True)
Exemplo n.º 11
0
 def test_app_make_after_simple_bind(self):
     app = App()
     req = Request()
     app.simple(req)
     assert app.make(Request) == req
Exemplo n.º 12
0
 def test_app_simple_bind_init(self):
     app = App()
     req = Request()
     app.simple(req)
     assert app.providers == {Request: req}
Exemplo n.º 13
0
 def test_app_simple_bind(self):
     app = App()
     app.simple(Request)
     assert app.providers == {Request: Request}
Exemplo n.º 14
0
 def test_app_make_after_simple_bind(self):
     app = App()
     req = Request()
     app.simple(req)
     self.assertEqual(app.make(Request), req)
Exemplo n.º 15
0
 def test_app_simple_bind_init(self):
     app = App()
     req = Request()
     app.simple(req)
     self.assertEqual(app.providers, {Request: req})
Exemplo n.º 16
0
 def test_app_simple_bind(self):
     app = App()
     app.simple(Request)
     self.assertEqual(app.providers, {Request: Request})