4 # Copyright (C) 2007, 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for unittesting the http module"""
29 from ganeti import http
31 import ganeti.http.server
32 import ganeti.http.client
35 class TestStartLines(unittest.TestCase):
36 """Test cases for start line classes"""
38 def testClientToServerStartLine(self):
39 """Test client to server start line (HTTP request)"""
40 start_line = http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1")
41 self.assertEqual(str(start_line), "GET / HTTP/1.1")
43 def testServerToClientStartLine(self):
44 """Test server to client start line (HTTP response)"""
45 start_line = http.HttpServerToClientStartLine("HTTP/1.1", 200, "OK")
46 self.assertEqual(str(start_line), "HTTP/1.1 200 OK")
49 class TestMisc(unittest.TestCase):
50 """Miscellaneous tests"""
52 def _TestDateTimeHeader(self, gmnow, expected):
53 self.assertEqual(http.server._DateTimeHeader(gmnow=gmnow), expected)
55 def testDateTimeHeader(self):
56 """Test ganeti.http._DateTimeHeader"""
57 self._TestDateTimeHeader((2008, 1, 2, 3, 4, 5, 3, 0, 0),
58 "Thu, 02 Jan 2008 03:04:05 GMT")
59 self._TestDateTimeHeader((2008, 1, 1, 0, 0, 0, 0, 0, 0),
60 "Mon, 01 Jan 2008 00:00:00 GMT")
61 self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 0, 0, 0),
62 "Mon, 31 Dec 2008 00:00:00 GMT")
63 self._TestDateTimeHeader((2008, 12, 31, 23, 59, 59, 0, 0, 0),
64 "Mon, 31 Dec 2008 23:59:59 GMT")
65 self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 6, 0, 0),
66 "Sun, 31 Dec 2008 00:00:00 GMT")
68 def testHttpServerRequest(self):
69 """Test ganeti.http.server._HttpServerRequest"""
70 fake_request = http.HttpMessage()
71 fake_request.start_line = \
72 http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1")
73 server_request = http.server._HttpServerRequest(fake_request)
75 # These are expected by users of the HTTP server
76 self.assert_(hasattr(server_request, "request_method"))
77 self.assert_(hasattr(server_request, "request_path"))
78 self.assert_(hasattr(server_request, "request_headers"))
79 self.assert_(hasattr(server_request, "request_body"))
80 self.assert_(isinstance(server_request.resp_headers, dict))
81 self.assert_(hasattr(server_request, "private"))
83 def testServerSizeLimits(self):
84 """Test HTTP server size limits"""
85 message_reader_class = http.server._HttpClientToServerMessageReader
86 self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0)
87 self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0)
89 def testClientSizeLimits(self):
90 """Test HTTP client size limits"""
91 message_reader_class = http.client._HttpServerToClientMessageReader
92 self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0)
93 self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0)
96 if __name__ == '__main__':