Ejemplo n.º 1
0
    def test_exec_controller_ok(self):
        '''This test case ensures that requested route is executed - success scenario.'''

        global_headers = {"X-Custom-Header1": "header1",
                          "X-Custom-Header2": "header2"}

        def get(key):
            if key == "installed_middleware":
                return ["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"]
            
            if key == "global_response_headers":
                return global_headers
                
        self._settings_facade.get = get
                
        app_middleware = FantasticoApp(self._settings_facade_cls)
        
        response = Response()
        response.content_type = "text/html"
        response.text = "Hello world"
        
        self._controller.exec_logic = lambda request: response
        
        self.assertEqual([b"Hello world"], app_middleware(self._environ, Mock()))
        self.assertTrue(self._environ["test_wrapped_ok"])
        
        self.assertEqual(global_headers["X-Custom-Header1"], response.headers["X-Custom-Header1"])
        self.assertEqual(global_headers["X-Custom-Header2"], response.headers["X-Custom-Header2"])
Ejemplo n.º 2
0
    def test_wrap_no_middleware(self):
        '''Test case that ensures the app entry point works as expected even if no middlewares are installed.'''
        
        self._settings_facade.get = Mock(return_value=[])
        
        app = FantasticoApp(self._settings_facade_cls)

        app(self._environ, Mock())
        
        self.assertTrue(True)
Ejemplo n.º 3
0
    def test_route_notfound(self):
        '''This test case ensures an exception is raised whenever the route requested can not be find.'''
        
        self._settings_facade.get = Mock(return_value=["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"])        
                
        app_middleware = FantasticoApp(self._settings_facade_cls)

        del self._environ["route_/simple/request_handler"]
        self.assertRaises(FantasticoRouteNotFoundError, app_middleware, *[self._environ, Mock()])
        
        self._environ["route_/simple/request_handler"] = None
        self.assertRaises(FantasticoRouteNotFoundError, app_middleware, *[self._environ, Mock()])
Ejemplo n.º 4
0
    def test_missing_request(self):
        '''This test case ensures an exception is raised if request middleware was not executed correctly.'''

        self._settings_facade.get = Mock(return_value=["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"])        
                
        app_middleware = FantasticoApp(self._settings_facade_cls)
        
        del self._environ["fantastico.request"]
        self.assertRaises(FantasticoNoRequestError, app_middleware, *[self._environ, Mock()])
        
        self._environ["fantastico.request"] = None
        self.assertRaises(FantasticoNoRequestError, app_middleware, *[self._environ, Mock()])
Ejemplo n.º 5
0
 def __call__(self, environ, start_response):
     if self._fantastico is None:
         if not self._instantiator_lock:
             self._instantiator_lock = threading.Lock()
             self._instantiator_lock.acquire()
             
             self._fantastico = FantasticoApp()
             
             self._instantiator_lock.release()
         else:
             self._instantiator_lock.acquire()
     
     return self._fantastico(environ, start_response)
Ejemplo n.º 6
0
    def test_route_nocontroller(self):
        '''This test case ensures an exception is raised whenever route controller is not set correctly.'''

        self._settings_facade.get = Mock(return_value=["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"])        
                
        app_middleware = FantasticoApp(self._settings_facade_cls)
        
        route_handler = "route_/simple/request_handler" 
        
        del self._environ[route_handler]["controller"]
        self.assertRaises(FantasticoRouteNotFoundError, app_middleware, *[self._environ, Mock()])        

        self._environ[route_handler]["controller"] = None
        self.assertRaises(FantasticoRouteNotFoundError, app_middleware, *[self._environ, Mock()])        
Ejemplo n.º 7
0
    def test_mistmatch_content_headers(self):
        '''This test case makes sure an exception is raised whenever client browser does not accept response
        content type.'''
        
        self._settings_facade.get = Mock(return_value=["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"])        

        response = Response()
        response.content_type = "not supported"
        response.text = "Hello world"
        
        self._controller.exec_logic = lambda request: response
        
        app_middleware = FantasticoApp(self._settings_facade_cls)
        
        self.assertRaises(FantasticoContentTypeError, app_middleware, *[self._environ, Mock()])            
        self.assertTrue(self._environ["test_wrapped_ok"])
Ejemplo n.º 8
0
    def test_middlewares_wrapped(self):
        '''Test case that ensures configured middlewares from a config profile are wrapped in the correct order.'''
        
        def get(key):
            if key == "installed_middleware":
                return ["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"]
        
        self._settings_facade.get = get
        
        app = FantasticoApp(self._settings_facade_cls)        
        
        app(self._environ, Mock())

        self.assertTrue(self._environ.get("test_wrapped_ok"))
        
        chained_resp = self._environ.get("middlewares_responses")
        self.assertIsNotNone(chained_resp)
        self.assertEqual(1, len(chained_resp))
        self.assertEqual(["middleware"], chained_resp)
Ejemplo n.º 9
0
    def test_exec_controller_url_params_wrong_method(self):
        '''This test case ensures a concrete exception is raised if the method registered as controller does
        not accept given named arguments.'''

        self._settings_facade.get = Mock(return_value=["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"])        
                
        app_middleware = FantasticoApp(self._settings_facade_cls)

        comp_name = "component"
        path =  "path/here"
        
        self._environ["route_/simple/request_handler"]["url_params"] = {"comp_name": comp_name, "path": path}
        
        self._controller.exec_logic = lambda request: None
        
        with self.assertRaises(TypeError) as cm:
            app_middleware(self._environ, Mock())
        
        self.assertTrue(str(cm.exception).find("comp_name") > -1, "Missing named arguments must be reported.")
Ejemplo n.º 10
0
    def init(self):
        self._environ = {"CONTENT_TYPE": "text/html; charset=UTF-8",
                           "HTTP_ACCEPT": "text/html;q=1,application/json;q=0.9",
                           "HTTP_ACCEPT_LANGUAGE": "ro-ro,en-US;q=0.8",
                           "HTTP_OAUTH_BEARER": "123",
                           "HTTP_HOST": "localhost:80",
                           "PATH_INFO": DummyRouteLoader.DUMMY_ROUTE,
                           "QUERY_STRING": "id=1",
                           "REQUEST_METHOD": "GET",
                           "SCRIPT_NAME": "",
                           "SERVER_NAME": "localhost",
                           "SERVER_PORT": "80",
                           "SERVER_PROTOCOL": "HTTP/1.1",
                           "wsgi.multiprocess": False,
                           "wsgi.multithread": False,
                           "wsgi.run_once": False,
                           "wsgi.url_scheme": 'http',
                           "wsgi.version": (1, 0)}

        self._middleware = FantasticoApp()
Ejemplo n.º 11
0
    def test_middleware_wrapped_in_order(self):
        '''This test case ensures middlewares are wrapped and executed in the configured order.'''
        
        def get(key):
            if key == "installed_middleware":
                return ["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware",
                        "fantastico.middleware.tests.test_fantastico_app.MockedMiddleware2",
                        "fantastico.middleware.tests.test_fantastico_app.MockedMiddleware3"]
        
        self._settings_facade.get = get
        
        app = FantasticoApp(self._settings_facade_cls)

        app(self._environ, Mock())
        
        self.assertTrue(self._environ.get("test_wrapped_ok"))
        
        chained_resp = self._environ.get("middlewares_responses")
        self.assertIsNotNone(chained_resp)
        self.assertEqual(3, len(chained_resp))
        self.assertEqual(["middleware", "middleware2", "middleware3"], chained_resp)
Ejemplo n.º 12
0
    def test_exec_controller_url_params_ok(self):
        '''This test case ensures that requested route is executed and url_params are passed correctly.'''

        def get(key):
            if key == "installed_middleware":
                return ["fantastico.middleware.tests.test_fantastico_app.MockedMiddleware"]
            
        self._settings_facade.get = get        
                
        app_middleware = FantasticoApp(self._settings_facade_cls)
        
        comp_name = "component"
        path =  "path/here"
        
        self._environ["route_/simple/request_handler"]["url_params"] = {"comp_name": comp_name, "path": path}
        
        self._controller.exec_logic = lambda request, comp_name, path: Response(content_type="text/html; charset=UTF-8",
                                                                                text="/%s/%s" % (comp_name, path))
        
        self.assertEqual([b"/component/path/here"], app_middleware(self._environ, Mock()))
        self.assertTrue(self._environ["test_wrapped_ok"])