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).matched assert not route.match(valid_request_no_param).matched assert route.match(valid_request_with_param).matched
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_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_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_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 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_create_route(self): route = Route(name='home', path='/') assert route.path == '/' assert route.name == 'home' assert repr( route) == '<watson.mvc.routing.Route name:home path:/ match:\/$>'
def test_execute(self): route = Route('test', path='/') match = RouteMatch(route, {}) event = Event('something', params={'route_match': match}) listener = listeners.DispatchExecute({'404': 'page/404'}) listener(event)
def test_assemble_segment_route_missing_param(self): route = Route(name='test', path='/search/:keyword') route.assemble()
def test_segment_bracket_mismatch(self): Route(name='mismatch', path='/search:keyword]')
def test_assemble_static_route(self): route = Route(name='test', path='/testing') assert route.assemble() == '/testing'
def test_assemble_segment_route(self): route = Route(name='test', path='/search[/:keyword]') assert route.assemble(keyword='test') == '/search/test'
def test_assemble_segment_not_required(self): route = Route(name='test', path='/search[/:keyword]') assert route.assemble() == '/search' route2 = Route(name='test', path='/search[/:keyword]') assert route2.assemble(keyword='test') == '/search/test'
def test_create_route_regex(self): route = Route(name='home', path='/', regex='.*') assert route.regex
def test_assemble_segment_route_missing_param(self): route = Route( name='test', path='/search/:keyword', requires={'keyword': '.*'}) route.assemble()