def test_create_from_list(self): router = Router([ { 'name': 'home', 'path': '/' } ]) assert len(router) == 1 router_objects = Router([ Route(name='home', path='/') ]) assert len(router_objects) == 1
def test_create_from_dict(self): router = Router({ 'home': { 'path': '/' } }) assert len(router) == 1 router_objects = Router({ 'home': Route(name='home', path='/') }) assert len(router_objects) == 1 for route in router: assert True assert router.assemble('home') == '/' assert repr(router) == '<watson.mvc.routing.Router routes:1>'
def create_event(self, **kwargs): router = Router({'home': {'path': '/'}}) environ = {} util.setup_testing_defaults(environ) environ.update(**kwargs) event = Event('TestEvent', params={ 'router': router, 'request': create_request_from_environ(environ) }) return event
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_route_to_url(self): base = controllers.HttpMixin() router = Router({ 'test': { 'path': '/test', }, 'segment': { 'path': '/segment[/:part]', 'type': 'segment' } }) base.container = Mock() base.container.get.return_value = router assert base.url('test') == '/test' assert base.url('segment', part='test') == '/segment/test'
def test_post_redirect_get(self): base = controllers.HttpMixin() router = Router({'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 = create_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 = Router({ 'test': { 'path': '/test', }, 'segment': { 'path': '/segment[/:part]', 'type': 'segment', 'defaults': {'part': 'test'} } }) base.request = create_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_assemble_invalid_route(self): router = Router() router.assemble('test')