Permalink
Browse files

api: Bottle.mount() not adds 'mountpoint' meta-data to the created ro…

…utes to allow introspection of the wrapped sub-application.

Thanks to David Buxton for the feature request and patch.
  • Loading branch information...
defnull committed May 29, 2012
1 parent 59854c0 commit 9b24401605e0470388a65c80a0964cba2bf64caf
Showing with 23 additions and 10 deletions.
  1. +14 −10 bottle.py
  2. +9 −0 test/test_mount.py
View
@@ -578,28 +578,32 @@ def mount(self, prefix, app, **options):
prefix, app = app, prefix
depr('Parameter order of Bottle.mount() changed.') # 0.10
parts = [p for p in prefix.split('/') if p]
if not parts: raise ValueError('Empty path prefix.')
path_depth = len(parts)
options.setdefault('skip', True)
options.setdefault('method', 'ANY')
segments = [p for p in prefix.split('/') if p]
if not segments: raise ValueError('Empty path prefix.')
path_depth = len(segments)
@self.route('/%s/:#.*#' % '/'.join(parts), **options)
def mountpoint():
def mountpoint_wrapper():
try:
request.path_shift(path_depth)
rs = BaseResponse([], 200)
def start_response(status, header):
rs.status = status
for name, value in header: rs.add_header(name, value)
return rs.body.append
rs.body = itertools.chain(rs.body, app(request.environ, start_response))
return HTTPResponse(rs.body, rs.status_code, rs.headers)
body = app(request.environ, start_response)
body = itertools.chain(rs.body, body)
return HTTPResponse(body, rs.status_code, rs.headers)
finally:
request.path_shift(-path_depth)
options.setdefault('skip', True)
options.setdefault('method', 'ANY')
options.setdefault('mountpoint', {'prefix': prefix, 'target': app})
options['callback'] = mountpoint_wrapper
self.route('/%s/<:re:.*>' % '/'.join(segments), **options)
if not prefix.endswith('/'):
self.route('/' + '/'.join(parts), callback=mountpoint, **options)
self.route('/' + '/'.join(segments), **options)
def merge(self, routes):
''' Merge the routes of another :class:`Bottle` application or a list of
View
@@ -20,6 +20,15 @@ def test_mount(self):
self.assertStatus(200, '/test/test/bar')
self.assertBody('bar', '/test/test/bar')
def test_mount_meta(self):
self.app.mount('/test/', self.subapp)
self.assertEqual(
self.app.routes[0].config.mountpoint['prefix'],
'/test/')
self.assertEqual(
self.app.routes[0].config.mountpoint['target'],
self.subapp)
def test_no_slash_prefix(self):
self.app.mount('/test', self.subapp)
self.assertStatus(404, '/')

0 comments on commit 9b24401

Please sign in to comment.