feature 8029 change RO to python3. Using vim plugins
[osm/RO.git] / RO / osm_ro / http_tools / tests / test_handler.py
1 # -*- coding: utf-8 -*-
2 import unittest
3
4 from mock import MagicMock, patch
5 from webtest import TestApp
6
7 from .. import handler
8 from ..handler import BaseHandler, route
9
10
11 class TestIntegration(unittest.TestCase):
12 def test_wsgi_app(self):
13 # Given a Handler class that implements a route
14 some_plugin = MagicMock()
15
16 class MyHandler(BaseHandler):
17 url_base = '/42'
18 plugins = [some_plugin]
19
20 @route('get', '/some/path')
21 def callback(self):
22 return 'some content'
23
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,
32 apply=[some_plugin])
33
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)
39
40 def test_route_created(self):
41 # Given a Handler class, as in the example documentation
42 class MyHandler(BaseHandler):
43 def __init__(self):
44 self.value = 42
45
46 @route('GET', '/some/path/<param>')
47 def callback(self, param):
48 return '{} + {}'.format(self.value, param)
49
50 # when this class is used to generate a webapp
51 app = TestApp(MyHandler().wsgi_app)
52
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')
58
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
64
65 @route('GET', '/some/path/<param>')
66 def callback(self, param):
67 return param
68
69 # when this class is used to generate a webapp
70 app = TestApp(MyHandler('/prefix').wsgi_app)
71
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')
76
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)
83
84 # is used to generate a webapp
85 app = TestApp(MyHandler().wsgi_app)
86
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**')
92
93
94 if __name__ == '__main__':
95 unittest.main()