我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用werkzeug.routing.RequestRedirect()。
def dispatch_url(self, url_string): url, url_adapter, query_args = self.parse_url(url_string) try: endpoint, kwargs = url_adapter.match() except NotFound: raise NotSupported(url_string) except RequestRedirect as e: new_url = "{0.new_url}?{1}".format(e, url_encode(query_args)) return self.dispatch_url(new_url) try: handler = import_string(endpoint) request = Request(url=url, args=query_args) return handler(request, **kwargs) except RequestRedirect as e: return self.dispatch_url(e.new_url)
def raise_routing_exception(self, request): """Exceptions that are recording during routing are reraised with this method. During debug we are not reraising redirect requests for non ``GET``, ``HEAD``, or ``OPTIONS`` requests and we're raising a different error instead to help debug situations. :internal: """ if not self.debug \ or not isinstance(request.routing_exception, RequestRedirect) \ or request.method in ('GET', 'HEAD', 'OPTIONS'): raise request.routing_exception from .debughelpers import FormDataRoutingRedirect raise FormDataRoutingRedirect(request)
def wsgi_app(self, environ, start_response): route = self.router.bind_to_environ(environ) try: endpoint, args = route.match() except RequestRedirect as e: return e except HTTPException: return NotFound() request = Request(environ) args = request.args response = Response() response.mimetype = 'text/plain' response.status_code = 200 if endpoint == 'contestant': if 'mac' not in args or 'row' not in args or 'col' not in args: response.status_code = 400 response.data = 'Required query parameters: mac, row, col' else: mac = args['mac'] row = args['row'] col = args['col'] self.add_contestant(mac, row, col, response) elif endpoint == 'worker': if 'mac' not in args or 'num' not in args: response.status_code = 400 response.data = 'Required query parameters: mac, num' else: mac = args['mac'] num = args['num'] self.add_worker(mac, num, response) elif endpoint == 'reboot_timestamp': response.data = str(self.reboot_string) return response
def wsgi_app(self, environ, start_response): route = self.router.bind_to_environ(environ) try: endpoint, args = route.match() except RequestRedirect as e: return e except HTTPException: return NotFound() request = Request(environ) get_args = dict(request.args) if endpoint == 'wipe': get_args['wipe'] = 'pixie_wipe=force' else: get_args['wipe'] = "" response = Response() response.mimetype = 'text/plain' response.status_code = 200 config = None if 'ip' in get_args: ip_addr = ipaddress.ip_address(get_args['ip'][0]) for cfg in self.configs: if ip_addr in cfg['subnet']: config = cfg if config is None: response.data = CONFIGSCRIPT.format(**self.default_config) else: for (k, v) in config.items(): get_args[k] = v response.data = BOOTSCRIPT.format(**get_args) return response
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'
def test_path(self): map = r.Map([ r.Rule('/', defaults={'name': 'FrontPage'}, endpoint='page'), r.Rule('/Special', endpoint='special'), r.Rule('/<int:year>', endpoint='year'), r.Rule('/<path:name>', endpoint='page'), r.Rule('/<path:name>/edit', endpoint='editpage'), r.Rule('/<path:name>/silly/<path:name2>', endpoint='sillypage'), r.Rule('/<path:name>/silly/<path:name2>/edit', endpoint='editsillypage'), r.Rule('/Talk:<path:name>', endpoint='talk'), r.Rule('/User:<username>', endpoint='user'), r.Rule('/User:<username>/<path:name>', endpoint='userpage'), r.Rule('/Files/<path:file>', endpoint='files'), ]) adapter = map.bind('example.org', '/') assert adapter.match('/') == ('page', {'name':'FrontPage'}) self.assert_raises(r.RequestRedirect, lambda: adapter.match('/FrontPage')) assert adapter.match('/Special') == ('special', {}) assert adapter.match('/2007') == ('year', {'year':2007}) assert adapter.match('/Some/Page') == ('page', {'name':'Some/Page'}) assert adapter.match('/Some/Page/edit') == ('editpage', {'name':'Some/Page'}) assert adapter.match('/Foo/silly/bar') == ('sillypage', {'name':'Foo', 'name2':'bar'}) assert adapter.match('/Foo/silly/bar/edit') == ('editsillypage', {'name':'Foo', 'name2':'bar'}) assert adapter.match('/Talk:Foo/Bar') == ('talk', {'name':'Foo/Bar'}) assert adapter.match('/User:thomas') == ('user', {'username':'thomas'}) assert adapter.match('/User:thomas/projects/werkzeug') == \ ('userpage', {'username':'thomas', 'name':'projects/werkzeug'}) assert adapter.match('/Files/downloads/werkzeug/0.2.zip') == \ ('files', {'file':'downloads/werkzeug/0.2.zip'})
def test_request_direct_charset_bug(self): map = r.Map([r.Rule(u'/öäü/')]) adapter = map.bind('localhost', '/') try: adapter.match(u'/öäü') except r.RequestRedirect as e: assert e.new_url == 'http://localhost/%C3%B6%C3%A4%C3%BC/' else: self.fail('expected request redirect exception')
def test_request_redirect_default(self): map = r.Map([r.Rule(u'/foo', defaults={'bar': 42}), r.Rule(u'/foo/<int:bar>')]) adapter = map.bind('localhost', '/') try: adapter.match(u'/foo/42') except r.RequestRedirect as e: assert e.new_url == 'http://localhost/foo' else: self.fail('expected request redirect exception')
def test_alias_redirects(self): m = r.Map([ r.Rule('/', endpoint='index'), r.Rule('/index.html', endpoint='index', alias=True), r.Rule('/users/', defaults={'page': 1}, endpoint='users'), r.Rule('/users/index.html', defaults={'page': 1}, alias=True, endpoint='users'), r.Rule('/users/page/<int:page>', endpoint='users'), r.Rule('/users/page-<int:page>.html', alias=True, endpoint='users'), ]) a = m.bind('example.com') def ensure_redirect(path, new_url, args=None): try: a.match(path, query_args=args) except r.RequestRedirect as e: assert e.new_url == 'http://example.com' + new_url else: assert False, 'expected redirect' ensure_redirect('/index.html', '/') ensure_redirect('/users/index.html', '/users/') ensure_redirect('/users/page-2.html', '/users/page/2') ensure_redirect('/users/page-1.html', '/users/') ensure_redirect('/users/page-1.html', '/users/?foo=bar', {'foo': 'bar'}) assert a.build('index') == '/' assert a.build('users', {'page': 1}) == '/users/' assert a.build('users', {'page': 2}) == '/users/page/2'
def test_host_matching(self): m = r.Map([ r.Rule('/', endpoint='index', host='www.<domain>'), r.Rule('/', endpoint='files', host='files.<domain>'), r.Rule('/foo/', defaults={'page': 1}, host='www.<domain>', endpoint='x'), r.Rule('/<int:page>', host='files.<domain>', endpoint='x') ], host_matching=True) a = m.bind('www.example.com') assert a.match('/') == ('index', {'domain': 'example.com'}) assert a.match('/foo/') == ('x', {'domain': 'example.com', 'page': 1}) try: a.match('/foo') except r.RequestRedirect as e: assert e.new_url == 'http://www.example.com/foo/' else: assert False, 'expected redirect' a = m.bind('files.example.com') assert a.match('/') == ('files', {'domain': 'example.com'}) assert a.match('/2') == ('x', {'domain': 'example.com', 'page': 2}) try: a.match('/1') except r.RequestRedirect as e: assert e.new_url == 'http://www.example.com/foo/' else: assert False, 'expected redirect'
def test_redirect_request_exception_code(self): exc = r.RequestRedirect('http://www.google.com/') exc.code = 307 env = create_environ() self.assert_strict_equal(exc.get_response(env).status_code, exc.code)
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')
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/')