1 # Copyright (C) 2013 Stratosphere Inc.
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
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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
22 from nose.tools import eq_
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
30 LOG = logging.getLogger('test_wsgi')
33 class _TestController(ControllerBase):
35 def __init__(self, req, link, data, **config):
36 super(_TestController, self).__init__(req, link, data, **config)
37 eq_(data['test_param'], 'foo')
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)
44 @route('test', '/test')
45 def test_root(self, req, **_kwargs):
46 return Response(status=200, body='root')
49 class Test_wsgi(unittest.TestCase):
51 """ Test case for wsgi
58 self.wsgi_app = WSGIApplication()
59 self.wsgi_app.register(_TestController, controller_data)
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'))
70 def test_wsgi_decorator_ng_path(self):
71 self.wsgi_app({'REQUEST_METHOD': 'GET',
73 lambda s, _: eq_(s, '404 Not Found'))
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'))
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'))
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'))
103 if __name__ == '__main__':
104 nose.main(argv=['nosetests', '-s', '-v'], defaultTest=__file__)