|
| 1 | +# Copyright (C) 2016 Red Hat, Inc |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +""" |
| 16 | +Test for commissaire_http.dispatcher |
| 17 | +""" |
| 18 | + |
| 19 | +import json |
| 20 | + |
| 21 | +from io import BytesIO |
| 22 | + |
| 23 | +from . import TestCase, mock |
| 24 | + |
| 25 | +from commissaire_http.dispatcher import Dispatcher |
| 26 | +from commissaire_http.router import Router |
| 27 | + |
| 28 | + |
| 29 | +class TestDispatcher(TestCase): |
| 30 | + """ |
| 31 | + Test for the Dispatcher class. |
| 32 | + """ |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + """ |
| 36 | + Creates a new instance to test with per test. |
| 37 | + """ |
| 38 | + self.router_instance = Router() |
| 39 | + self.router_instance.connect( |
| 40 | + '/path/', |
| 41 | + controller='commissaire_http.handlers.hello_world', |
| 42 | + conditions={'method': 'GET'}) |
| 43 | + self.router_instance.connect( |
| 44 | + '/world/', |
| 45 | + controller='commissaire_http.handlers.create_world', |
| 46 | + conditions={'method': 'PUT'}) |
| 47 | + self.dispatcher_instance = Dispatcher( |
| 48 | + self.router_instance, |
| 49 | + handler_packages=['commissaire_http.handlers'] |
| 50 | + ) |
| 51 | + |
| 52 | + def test_dispatcher_initialization(self): |
| 53 | + """ |
| 54 | + Verify the Dispatcher initializes properly. |
| 55 | + """ |
| 56 | + self.assertEquals( |
| 57 | + self.router_instance, self.dispatcher_instance._router) |
| 58 | + self.assertTrue(self.dispatcher_instance._handler_map) |
| 59 | + |
| 60 | + def test_dispatcher_reload_handlers(self): |
| 61 | + """ |
| 62 | + Verify the Dispatcher.reload_handlers actually loads handlers. |
| 63 | + """ |
| 64 | + self.dispatcher_instance._handler_map = {} |
| 65 | + self.assertFalse(self.dispatcher_instance._handler_map) |
| 66 | + self.dispatcher_instance.reload_handlers() |
| 67 | + self.assertTrue(self.dispatcher_instance._handler_map) |
| 68 | + |
| 69 | + def test_dispatcher_dispatch_with_valid_path(self): |
| 70 | + """ |
| 71 | + Verify the Dispatcher.dispatch works with valid paths. |
| 72 | + """ |
| 73 | + environ = { |
| 74 | + 'PATH_INFO': '/path/', |
| 75 | + 'REQUEST_METHOD': 'GET', |
| 76 | + } |
| 77 | + start_response = mock.MagicMock() |
| 78 | + result = self.dispatcher_instance.dispatch(environ, start_response) |
| 79 | + start_response.assert_called_once_with('200 OK', mock.ANY) |
| 80 | + self.assertEquals('{"Hello": "there"}', result[0].decode()) |
| 81 | + |
| 82 | + def test_dispatcher_dispatch_with_valid_path_and_params(self): |
| 83 | + """ |
| 84 | + Verify the Dispatcher.dispatch works with valid paths and params. |
| 85 | + """ |
| 86 | + environ = { |
| 87 | + 'PATH_INFO': '/path/', |
| 88 | + 'REQUEST_METHOD': 'GET', |
| 89 | + 'QUERY_STRING': 'name=bob' |
| 90 | + } |
| 91 | + start_response = mock.MagicMock() |
| 92 | + result = self.dispatcher_instance.dispatch(environ, start_response) |
| 93 | + start_response.assert_called_once_with('200 OK', mock.ANY) |
| 94 | + self.assertEquals('{"Hello": "bob"}', result[0].decode()) |
| 95 | + |
| 96 | + def test_dispatcher_dispatch_with_valid_path_with_wsgi_input(self): |
| 97 | + """ |
| 98 | + Verify the Dispatcher.dispatch works when wsgi_input is in use. |
| 99 | + """ |
| 100 | + environ = { |
| 101 | + 'PATH_INFO': '/world/', |
| 102 | + 'REQUEST_METHOD': 'PUT', # PUT uses wsgi.input |
| 103 | + 'wsgi.input': BytesIO(b'{"name": "world"}'), |
| 104 | + 'CONTENT_LENGTH': b'18', |
| 105 | + } |
| 106 | + start_response = mock.MagicMock() |
| 107 | + result = self.dispatcher_instance.dispatch(environ, start_response) |
| 108 | + start_response.assert_called_once_with('200 OK', mock.ANY) |
| 109 | + self.assertEquals('world', json.loads(result[0].decode())['name']) |
| 110 | + |
| 111 | + def test_dispatcher_dispatch_with_invalid_path(self): |
| 112 | + """ |
| 113 | + Verify the Dispatcher.dispatch works with invalid paths. |
| 114 | + """ |
| 115 | + environ = { |
| 116 | + 'PATH_INFO': '/idonotexist/', |
| 117 | + 'REQUEST_METHOD': 'GET', |
| 118 | + } |
| 119 | + start_response = mock.MagicMock() |
| 120 | + result = self.dispatcher_instance.dispatch(environ, start_response) |
| 121 | + start_response.assert_called_once_with('404 Not Found', mock.ANY) |
| 122 | + self.assertEquals('Not Found', result[0].decode()) |
0 commit comments