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