Python werkzeug.routing.Map() Examples
The following are 30
code examples of werkzeug.routing.Map().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
werkzeug.routing
, or try the search function
.
Example #1
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_double_defaults(self): for prefix in '', '/aaa': m = r.Map([ r.Rule(prefix + '/', defaults={'foo': 1, 'bar': False}, endpoint='x'), r.Rule(prefix + '/<int:foo>', defaults={'bar': False}, endpoint='x'), r.Rule(prefix + '/bar/', defaults={'foo': 1, 'bar': True}, endpoint='x'), r.Rule(prefix + '/bar/<int:foo>', defaults={'bar': True}, endpoint='x') ]) a = m.bind('example.com') assert a.match(prefix + '/') == ('x', {'foo': 1, 'bar': False}) assert a.match(prefix + '/2') == ('x', {'foo': 2, 'bar': False}) assert a.match(prefix + '/bar/') == ('x', {'foo': 1, 'bar': True}) assert a.match(prefix + '/bar/2') == ('x', {'foo': 2, 'bar': True}) assert a.build('x', {'foo': 1, 'bar': False}) == prefix + '/' assert a.build('x', {'foo': 2, 'bar': False}) == prefix + '/2' assert a.build('x', {'bar': False}) == prefix + '/' assert a.build('x', {'foo': 1, 'bar': True}) == prefix + '/bar/' assert a.build('x', {'foo': 2, 'bar': True}) == prefix + '/bar/2' assert a.build('x', {'bar': True}) == prefix + '/bar/'
Example #2
Source File: http.py From prometheus-pve-exporter with Apache License 2.0 | 6 votes |
def __init__(self, config, duration, errors): self._config = config self._duration = duration self._errors = errors self._url_map = Map([ Rule('/', endpoint='index'), Rule('/metrics', endpoint='metrics'), Rule('/pve', endpoint='pve'), ]) self._args = { 'pve': ['module', 'target'] } self._views = { 'index': self.on_index, 'metrics': self.on_metrics, 'pve': self.on_pve, } self._log = logging.getLogger(__name__)
Example #3
Source File: webserver.py From nichtparasoup with MIT License | 6 votes |
def __init__(self, imageserver: Server, hostname: str, port: int, *, developer_mode: bool = False) -> None: # pragma: no cover """ :param imageserver: The imageserver to represent. :param hostname: The hostname to bind to. :param port: The port to bind to. :param developer_mode: Run in insecure web-developer mode; sets CORS to "*". """ self.developer_mode = developer_mode self.imageserver = imageserver self.hostname = hostname self.port = port self.url_map = Map([ Rule('/', endpoint='root'), Rule('/get', endpoint='get'), Rule('/status', endpoint='status'), Rule('/status/<what>', endpoint='status_what'), Rule('/reset', endpoint='reset'), Rule('/css/sourceIcons.css', endpoint='sourceicons') ])
Example #4
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_double_defaults(self): for prefix in '', '/aaa': m = r.Map([ r.Rule(prefix + '/', defaults={'foo': 1, 'bar': False}, endpoint='x'), r.Rule(prefix + '/<int:foo>', defaults={'bar': False}, endpoint='x'), r.Rule(prefix + '/bar/', defaults={'foo': 1, 'bar': True}, endpoint='x'), r.Rule(prefix + '/bar/<int:foo>', defaults={'bar': True}, endpoint='x') ]) a = m.bind('example.com') assert a.match(prefix + '/') == ('x', {'foo': 1, 'bar': False}) assert a.match(prefix + '/2') == ('x', {'foo': 2, 'bar': False}) assert a.match(prefix + '/bar/') == ('x', {'foo': 1, 'bar': True}) assert a.match(prefix + '/bar/2') == ('x', {'foo': 2, 'bar': True}) assert a.build('x', {'foo': 1, 'bar': False}) == prefix + '/' assert a.build('x', {'foo': 2, 'bar': False}) == prefix + '/2' assert a.build('x', {'bar': False}) == prefix + '/' assert a.build('x', {'foo': 1, 'bar': True}) == prefix + '/bar/' assert a.build('x', {'foo': 2, 'bar': True}) == prefix + '/bar/2' assert a.build('x', {'bar': True}) == prefix + '/bar/'
Example #5
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_server_name_interpolation(self): server_name = 'example.invalid' map = r.Map([r.Rule('/', endpoint='index'), r.Rule('/', endpoint='alt', subdomain='alt')]) env = create_environ('/', 'http://%s/' % server_name) adapter = map.bind_to_environ(env, server_name=server_name) assert adapter.match() == ('index', {}) env = create_environ('/', 'http://alt.%s/' % server_name) adapter = map.bind_to_environ(env, server_name=server_name) assert adapter.match() == ('alt', {}) env = create_environ('/', 'http://%s/' % server_name) adapter = map.bind_to_environ(env, server_name='foo') assert adapter.subdomain == '<invalid>'
Example #6
Source File: tfserve.py From tfserve with MIT License | 6 votes |
def _init_app(self, middleware=None): """Initialize a WSGI application for handling POST to '/'. `middleware` may be provided as WSGI middleware. """ routes = routing.Map([ routing.Rule('/', endpoint=self._handle_inference), routing.Rule('/ping', endpoint=self._handle_ping), routing.Rule('/shutdown', endpoint=self._handle_shutdown), ]) def app(env, start_resp): """WSGI application to handle server requests. """ urls = routes.bind_to_environ(env) try: handler, _kw = urls.match() req = Request(env) if middleware: return middleware(handler, req)(env, start_resp) return handler(req)(env, start_resp) except HTTPException as e: return e(env, start_resp) return app
Example #7
Source File: flask_sockets.py From PhonePi_SampleServer with MIT License | 6 votes |
def __init__(self, app=None): #: Compatibility with 'Flask' application. #: The :class:`~werkzeug.routing.Map` for this instance. You can use #: this to change the routing converters after the class was created #: but before any routes are connected. self.url_map = Map() #: Compatibility with 'Flask' application. #: All the attached blueprints in a dictionary by name. Blueprints #: can be attached multiple times so this dictionary does not tell #: you how often they got attached. self.blueprints = {} self._blueprint_order = [] if app: self.init_app(app)
Example #8
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_method_fallback(self): map = r.Map([ r.Rule('/', endpoint='index', methods=['GET']), r.Rule('/<name>', endpoint='hello_name', methods=['GET']), r.Rule('/select', endpoint='hello_select', methods=['POST']), r.Rule('/search_get', endpoint='search', methods=['GET']), r.Rule('/search_post', endpoint='search', methods=['POST']) ]) adapter = map.bind('example.com') assert adapter.build('index') == '/' assert adapter.build('index', method='GET') == '/' assert adapter.build('hello_name', {'name': 'foo'}) == '/foo' assert adapter.build('hello_select') == '/select' assert adapter.build('hello_select', method='POST') == '/select' assert adapter.build('search') == '/search_get' assert adapter.build('search', method='GET') == '/search_get' assert adapter.build('search', method='POST') == '/search_post'
Example #9
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_dispatch(self): env = create_environ('/') map = r.Map([ r.Rule('/', endpoint='root'), r.Rule('/foo/', endpoint='foo') ]) adapter = map.bind_to_environ(env) raise_this = None def view_func(endpoint, values): if raise_this is not None: raise raise_this return Response(repr((endpoint, values))) dispatch = lambda p, q=False: Response.force_type(adapter.dispatch(view_func, p, catch_http_exceptions=q), env) assert dispatch('/').data == b"('root', {})" assert dispatch('/foo').status_code == 301 raise_this = r.NotFound() self.assert_raises(r.NotFound, lambda: dispatch('/bar')) assert dispatch('/bar', True).status_code == 404
Example #10
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_http_host_before_server_name(self): env = { 'HTTP_HOST': 'wiki.example.com', 'SERVER_NAME': 'web0.example.com', 'SERVER_PORT': '80', 'SCRIPT_NAME': '', 'PATH_INFO': '', 'REQUEST_METHOD': 'GET', 'wsgi.url_scheme': 'http' } map = r.Map([r.Rule('/', endpoint='index', subdomain='wiki')]) adapter = map.bind_to_environ(env, server_name='example.com') assert adapter.match('/') == ('index', {}) assert adapter.build('index', force_external=True) == 'http://wiki.example.com/' assert adapter.build('index') == '/' env['HTTP_HOST'] = 'admin.example.com' adapter = map.bind_to_environ(env, server_name='example.com') assert adapter.build('index') == 'http://wiki.example.com/'
Example #11
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_http_host_before_server_name(self): env = { 'HTTP_HOST': 'wiki.example.com', 'SERVER_NAME': 'web0.example.com', 'SERVER_PORT': '80', 'SCRIPT_NAME': '', 'PATH_INFO': '', 'REQUEST_METHOD': 'GET', 'wsgi.url_scheme': 'http' } map = r.Map([r.Rule('/', endpoint='index', subdomain='wiki')]) adapter = map.bind_to_environ(env, server_name='example.com') assert adapter.match('/') == ('index', {}) assert adapter.build('index', force_external=True) == 'http://wiki.example.com/' assert adapter.build('index') == '/' env['HTTP_HOST'] = 'admin.example.com' adapter = map.bind_to_environ(env, server_name='example.com') assert adapter.build('index') == 'http://wiki.example.com/'
Example #12
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_dispatch(self): env = create_environ('/') map = r.Map([ r.Rule('/', endpoint='root'), r.Rule('/foo/', endpoint='foo') ]) adapter = map.bind_to_environ(env) raise_this = None def view_func(endpoint, values): if raise_this is not None: raise raise_this return Response(repr((endpoint, values))) dispatch = lambda p, q=False: Response.force_type(adapter.dispatch(view_func, p, catch_http_exceptions=q), env) assert dispatch('/').data == b"('root', {})" assert dispatch('/foo').status_code == 301 raise_this = r.NotFound() self.assert_raises(r.NotFound, lambda: dispatch('/bar')) assert dispatch('/bar', True).status_code == 404
Example #13
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_server_name_interpolation(self): server_name = 'example.invalid' map = r.Map([r.Rule('/', endpoint='index'), r.Rule('/', endpoint='alt', subdomain='alt')]) env = create_environ('/', 'http://%s/' % server_name) adapter = map.bind_to_environ(env, server_name=server_name) assert adapter.match() == ('index', {}) env = create_environ('/', 'http://alt.%s/' % server_name) adapter = map.bind_to_environ(env, server_name=server_name) assert adapter.match() == ('alt', {}) env = create_environ('/', 'http://%s/' % server_name) adapter = map.bind_to_environ(env, server_name='foo') assert adapter.subdomain == '<invalid>'
Example #14
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_method_fallback(self): map = r.Map([ r.Rule('/', endpoint='index', methods=['GET']), r.Rule('/<name>', endpoint='hello_name', methods=['GET']), r.Rule('/select', endpoint='hello_select', methods=['POST']), r.Rule('/search_get', endpoint='search', methods=['GET']), r.Rule('/search_post', endpoint='search', methods=['POST']) ]) adapter = map.bind('example.com') assert adapter.build('index') == '/' assert adapter.build('index', method='GET') == '/' assert adapter.build('hello_name', {'name': 'foo'}) == '/foo' assert adapter.build('hello_select') == '/select' assert adapter.build('hello_select', method='POST') == '/select' assert adapter.build('search') == '/search_get' assert adapter.build('search', method='GET') == '/search_get' assert adapter.build('search', method='POST') == '/search_post'
Example #15
Source File: routing.py From Flask with Apache License 2.0 | 6 votes |
def test_server_name_casing(self): m = r.Map([ r.Rule('/', endpoint='index', subdomain='foo') ]) env = create_environ() env['SERVER_NAME'] = env['HTTP_HOST'] = 'FOO.EXAMPLE.COM' a = m.bind_to_environ(env, server_name='example.com') assert a.match('/') == ('index', {}) env = create_environ() env['SERVER_NAME'] = '127.0.0.1' env['SERVER_PORT'] = '5000' del env['HTTP_HOST'] a = m.bind_to_environ(env, server_name='example.com') try: a.match() except r.NotFound: pass else: assert False, 'Expected not found exception'
Example #16
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_external_building_with_port_bind_to_environ_wrong_servername(self): map = r.Map([ r.Rule('/', endpoint='index'), ]) environ = create_environ('/', 'http://example.org:5000/') adapter = map.bind_to_environ(environ, server_name="example.org") assert adapter.subdomain == '<invalid>'
Example #17
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_greedy(self): map = r.Map([ r.Rule('/foo', endpoint='foo'), r.Rule('/<path:bar>', endpoint='bar'), r.Rule('/<path:bar>/<path:blub>', endpoint='bar') ]) adapter = map.bind('example.org', '/') assert adapter.match('/foo') == ('foo', {}) assert adapter.match('/blub') == ('bar', {'bar': 'blub'}) assert adapter.match('/he/he') == ('bar', {'bar': 'he', 'blub': 'he'}) assert adapter.build('foo', {}) == '/foo' assert adapter.build('bar', {'bar': 'blub'}) == '/blub' assert adapter.build('bar', {'bar': 'blub', 'blub': 'bar'}) == '/blub/bar'
Example #18
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_redirect_path_quoting(self): url_map = r.Map([ r.Rule('/<category>', defaults={'page': 1}, endpoint='category'), r.Rule('/<category>/page/<int:page>', endpoint='category') ]) adapter = url_map.bind('example.com') try: adapter.match('/foo bar/page/1') except r.RequestRedirect as e: response = e.get_response({}) self.assert_strict_equal(response.headers['location'], u'http://example.com/foo%20bar') else: self.fail('Expected redirect')
Example #19
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_defaults(self): map = r.Map([ r.Rule('/foo/', defaults={'page': 1}, endpoint='foo'), r.Rule('/foo/<int:page>', endpoint='foo') ]) adapter = map.bind('example.org', '/') assert adapter.match('/foo/') == ('foo', {'page': 1}) self.assert_raises(r.RequestRedirect, lambda: adapter.match('/foo/1')) assert adapter.match('/foo/2') == ('foo', {'page': 2}) assert adapter.build('foo', {}) == '/foo/' assert adapter.build('foo', {'page': 1}) == '/foo/' assert adapter.build('foo', {'page': 2}) == '/foo/2'
Example #20
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_basic_building(self): map = r.Map([ r.Rule('/', endpoint='index'), r.Rule('/foo', endpoint='foo'), r.Rule('/bar/<baz>', endpoint='bar'), r.Rule('/bar/<int:bazi>', endpoint='bari'), r.Rule('/bar/<float:bazf>', endpoint='barf'), r.Rule('/bar/<path:bazp>', endpoint='barp'), r.Rule('/hehe', endpoint='blah', subdomain='blah') ]) adapter = map.bind('example.org', '/', subdomain='blah') assert adapter.build('index', {}) == 'http://example.org/' assert adapter.build('foo', {}) == 'http://example.org/foo' assert adapter.build('bar', {'baz': 'blub'}) == 'http://example.org/bar/blub' assert adapter.build('bari', {'bazi': 50}) == 'http://example.org/bar/50' assert adapter.build('barf', {'bazf': 0.815}) == 'http://example.org/bar/0.815' assert adapter.build('barp', {'bazp': 'la/di'}) == 'http://example.org/bar/la/di' assert adapter.build('blah', {}) == '/hehe' self.assert_raises(r.BuildError, lambda: adapter.build('urks')) adapter = map.bind('example.org', '/test', subdomain='blah') assert adapter.build('index', {}) == 'http://example.org/test/' assert adapter.build('foo', {}) == 'http://example.org/test/foo' assert adapter.build('bar', {'baz': 'blub'}) == 'http://example.org/test/bar/blub' assert adapter.build('bari', {'bazi': 50}) == 'http://example.org/test/bar/50' assert adapter.build('barf', {'bazf': 0.815}) == 'http://example.org/test/bar/0.815' assert adapter.build('barp', {'bazp': 'la/di'}) == 'http://example.org/test/bar/la/di' assert adapter.build('blah', {}) == '/test/hehe'
Example #21
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_environ_nonascii_pathinfo(self): environ = create_environ(u'/лошадь') m = r.Map([ r.Rule(u'/', endpoint='index'), r.Rule(u'/лошадь', endpoint='horse') ]) a = m.bind_to_environ(environ) self.assert_strict_equal(a.match(u'/'), ('index', {})) self.assert_strict_equal(a.match(u'/лошадь'), ('horse', {})) self.assert_raises(r.NotFound, a.match, u'/барсук')
Example #22
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_environ_defaults(self): environ = create_environ("/foo") self.assert_strict_equal(environ["PATH_INFO"], '/foo') m = r.Map([r.Rule("/foo", endpoint="foo"), r.Rule("/bar", endpoint="bar")]) a = m.bind_to_environ(environ) self.assert_strict_equal(a.match("/foo"), ('foo', {})) self.assert_strict_equal(a.match(), ('foo', {})) self.assert_strict_equal(a.match("/bar"), ('bar', {})) self.assert_raises(r.NotFound, a.match, "/bars")
Example #23
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_unicode_rules(self): m = r.Map([ r.Rule(u'/войти/', endpoint='enter'), r.Rule(u'/foo+bar/', endpoint='foobar') ]) a = m.bind(u'☃.example.com') try: a.match(u'/войти') except r.RequestRedirect as e: self.assert_strict_equal(e.new_url, 'http://xn--n3h.example.com/' '%D0%B2%D0%BE%D0%B9%D1%82%D0%B8/') endpoint, values = a.match(u'/войти/') self.assert_strict_equal(endpoint, 'enter') self.assert_strict_equal(values, {}) try: a.match(u'/foo+bar') except r.RequestRedirect as e: self.assert_strict_equal(e.new_url, 'http://xn--n3h.example.com/' 'foo+bar/') endpoint, values = a.match(u'/foo+bar/') self.assert_strict_equal(endpoint, 'foobar') self.assert_strict_equal(values, {}) url = a.build('enter', {}, force_external=True) self.assert_strict_equal(url, 'http://xn--n3h.example.com/%D0%B2%D0%BE%D0%B9%D1%82%D0%B8/') url = a.build('foobar', {}, force_external=True) self.assert_strict_equal(url, 'http://xn--n3h.example.com/foo+bar/')
Example #24
Source File: wrappers.py From Flask with Apache License 2.0 | 5 votes |
def test_reverse_slash_behavior(self): class MyRequest(wrappers.ReverseSlashBehaviorRequestMixin, Request): pass req = MyRequest.from_values('/foo/bar', 'http://example.com/test') assert req.url == 'http://example.com/test/foo/bar' assert req.path == 'foo/bar' assert req.script_root == '/test/' # make sure the routing system works with the slashes in # reverse order as well. map = routing.Map([routing.Rule('/foo/bar', endpoint='foo')]) adapter = map.bind_to_environ(req.environ) assert adapter.match() == ('foo', {}) adapter = map.bind(req.host, req.script_root) assert adapter.match(req.path) == ('foo', {})
Example #25
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_map_repr(self): m = r.Map([ r.Rule(u'/wat', endpoint='enter'), r.Rule(u'/woop', endpoint='foobar') ]) rv = repr(m) self.assert_strict_equal(rv, "Map([<Rule '/woop' -> foobar>, <Rule '/wat' -> enter>])")
Example #26
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_unicode_rules(self): m = r.Map([ r.Rule(u'/войти/', endpoint='enter'), r.Rule(u'/foo+bar/', endpoint='foobar') ]) a = m.bind(u'☃.example.com') try: a.match(u'/войти') except r.RequestRedirect as e: self.assert_strict_equal(e.new_url, 'http://xn--n3h.example.com/' '%D0%B2%D0%BE%D0%B9%D1%82%D0%B8/') endpoint, values = a.match(u'/войти/') self.assert_strict_equal(endpoint, 'enter') self.assert_strict_equal(values, {}) try: a.match(u'/foo+bar') except r.RequestRedirect as e: self.assert_strict_equal(e.new_url, 'http://xn--n3h.example.com/' 'foo+bar/') endpoint, values = a.match(u'/foo+bar/') self.assert_strict_equal(endpoint, 'foobar') self.assert_strict_equal(values, {}) url = a.build('enter', {}, force_external=True) self.assert_strict_equal(url, 'http://xn--n3h.example.com/%D0%B2%D0%BE%D0%B9%D1%82%D0%B8/') url = a.build('foobar', {}, force_external=True) self.assert_strict_equal(url, 'http://xn--n3h.example.com/foo+bar/')
Example #27
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_redirect_path_quoting(self): url_map = r.Map([ r.Rule('/<category>', defaults={'page': 1}, endpoint='category'), r.Rule('/<category>/page/<int:page>', endpoint='category') ]) adapter = url_map.bind('example.com') try: adapter.match('/foo bar/page/1') except r.RequestRedirect as e: response = e.get_response({}) self.assert_strict_equal(response.headers['location'], u'http://example.com/foo%20bar') else: self.fail('Expected redirect')
Example #28
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_map_repr(self): m = r.Map([ r.Rule(u'/wat', endpoint='enter'), r.Rule(u'/woop', endpoint='foobar') ]) rv = repr(m) self.assert_strict_equal(rv, "Map([<Rule '/woop' -> foobar>, <Rule '/wat' -> enter>])")
Example #29
Source File: wrappers.py From Flask with Apache License 2.0 | 5 votes |
def test_reverse_slash_behavior(self): class MyRequest(wrappers.ReverseSlashBehaviorRequestMixin, Request): pass req = MyRequest.from_values('/foo/bar', 'http://example.com/test') assert req.url == 'http://example.com/test/foo/bar' assert req.path == 'foo/bar' assert req.script_root == '/test/' # make sure the routing system works with the slashes in # reverse order as well. map = routing.Map([routing.Rule('/foo/bar', endpoint='foo')]) adapter = map.bind_to_environ(req.environ) assert adapter.match() == ('foo', {}) adapter = map.bind(req.host, req.script_root) assert adapter.match(req.path) == ('foo', {})
Example #30
Source File: routing.py From Flask with Apache License 2.0 | 5 votes |
def test_adapter_url_parameter_sorting(self): map = r.Map([r.Rule('/', endpoint='index')], sort_parameters=True, sort_key=lambda x: x[1]) adapter = map.bind('localhost', '/') assert adapter.build('index', {'x': 20, 'y': 10, 'z': 30}, force_external=True) == 'http://localhost/?y=10&x=20&z=30'