1 # -*- coding: utf-8 -*-
4 from mock
import MagicMock
, patch
5 from webtest
import TestApp
8 from ..handler
import BaseHandler
, route
11 class TestIntegration(unittest
.TestCase
):
12 def test_wsgi_app(self
):
13 # Given a Handler class that implements a route
14 some_plugin
= MagicMock()
16 class MyHandler(BaseHandler
):
18 plugins
= [some_plugin
]
20 @route('get', '/some/path')
24 route_mock
= MagicMock()
25 with
patch(handler
.__name
__+'.Bottle.route', route_mock
):
26 # When we try to access wsgi_app for the first time
27 my_handler
= MyHandler()
28 assert my_handler
.wsgi_app
29 # then bottle.route should be called with the right arguments
30 route_mock
.assert_called_once_with('/42/some/path', method
='GET',
31 callback
=my_handler
.callback
,
34 # When we try to access wsgi_app for the second time
35 assert my_handler
.wsgi_app
36 # then the result should be cached
37 # and bottle.route should not be called again
38 self
.assertEqual(route_mock
.call_count
, 1)
40 def test_route_created(self
):
41 # Given a Handler class, as in the example documentation
42 class MyHandler(BaseHandler
):
46 @route('GET', '/some/path/<param>')
47 def callback(self
, param
):
48 return '{} + {}'.format(self
.value
, param
)
50 # when this class is used to generate a webapp
51 app
= TestApp(MyHandler().wsgi_app
)
53 # then the defined URLs should be available
54 response
= app
.get('/some/path/0')
55 self
.assertEqual(response
.status_code
, 200)
56 # and the callbacks should have access to ``self``
57 response
.mustcontain('42 + 0')
59 def test_url_base(self
):
60 # Given a Handler class that allows url_base customization
61 class MyHandler(BaseHandler
):
62 def __init__(self
, url_base
):
63 self
.url_base
= url_base
65 @route('GET', '/some/path/<param>')
66 def callback(self
, param
):
69 # when this class is used to generate a webapp
70 app
= TestApp(MyHandler('/prefix').wsgi_app
)
72 # then the prefixed URLs should be available
73 response
= app
.get('/prefix/some/path/content')
74 self
.assertEqual(response
.status_code
, 200)
75 response
.mustcontain('content')
77 def test_starting_param(self
):
78 # Given a Handler class with a route beginning with a param
79 class MyHandler(BaseHandler
):
80 @route('GET', '/<param>/some/path')
81 def callback(self
, param
):
82 return '**{}**'.format(param
)
84 # is used to generate a webapp
85 app
= TestApp(MyHandler().wsgi_app
)
87 # when the defined URLs is accessed
88 response
= app
.get('/42/some/path')
89 # Then no error should happen
90 self
.assertEqual(response
.status_code
, 200)
91 response
.mustcontain('**42**')
94 if __name__
== '__main__':