def test_child_route_creation(self):
        router = Router({
            'home': {
                'path': '/'
            },
            'parent': {
                'path': '/parent',
                'children': {
                    'child_one': {
                        'path': '/child_one',
                        'children': {
                            'sub_child': {
                                'path': '/test'
                            }
                        }
                    },
                    'child_two': {
                        'path': '/child_two'
                    }
                }
            }
        })
        assert len(router) == 5

        request = create_request_from_environ(sample_environ(PATH_INFO='/child_one'))
        matches = router.matches(request)
        assert len(matches) == 0

        request = create_request_from_environ(sample_environ(PATH_INFO='/parent/child_one'))
        matches = router.matches(request)
        assert len(matches) == 1

        assert router.assemble('parent/child_one/sub_child') == '/parent/child_one/test'
 def test_static_match(self):
     route = Route(name='home', path='/')
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/test'))
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/'))
     assert not route.match(invalid_request)
     assert route.match(valid_request)
 def test_accept_method_match(self):
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/test', REQUEST_METHOD='POST'))
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/test', REQUEST_METHOD='GET'))
     route = Route(name='test', path='/test', accepts=('POST',))
     assert route.match(valid_request)
     assert not route.match(invalid_request)
 def test_mandatory_segment_match(self):
     route = Route("search", path='/search/:keyword')
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/searching'))
     valid_request_no_param = create_request_from_environ(
         sample_environ(PATH_INFO='/search'))
     valid_request_with_param = create_request_from_environ(
         sample_environ(PATH_INFO='/search/test'))
     assert not route.match(invalid_request)
     assert not route.match(valid_request_no_param)
     assert route.match(valid_request_with_param)
 def test_optional_segment_with_required(self):
     route = Route(
         name="search",
         path='/search[/:keyword]',
         requires={'keyword': 'blah'})
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/search/blah'))
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/search/test'))
     assert not route.match(invalid_request)
     assert route.match(valid_request)
 def test_optional_segment_match(self):
     route = Route(name="search", path='/search[/:keyword]')
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/searching'))
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/search'))
     valid_request_with_param = create_request_from_environ(
         sample_environ(PATH_INFO='/search/test'))
     assert not route.match(invalid_request)
     assert route.match(valid_request)
     assert route.match(valid_request_with_param)
    def test_requires_get_variables(self):
        route = Route(name='test', path='/', requires={'test': '.*'})
        request = create_request_from_environ(sample_environ(
            PATH_INFO='/',
            QUERY_STRING='test=blah&something=test'))
        match = route.match(request)
        assert match

        request = create_request_from_environ(sample_environ(
            PATH_INFO='/',
            QUERY_STRING='tesst=blah'))
        match = route.match(request)
        assert not match
 def test_subdomain_match(self):
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/test',
                        SERVER_NAME='clients.test.com',
                        HTTP_HOST='clients.test.com'))
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/test', SERVER_NAME='clients2.test.com',
                        HTTP_HOST='clients2.test.com'))
     route = Route(name='test', path='/test', subdomain='clients')
     assert route.match(valid_request)
     assert not route.match(invalid_request)
     route_multiple_subdomains = Route(name='test', path='/test', subdomain=('clients', 'test'))
     assert route_multiple_subdomains.match(valid_request)
     assert not route_multiple_subdomains.match(invalid_request)
 def test_call(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller': 'tests.watson.framework.support.TestController'
                 },
                 'requires': {
                     'format': 'json'
                 }
             }
         },
         'views': {
             'templates': {
                 'watson/mvc/test_applications/testcontroller/post': 'blank'
             }
         },
         'debug': {
             'enabled': True
         }
     })
     environ = sample_environ(PATH_INFO='/',
                              REQUEST_METHOD='POST',
                              HTTP_ACCEPT='application/json')
     response = application(environ, start_response)
     assert response == [b'{"content": "Posted Hello World!"}']
Beispiel #10
0
 def test_format_match(self):
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/dump',
                        HTTP_ACCEPT='application/json'))
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/dump', HTTP_ACCEPT='application/xml'))
     valid_request_segment = create_request_from_environ(
         sample_environ(PATH_INFO='/dump.json'))
     route = Route(name='json', path='/dump', requires={'format': 'json'})
     route_format = Route(
         name='json',
         path='/dump.:format',
         requires={'format': 'json'})
     assert route.match(valid_request)
     assert not route.match(invalid_request)
     assert route_format.match(valid_request_segment)
 def test_call(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller':
                     'tests.watson.framework.support.TestController'
                 },
                 'requires': {
                     'format': 'json'
                 }
             }
         },
         'views': {
             'templates': {
                 'watson/mvc/test_applications/testcontroller/post': 'blank'
             }
         },
         'debug': {
             'enabled': True
         }
     })
     environ = sample_environ(PATH_INFO='/',
                              REQUEST_METHOD='POST',
                              HTTP_ACCEPT='application/json')
     response = application(environ, start_response)
     assert response == [b'{"content": "Posted Hello World!"}']
Beispiel #12
0
 def test_optional_segment_with_defaults(self):
     route = Route(
         name="search",
         path='/search[/:keyword]',
         defaults={'keyword': 'blah'})
     invalid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/searching'))
     valid_request = create_request_from_environ(
         sample_environ(PATH_INFO='/search'))
     valid_request_with_param = create_request_from_environ(
         sample_environ(PATH_INFO='/search/test'))
     valid_request_with_default = route.match(valid_request)
     assert not route.match(invalid_request)
     assert valid_request_with_default
     assert valid_request_with_default.params == {'keyword': 'blah'}
     assert route.match(valid_request_with_param)
 def test_forward_no_method(self):
     controller = SampleActionController()
     controller.container = IocContainer()
     request = Request.from_environ(sample_environ())
     context = {'request': request}
     controller.event = types.Event('test', params={'context': context})
     controller.__action__ = 'do_method_forward'
     assert controller.do_method_forward() == 'Another Response'
 def test_forward_no_method(self):
     controller = SampleActionController()
     controller.container = IocContainer()
     request = Request.from_environ(sample_environ())
     context = {'request': request}
     controller.event = types.Event('test', params={'context': context})
     controller.__action__ = 'do_method_forward'
     assert controller.do_method_forward() == 'Another Response'
 def test_flash_message(self):
     controller = SampleActionController()
     controller.request = Request.from_environ(sample_environ(), 'watson.http.sessions.Memory')
     controller.flash_messages.add('testing')
     controller.flash_messages.add('something')
     assert controller.flash_messages['info'] == ['testing', 'something']
     for namespace, message in controller.flash_messages:
         assert namespace == 'info'
     assert not controller.flash_messages.messages
 def test_flash_message(self):
     controller = SampleActionController()
     controller.request = Request.from_environ(sample_environ(), 'watson.http.sessions.Memory')
     controller.flash_messages.add('testing')
     controller.flash_messages.add('something')
     assert controller.flash_messages['info'] == ['testing', 'something']
     for namespace, message in controller.flash_messages:
         assert namespace == 'info'
     assert not controller.flash_messages.messages
 def test_last_exception(self):
     # occurs when exceptions have been raised from others
     environ = sample_environ()
     context = {'request': Request.from_environ(environ)}
     app = applications.Http()
     response, view_model = app.exception(last_exception=True,
                                          exception=Exception('test'),
                                          context=context)
     assert '<h1>Internal Server Error</h1>' in response.body
 def test_flash_message(self):
     controller = SampleActionController()
     controller.request = Request.from_environ(sample_environ(), "watson.http.sessions.Memory")
     controller.flash_messages.add("testing")
     controller.flash_messages.add("something")
     assert controller.flash_messages["info"] == ["testing", "something"]
     for namespace, message in controller.flash_messages:
         assert namespace == "info"
     assert not controller.flash_messages.messages
 def test_last_exception(self):
     # occurs when exceptions have been raised from others
     environ = sample_environ()
     context = {
         'request': Request.from_environ(environ)
     }
     app = applications.Http()
     response, view_model = app.exception(
         last_exception=True, exception=Exception('test'), context=context)
     assert '<h1>Internal Server Error</h1>' in response.body
 def test_short_circuit(self):
     environ = sample_environ()
     route = LiteralRoute(
         "test", path="/", options={"controller": "tests.watson.framework.support.ShortCircuitedController"}
     )
     match = RouteMatch(route, {})
     context = {"request": Request.from_environ(environ), "route_match": match}
     event = Event("something", params={"container": IocContainer(), "context": context})
     listener = listeners.DispatchExecute({"404": "page/404"})
     response, view_model = listener(event)
     assert isinstance(response, Response)
Beispiel #21
0
 def setup(self):
     app = applications.Http()
     p = Request(
         {'enabled': True}, app.container.get('jinja2_renderer'), app)
     self.app = app
     self.panel = p
     params = {
         'context': {
             'request': messages.Request.from_environ(support.sample_environ())
         }
     }
     p.event = types.Event('test', params=params)
 def test_overriden_template(self):
     environ = sample_environ()
     route = LiteralRoute(
         "test", path="/", options={"controller": "tests.watson.framework.support.SampleActionController"}
     )
     match = RouteMatch(route, {"action": "view_model_template"})
     context = {"request": Request.from_environ(environ), "route_match": match}
     event = Event("something", params={"container": IocContainer(), "context": context})
     listener = listeners.DispatchExecute({"404": "page/500"})
     response, view_model = listener(event)
     assert isinstance(view_model, views.Model)
     assert view_model.template == "sampleactioncontroller/404"
 def test_raise_exception_event_server_error(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller': 'tests.watson.framework.support.TestController'
                 }
             }
         }
     })
     response = application(sample_environ(PATH_INFO='/'), start_response)
     assert '<h1>Internal Server Error</h1>' in response[0].decode('utf-8')
 def test_short_circuit(self):
     environ = sample_environ()
     route = LiteralRoute(
         'test',
         path='/',
         options={'controller': 'tests.watson.framework.support.ShortCircuitedController'})
     match = RouteMatch(route, {})
     context = {'request': Request.from_environ(environ), 'route_match': match}
     event = Event(
         'something',
         params={'container': IocContainer(), 'context': context})
     listener = listeners.DispatchExecute({'404': 'page/404'})
     response, view_model = listener(event)
     assert isinstance(response, Response)
 def test_short_circuit(self):
     environ = sample_environ()
     route = LiteralRoute(
         'test',
         path='/',
         options={'controller': 'tests.watson.framework.support.ShortCircuitedController'})
     match = RouteMatch(route, {})
     context = {'request': Request.from_environ(environ), 'route_match': match}
     event = Event(
         'something',
         params={'container': IocContainer(), 'context': context})
     listener = listeners.DispatchExecute({'404': 'page/404'})
     response, view_model = listener(event)
     assert isinstance(response, Response)
 def test_raise_exception_event_server_error(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller':
                     'tests.watson.framework.support.TestController'
                 }
             }
         }
     })
     response = application(sample_environ(PATH_INFO='/'), start_response)
     assert '<h1>Internal Server Error</h1>' in response[0].decode('utf-8')
 def test_post_redirect_get(self):
     base = controllers.HttpMixin()
     router = DictRouter({"test": {"path": "/test"}})
     environ = sample_environ(PATH_INFO="/", REQUEST_METHOD="POST")
     environ["wsgi.input"] = BufferedReader(BytesIO(b"post_var_one=test&post_var_two=blah"))
     base.request = Request.from_environ(environ, "watson.http.sessions.Memory")
     base.container = Mock()
     base.container.get.return_value = router
     response = base.redirect("test")
     assert response.status_code == 303
     assert base.redirect_vars == base.request.session["post_redirect_get"]
     base.clear_redirect_vars()
     assert not base.redirect_vars
     base.redirect("test", clear=True)
     assert not base.redirect_vars
 def test_overriden_template(self):
     environ = sample_environ()
     route = LiteralRoute(
         'test',
         path='/',
         options={'controller': 'tests.watson.framework.support.SampleActionController'})
     match = RouteMatch(route, {'action': 'view_model_template'})
     context = {'request': Request.from_environ(environ), 'route_match': match}
     event = Event(
         'something',
         params={'container': IocContainer(), 'context': context})
     listener = listeners.DispatchExecute({'404': 'page/500'})
     response, view_model = listener(event)
     assert isinstance(view_model, views.Model)
     assert view_model.template == 'sampleactioncontroller/404'
 def test_redirect(self):
     base = controllers.HttpMixin()
     router = DictRouter(
         {
             "test": {"path": "/test"},
             "segment": {"path": "/segment[/:part]", "type": "segment", "defaults": {"part": "test"}},
         }
     )
     base.request = Request.from_environ(sample_environ())
     base.container = Mock()
     base.container.get.return_value = router
     response = base.redirect("/test")
     assert response.headers["location"] == "/test"
     response = base.redirect("segment")
     assert response.headers["location"] == "/segment/test"
     assert response.status_code == 302
 def test_post_redirect_get(self):
     base = controllers.HttpMixin()
     router = DictRouter({'test': {'path': '/test'}})
     environ = sample_environ(PATH_INFO='/', REQUEST_METHOD='POST')
     environ['wsgi.input'] = BufferedReader(
         BytesIO(b'post_var_one=test&post_var_two=blah'))
     base.request = Request.from_environ(
         environ, 'watson.http.sessions.Memory')
     base.container = Mock()
     base.container.get.return_value = router
     response = base.redirect('test')
     assert response.status_code == 303
     assert base.redirect_vars == base.request.session['post_redirect_get']
     base.clear_redirect_vars()
     assert not base.redirect_vars
     base.redirect('test', clear=True)
     assert not base.redirect_vars
 def test_post_redirect_get(self):
     base = controllers.HttpMixin()
     router = DictRouter({'test': {'path': '/test'}})
     environ = sample_environ(PATH_INFO='/', REQUEST_METHOD='POST')
     environ['wsgi.input'] = BufferedReader(
         BytesIO(b'post_var_one=test&post_var_two=blah'))
     base.request = Request.from_environ(environ,
                                         'watson.http.sessions.Memory')
     base.container = Mock()
     base.container.get.return_value = router
     response = base.redirect('test')
     assert response.status_code == 303
     assert base.redirect_vars == base.request.session['post_redirect_get']
     base.clear_redirect_vars()
     assert not base.redirect_vars
     base.redirect('test', clear=True)
     assert not base.redirect_vars
 def test_redirect(self):
     base = controllers.HttpMixin()
     router = DictRouter({
         'test': {
             'path': '/test',
         },
         'segment': {
             'path': '/segment[/:part]',
             'type': 'segment',
             'defaults': {'part': 'test'}
         }
     })
     base.request = Request.from_environ(sample_environ())
     base.container = Mock()
     base.container.get.return_value = router
     response = base.redirect('/test')
     assert response.headers['location'] == '/test'
     response = base.redirect('segment')
     assert response.headers['location'] == '/segment/test'
     assert response.status_code == 302
 def test_redirect(self):
     base = controllers.HttpMixin()
     router = DictRouter({
         'test': {
             'path': '/test',
         },
         'segment': {
             'path': '/segment[/:part]',
             'type': 'segment',
             'defaults': {'part': 'test'}
         }
     })
     base.request = Request.from_environ(sample_environ())
     base.container = Mock()
     base.container.get.return_value = router
     response = base.redirect('/test')
     assert response.headers['location'] == '/test'
     response = base.redirect('segment')
     assert response.headers['location'] == '/segment/test'
     assert response.status_code == 302
Beispiel #34
0
 def test_sort_routes(self):
     router = Router({
         'home': {
             'path': '/'
         },
         'test': {
             'path': '/'
         },
         'highest': {
             'path': '/',
             'priority': 1000
         },
         'lowest': {
             'path': '/',
             'priority': -1
         }
     })
     request = create_request_from_environ(sample_environ(PATH_INFO='/'))
     matches = router.matches(request)
     assert matches[0].route.name == 'highest'
     assert matches[3].route.name == 'lowest'
 def test_application_logic_error(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller':
                     'tests.watson.framework.support.SampleActionController',
                     'action': 'blah_syntax_error'
                 }
             }
         },
         'views': {
             'templates': {
                 'watson/mvc/test_applications/testcontroller/blah_syntax_error':
                 'blank'
             }
         }
     })
     response = application(sample_environ(PATH_INFO='/'), start_response)
     assert '<h1>Internal Server Error</h1>' in response[0].decode('utf-8')
 def test_application_logic_error(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller':
                         'tests.watson.framework.support.SampleActionController',
                     'action': 'blah_syntax_error'
                 }
             }
         },
         'views': {
             'templates': {
                 'watson/mvc/test_applications/testcontroller/blah_syntax_error':
                 'blank'
             }
         }
     })
     response = application(sample_environ(PATH_INFO='/'), start_response)
     assert '<h1>Internal Server Error</h1>' in response[0].decode('utf-8')
 def test_json_output(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller': 'tests.watson.framework.support.AnotherSampleActionController',
                 },
                 'defaults': {
                     'action': 'json'
                 },
             }
         },
         'debug': {
             'enabled': True
         }
     })
     environ = sample_environ(PATH_INFO='/',
                              REQUEST_METHOD='GET',
                              HTTP_ACCEPT='application/json')
     response = application(environ, start_response)
     assert response == [b'{"name": "value"}']
 def test_json_output(self):
     application = applications.Http({
         'routes': {
             'home': {
                 'path': '/',
                 'options': {
                     'controller':
                     'tests.watson.framework.support.AnotherSampleActionController',
                 },
                 'defaults': {
                     'action': 'json'
                 },
             }
         },
         'debug': {
             'enabled': True
         }
     })
     environ = sample_environ(PATH_INFO='/',
                              REQUEST_METHOD='GET',
                              HTTP_ACCEPT='application/json')
     response = application(environ, start_response)
     assert response == [b'{"name": "value"}']
 def test_blank_response(self):
     controller = SampleActionController()
     controller.request = Request.from_environ(sample_environ())
     result = controller.execute(action='blank')
     assert isinstance(result, dict)
 def test_raise_exception_event_not_found(self):
     application = applications.Http()
     response = application(sample_environ(PATH_INFO='/'), start_response)
     assert '<h1>Not Found</h1>' in response[0].decode('utf-8')
 def test_raise_exception_event_not_found(self):
     application = applications.Http()
     response = application(sample_environ(PATH_INFO='/'), start_response)
     assert '<h1>Not Found</h1>' in response[0].decode('utf-8')
 def test_forward(self):
     controller = SampleActionController()
     request = Request.from_environ(sample_environ())
     context = {"request": request}
     controller.event = types.Event("test", params={"context": context})
     assert controller.do_forward() == "Response"
Beispiel #43
0
 def test_run(self):
     response = self.panel.run(support.sample_environ(),
                               support.start_response)
     assert isinstance(response[0], bytes)
 def test_forward(self):
     controller = SampleActionController()
     request = Request.from_environ(sample_environ())
     context = {'request': request}
     controller.event = types.Event('test', params={'context': context})
     assert controller.do_forward() == 'Response'
 def test_request_response(self):
     base = controllers.HttpMixin()
     base.request = Request.from_environ(sample_environ())
     assert isinstance(base.request, Request)
     assert isinstance(base.response, Response)
 def test_execute_result(self):
     controller = SampleRestController()
     controller.request = Request.from_environ(sample_environ())
     result = controller.execute(something='test')
     assert result == 'GET'
 def test_request_response(self):
     base = controllers.HttpMixin()
     base.request = Request.from_environ(sample_environ())
     assert isinstance(base.request, Request)
     assert isinstance(base.response, Response)
 def test_method_template(self):
     controller = SampleRestController()
     controller.request = Request.from_environ(sample_environ())
     assert controller.get_execute_method_path() == [
         'samplerestcontroller', 'get'
     ]