Beispiel #1
0
    def boot(self, request: Request, response: Response):
        """Boots services required by the container."""
        headers = config('middleware.cors') or {}
        request.header(headers)

        if request.get_request_method().lower() == 'options':
            response.view('preflight')
Beispiel #2
0
class TestResponse:
    def setup_method(self):
        self.app = App()
        self.request = Request(generate_wsgi()).load_app(self.app)
        self.app.bind('Request', self.request)
        self.response = Response(self.app)

    def test_can_set_json(self):
        self.response.json({'test': 'value'})

        assert self.request.is_status(200)
        assert self.request.header('Content-Length') == '17'
        assert self.request.header(
            'Content-Type') == 'application/json; charset=utf-8'

    def test_redirect(self):
        self.response.redirect('/some/test')

        assert self.request.is_status(302)
        assert self.request.header('Location', '/some/test')

    def test_response_does_not_override_header_from_controller(self):
        self.response.view(self.app.resolve(ControllerTest().change_header))

        assert self.request.header('Content-Type') == 'application/xml'

    def test_view(self):
        view = View(self.app).render('test', {'test': 'test'})

        self.response.view(view)

        assert self.app.make('Response') == 'test'
        assert self.request.is_status(200)

        self.response.view('foobar')

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

    def test_view_can_return_integer_as_string(self):
        self.response.view(1)

        assert self.app.make('Response') == '1'
        assert self.request.is_status(200)
 def response_int(self, response: Response):
     return response.view(1)
Beispiel #4
0
class TestResponse(unittest.TestCase):
    def setUp(self):
        self.app = App()
        self.request = Request(generate_wsgi()).load_app(self.app)
        self.app.bind('Request', self.request)
        self.app.bind('StatusCode', None)
        self.response = Response(self.app)
        self.app.bind('Response', self.response)

    def test_can_set_json(self):
        self.response.json({'test': 'value'})

        self.assertTrue(self.request.is_status(200))
        self.assertEqual(self.request.header('Content-Length'), '17')
        self.assertEqual(self.request.header('Content-Type'),
                         'application/json; charset=utf-8')

    def test_redirect(self):
        self.response.redirect('/some/test')

        self.request.header('Location', '/some/test')
        self.assertTrue(self.request.is_status(302))
        self.assertEqual(self.request.header('Location'), '/some/test')

    def test_response_does_not_override_header_from_controller(self):
        self.response.view(self.app.resolve(ControllerTest().change_header))

        self.assertEqual(self.request.header('Content-Type'),
                         'application/xml')

    def test_view(self):
        view = View(self.app).render('test', {'test': 'test'})

        self.response.view(view)

        self.assertEqual(self.app.make('Response'), 'test')
        self.assertTrue(self.request.is_status(200))

        self.response.view('foobar')

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

    def test_view_can_return_integer_as_string(self):
        self.response.view(1)

        self.assertEqual(self.app.make('Response'), '1')
        self.assertTrue(self.request.is_status(200))

    def test_view_can_set_own_status_code_to_404(self):
        self.response.view(self.app.resolve(ControllerTest().change_404))
        self.assertTrue(self.request.is_status(404))

    def test_view_can_set_own_status_code(self):

        self.response.view(self.app.resolve(ControllerTest().change_status))
        self.assertTrue(self.request.is_status(203))

    def test_view_should_return_a_json_response_when_retrieve_a_user_from_model(
            self):

        self.assertIsInstance(MockUser(), Model)
        self.response.view(MockUser().all())

        self.assertIn('"name": "TestUser"', self.app.make('Response'))
        self.assertIn('"email": "*****@*****.**"', self.app.make('Response'))

        self.response.view(MockUser().find(1))

        self.assertIn('"name": "TestUser"', self.app.make('Response'))
        self.assertIn('"email": "*****@*****.**"', self.app.make('Response'))
Beispiel #5
0
    def boot(self, router: Route, request: Request, response: Response):
        # All routes joined
        for route in self.app.make('WebRoutes'):
            """Make a better match for trailing slashes
            Sometimes a user will end with a trailing slash. Because the user might
            create routes like `/url/route` and `/url/route/` and how the regex
            is compiled down, we may need to adjust for urls that end or dont
            end with a trailing slash.
            """

            matchurl = create_matchurl(router, route)
            """Houston, we've got a match
                Check to see if a route matches the corresponding router url. If a match
                is found, execute that route and break out of the loop. We only need
                one match. Routes are executed on a first come, first serve basis
            """

            if matchurl.match(router.url) and request.get_request_method(
            ) in route.method_type:
                route.load_request(request)
                """Check if subdomains are active and if the route matches on the subdomain
                    It needs to match to.
                """

                if request.has_subdomain():
                    # Check if the subdomain matches the correct routes domain
                    if not route.has_required_domain():
                        response.view('Route not found. Error 404')
                        continue
                """Get URL Parameters
                    This will create a dictionary of parameters given. This is sort of a short
                    but complex way to retrieve the url parameters.
                    This is the code used to convert /url/@firstname/@lastname
                    to {'firstmane': 'joseph', 'lastname': 'mancuso'}.
                """

                try:
                    parameter_dict = {}
                    for index, value in enumerate(
                            matchurl.match(router.url).groups()):
                        parameter_dict[router.generated_url_list()
                                       [index]] = value
                    request.set_params(parameter_dict)
                except AttributeError:
                    pass
                """Excute HTTP before middleware
                    Only those middleware that have a "before" method are ran.
                """

                for http_middleware in self.app.make('HttpMiddleware'):
                    located_middleware = self.app.resolve(http_middleware)
                    if hasattr(located_middleware, 'before'):
                        located_middleware.before()
                """Execute Route Before Middleware
                    This is middleware that contains a before method.
                """

                route.run_middleware('before')

                # Show a helper in the terminal of which route has been visited
                if application.DEBUG:
                    print(request.get_request_method() + ' Route: ' +
                          router.url)

                # If no routes have been found and no middleware has changed the status code
                if not request.get_status():
                    """Get the response from the route and set it on the 'Response' key.
                        This data is typically the output of the controller method depending
                        on the type of route.
                    """

                    response.view(route.get_response(), status=200)
                """Execute Route After Route Middleware
                    This is middleware that contains an after method.
                """

                route.run_middleware('after')
                """Excute HTTP after middleware
                    Only those middleware that have an "after" method are ran.
                    Check here if the middleware even has the required method.
                """

                for http_middleware in self.app.make('HttpMiddleware'):
                    located_middleware = self.app.resolve(http_middleware)

                    if hasattr(located_middleware, 'after'):
                        located_middleware.after()
                """Return breaks the loop because the incoming route is found and executed.
                    There is no need to continue searching the route list. First come
                    first serve around these parts of the woods.
                """
                return
        """No Response was found in the for loop so let's set an arbitrary response now.
        """
        response.view('Route not found. Error 404', status=404)
        # If the route exists but not the method is incorrect
        if request.is_status(404) and request.route_exists(request.path):
            response.view('Method not allowed', status=405)
Beispiel #6
0
class TestResponse:
    def setup_method(self):
        self.app = App()
        self.request = Request(generate_wsgi()).load_app(self.app)
        self.app.bind('Request', self.request)
        self.app.bind('StatusCode', None)
        self.response = Response(self.app)
        self.app.bind('Response', self.response)

    def test_can_set_json(self):
        self.response.json({'test': 'value'})

        assert self.request.is_status(200)
        assert self.request.header('Content-Length') == '17'
        assert self.request.header(
            'Content-Type') == 'application/json; charset=utf-8'

    def test_redirect(self):
        self.response.redirect('/some/test')

        assert self.request.is_status(302)
        assert self.request.header('Location', '/some/test')

    def test_response_does_not_override_header_from_controller(self):
        self.response.view(self.app.resolve(ControllerTest().change_header))

        assert self.request.header('Content-Type') == 'application/xml'

    def test_view(self):
        view = View(self.app).render('test', {'test': 'test'})

        self.response.view(view)

        assert self.app.make('Response') == 'test'
        assert self.request.is_status(200)

        self.response.view('foobar')

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

    def test_view_can_return_integer_as_string(self):
        self.response.view(1)

        assert self.app.make('Response') == '1'
        assert self.request.is_status(200)

    def test_view_can_set_own_status_code_to_404(self):
        self.response.view(self.app.resolve(ControllerTest().change_404))
        assert self.request.is_status(404)

    def test_view_can_set_own_status_code(self):

        self.response.view(self.app.resolve(ControllerTest().change_status))
        assert self.request.is_status(203)

    def test_view_should_return_a_json_response_when_retrieve_a_user_from_model(
            self):

        assert isinstance(MockUser(), Model)
        self.response.view(MockUser().all())

        json_response = '[{"name": "TestUser", "email": "*****@*****.**"}, {"name": "TestUser", "email": "*****@*****.**"}]'
        assert self.app.make('Response') == json_response

        self.response.view(MockUser().find(1))

        json_response = '{"name": "TestUser", "email": "*****@*****.**"}'
        assert self.app.make('Response') == json_response