backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / app / test_wsgi.py
1 # Copyright (C) 2013 Stratosphere Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
17
18 import unittest
19 import logging
20
21 import nose
22 from nose.tools import eq_
23
24 from ryu.app.wsgi import ControllerBase
25 from ryu.app.wsgi import WSGIApplication
26 from ryu.app.wsgi import Response
27 from ryu.app.wsgi import route
28 from ryu.lib import dpid as dpidlib
29
30 LOG = logging.getLogger('test_wsgi')
31
32
33 class _TestController(ControllerBase):
34
35     def __init__(self, req, link, data, **config):
36         super(_TestController, self).__init__(req, link, data, **config)
37         eq_(data['test_param'], 'foo')
38
39     @route('test', '/test/{dpid}',
40            methods=['GET'], requirements={'dpid': dpidlib.DPID_PATTERN})
41     def test_get_dpid(self, req, dpid, **_kwargs):
42         return Response(status=200, body=dpid)
43
44     @route('test', '/test')
45     def test_root(self, req, **_kwargs):
46         return Response(status=200, body='root')
47
48
49 class Test_wsgi(unittest.TestCase):
50
51     """ Test case for wsgi
52     """
53
54     def setUp(self):
55         controller_data = {
56             'test_param': 'foo'
57         }
58         self.wsgi_app = WSGIApplication()
59         self.wsgi_app.register(_TestController, controller_data)
60
61     def tearDown(self):
62         pass
63
64     def test_wsgi_decorator_ok(self):
65         r = self.wsgi_app({'REQUEST_METHOD': 'GET',
66                            'PATH_INFO': '/test/0123456789abcdef'},
67                           lambda s, _: eq_(s, '200 OK'))
68         eq_(r[0], (b'0123456789abcdef'))
69
70     def test_wsgi_decorator_ng_path(self):
71         self.wsgi_app({'REQUEST_METHOD': 'GET',
72                        'PATH_INFO': '/'},
73                       lambda s, _: eq_(s, '404 Not Found'))
74
75     def test_wsgi_decorator_ng_method(self):
76         # XXX: If response code is "405 Method Not Allowed", it is better.
77         self.wsgi_app({'REQUEST_METHOD': 'PUT',
78                        'PATH_INFO': '/test/0123456789abcdef'},
79                       lambda s, _: eq_(s, '404 Not Found'))
80
81     def test_wsgi_decorator_ng_requirements(self):
82         # XXX: If response code is "400 Bad Request", it is better.
83         self.wsgi_app({'REQUEST_METHOD': 'GET',
84                        'PATH_INFO': '/test/hogehoge'},
85                       lambda s, _: eq_(s, '404 Not Found'))
86
87     def test_wsgi_decorator_ok_any_method(self):
88         self.wsgi_app({'REQUEST_METHOD': 'GET',
89                        'PATH_INFO': '/test'},
90                       lambda s, _: eq_(s, '200 OK'))
91         self.wsgi_app({'REQUEST_METHOD': 'POST',
92                        'PATH_INFO': '/test'},
93                       lambda s, _: eq_(s, '200 OK'))
94         self.wsgi_app({'REQUEST_METHOD': 'PUT',
95                        'PATH_INFO': '/test'},
96                       lambda s, _: eq_(s, '200 OK'))
97         r = self.wsgi_app({'REQUEST_METHOD': 'DELETE',
98                            'PATH_INFO': '/test'},
99                           lambda s, _: eq_(s, '200 OK'))
100         eq_(r[0], b'root')
101
102
103 if __name__ == '__main__':
104     nose.main(argv=['nosetests', '-s', '-v'], defaultTest=__file__)