Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for redirect routes #7

Merged
merged 1 commit into from
Sep 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion corker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
from routes.util import URLGenerator
from webob import exc
from webob.dec import wsgify
from webob.response import Response

from corker.controller import BaseController


log = logging.getLogger(__name__)


class Application(object):
"""Give it a mapper, and it will return a wsgi app that gets routes via
the mapper and executes them."""
Expand All @@ -21,9 +26,29 @@ def __call__(self, req):
results = self.map.routematch(environ=req.environ)
if not results:
return exc.HTTPNotFound()

match, route = results
link = URLGenerator(self.map, req.environ)

if route.redirect:
# Taken from the routes middleware module
route_name = '_redirect_%s' % id(route)
location = link(route_name, **match)

# Build the response manually so we don't have to try to map the
# route status to a specific webob exception
redirect_response = Response(status=route.redirect_status)
redirect_response.location = location

return redirect_response

match_controller = match.get('controller', None)

if not callable(match_controller):
log.error('Unsupported route match: %s', match)
return exc.HTTPNotFound()

req.urlvars = ((), match)
req.link = link
controller = match['controller'](req, **self.config)
controller = match_controller(req, **self.config)
return controller()
2 changes: 0 additions & 2 deletions corker/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def setup_routes(cls, mapper, prefix='/', config={}):
for route in attr._route:
(args, kw) = route
kw['action'] = attr_name
print("Map:", args, kw)
m.connect(*args, **kw)


def __init__(self, request, **config):
self.request = request
for name, value in config.items():
Expand Down
39 changes: 39 additions & 0 deletions corker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,42 @@ def view(self, request, item):

ret = app.get('/view/4')
eq_(ret.body, b"Hi view 4!\n{'bdb': 5}\n4")


def test_redirect():
mapper = Mapper()
mapper.redirect('/foo', '/bar',
_redirect_code="301 Moved Permanently")

test_app = Application(mapper)

app = TestApp(test_app)

response = app.get('/foo')

eq_(response.status, '301 Moved Permanently')
eq_(response.location, 'http://localhost/bar')


def test_bad_route():
mapper = Mapper()

mapper.connect('foo', '/foo')

test_app = Application(mapper)

app = TestApp(test_app)

app.get('/foo', status=404)


def test_not_found():
mapper = Mapper()

mapper.connect('foo', '/foo')

test_app = Application(mapper)

app = TestApp(test_app)

app.get('/bar', status=404)