Revert "Removing deprecated/unused/outdated code"
[osm/RO.git] / RO / osm_ro / http_tools / tests / test_handler.py
1 # -*- coding: utf-8 -*-
2 ##
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 # under the License.
14 ##
15
16 import unittest
17
18 from mock import MagicMock, patch
19 from webtest import TestApp
20
21 from .. import handler
22 from ..handler import BaseHandler, route
23
24
25 class TestIntegration(unittest.TestCase):
26 def test_wsgi_app(self):
27 # Given a Handler class that implements a route
28 some_plugin = MagicMock()
29
30 class MyHandler(BaseHandler):
31 url_base = '/42'
32 plugins = [some_plugin]
33
34 @route('get', '/some/path')
35 def callback(self):
36 return 'some content'
37
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,
46 apply=[some_plugin])
47
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)
53
54 def test_route_created(self):
55 # Given a Handler class, as in the example documentation
56 class MyHandler(BaseHandler):
57 def __init__(self):
58 self.value = 42
59
60 @route('GET', '/some/path/<param>')
61 def callback(self, param):
62 return '{} + {}'.format(self.value, param)
63
64 # when this class is used to generate a webapp
65 app = TestApp(MyHandler().wsgi_app)
66
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')
72
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
78
79 @route('GET', '/some/path/<param>')
80 def callback(self, param):
81 return param
82
83 # when this class is used to generate a webapp
84 app = TestApp(MyHandler('/prefix').wsgi_app)
85
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')
90
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)
97
98 # is used to generate a webapp
99 app = TestApp(MyHandler().wsgi_app)
100
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**')
106
107
108 if __name__ == '__main__':
109 unittest.main()