1 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
18 from mock
import MagicMock
, patch
19 from webtest
import TestApp
21 from .. import handler
22 from ..handler
import BaseHandler
, route
25 class TestIntegration(unittest
.TestCase
):
26 def test_wsgi_app(self
):
27 # Given a Handler class that implements a route
28 some_plugin
= MagicMock()
30 class MyHandler(BaseHandler
):
32 plugins
= [some_plugin
]
34 @route('get', '/some/path')
38 route_mock
= MagicMock()
39 with
patch(handler
.__name
__+'.Bottle.route', route_mock
):
40 # When we try to access wsgi_app for the first time
41 my_handler
= MyHandler()
42 assert my_handler
.wsgi_app
43 # then bottle.route should be called with the right arguments
44 route_mock
.assert_called_once_with('/42/some/path', method
='GET',
45 callback
=my_handler
.callback
,
48 # When we try to access wsgi_app for the second time
49 assert my_handler
.wsgi_app
50 # then the result should be cached
51 # and bottle.route should not be called again
52 self
.assertEqual(route_mock
.call_count
, 1)
54 def test_route_created(self
):
55 # Given a Handler class, as in the example documentation
56 class MyHandler(BaseHandler
):
60 @route('GET', '/some/path/<param>')
61 def callback(self
, param
):
62 return '{} + {}'.format(self
.value
, param
)
64 # when this class is used to generate a webapp
65 app
= TestApp(MyHandler().wsgi_app
)
67 # then the defined URLs should be available
68 response
= app
.get('/some/path/0')
69 self
.assertEqual(response
.status_code
, 200)
70 # and the callbacks should have access to ``self``
71 response
.mustcontain('42 + 0')
73 def test_url_base(self
):
74 # Given a Handler class that allows url_base customization
75 class MyHandler(BaseHandler
):
76 def __init__(self
, url_base
):
77 self
.url_base
= url_base
79 @route('GET', '/some/path/<param>')
80 def callback(self
, param
):
83 # when this class is used to generate a webapp
84 app
= TestApp(MyHandler('/prefix').wsgi_app
)
86 # then the prefixed URLs should be available
87 response
= app
.get('/prefix/some/path/content')
88 self
.assertEqual(response
.status_code
, 200)
89 response
.mustcontain('content')
91 def test_starting_param(self
):
92 # Given a Handler class with a route beginning with a param
93 class MyHandler(BaseHandler
):
94 @route('GET', '/<param>/some/path')
95 def callback(self
, param
):
96 return '**{}**'.format(param
)
98 # is used to generate a webapp
99 app
= TestApp(MyHandler().wsgi_app
)
101 # when the defined URLs is accessed
102 response
= app
.get('/42/some/path')
103 # Then no error should happen
104 self
.assertEqual(response
.status_code
, 200)
105 response
.mustcontain('**42**')
108 if __name__
== '__main__':