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_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).matched assert not route.match(valid_request_no_param).matched assert route.match(valid_request_with_param).matched
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_subdomain_match(self): valid_request = create_request_from_environ( sample_environ(PATH_INFO='/test', SERVER_NAME='clients.test.com')) invalid_request = create_request_from_environ( sample_environ(PATH_INFO='/test', SERVER_NAME='clients2.test.com')) route = Route(name='test', path='/test', subdomain='clients') assert route.match(valid_request) assert not route.match(invalid_request)
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).matched assert not route.match(invalid_request).matched assert route_format.match(valid_request_segment).matched
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).matched assert valid_request_with_default.matched assert valid_request_with_default.params == {'keyword': 'blah'} assert route.match(valid_request_with_param).matched
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_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_call(self): application = applications.Http({ 'routes': { 'home': { 'path': '/', 'options': { 'controller': 'tests.watson.mvc.support.TestController' } } }, 'views': { 'templates': { 'watson/mvc/test_applications/testcontroller/post': 'blank' } }, 'debug': { 'enabled': True } }) response = application( sample_environ(PATH_INFO='/', REQUEST_METHOD='POST', HTTP_ACCEPT='application/json'), start_response) assert response == [b'{"content": "Posted Hello World!"}']
def test_short_circuit(self): environ = sample_environ() route = Route("test", path="/", defaults={"controller": "tests.watson.mvc.support.ShortCircuitedController"}) match = RouteMatch(route, {"controller": "tests.watson.mvc.support.ShortCircuitedController"}, True) event = Event( "something", params={"route_match": match, "container": IocContainer(), "request": create_request_from_environ(environ)}, ) listener = listeners.DispatchExecute({"404": "page/404"}) response = listener(event) assert isinstance(response, Response)
def test_raise_exception_event_server_error(self): application = applications.Http({ 'routes': { 'home': { 'path': '/', 'defaults': { 'controller': 'tests.watson.mvc.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 = 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_short_circuit(self): environ = sample_environ() route = Route( 'test', path='/', options={'controller': 'tests.watson.mvc.support.ShortCircuitedController'}) match = RouteMatch( route, {'controller': 'tests.watson.mvc.support.ShortCircuitedController'}) event = Event( 'something', params={'route_match': match, 'container': IocContainer(), 'request': create_request_from_environ(environ)}) listener = listeners.DispatchExecute({'404': 'page/404'}) response = listener(event) assert isinstance(response, Response)
def test_call(self): application = applications.Http({ 'routes': { 'home': { 'path': '/', 'defaults': { 'controller': 'tests.watson.mvc.support.TestController' } } }, 'views': { 'templates': { 'watson/mvc/test_applications/testcontroller/post': 'blank' } } }) response = application(sample_environ(PATH_INFO='/', REQUEST_METHOD='POST', HTTP_ACCEPT='application/json'), start_response) assert response == [b'{"content": "Posted Hello World!"}']
def test_application_logic_error(self): application = applications.Http({ 'routes': { 'home': { 'path': '/', 'defaults': { 'controller': 'tests.watson.mvc.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_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_short_circuit(self): environ = sample_environ() route = Route('test', path='/', options={ 'controller': 'tests.watson.mvc.support.ShortCircuitedController' }) match = RouteMatch(route, { 'controller': 'tests.watson.mvc.support.ShortCircuitedController' }) event = Event('something', params={ 'route_match': match, 'container': IocContainer(), 'request': create_request_from_environ(environ) }) listener = listeners.DispatchExecute({'404': 'page/404'}) response = listener(event) assert isinstance(response, Response)
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_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')