Commit f30ca1e6 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Add simple unittests for ganeti.http

More complex unittests will need some refactoring in the HTTP code.

Reviewed-by: amishchenko
parent e38220e4
......@@ -59,11 +59,15 @@ DEFAULT_ERROR_MESSAGE = """\
"""
def _DateTimeHeader():
def _DateTimeHeader(gmnow=None):
"""Return the current date and time formatted for a message header.
The time MUST be in the GMT timezone.
"""
(year, month, day, hh, mm, ss, wd, _, _) = time.gmtime()
if gmnow is None:
gmnow = time.gmtime()
(year, month, day, hh, mm, ss, wd, _, _) = gmnow
return ("%s, %02d %3s %4d %02d:%02d:%02d GMT" %
(WEEKDAYNAME[wd], day, MONTHNAME[month], year, hh, mm, ss))
......
......@@ -24,13 +24,72 @@
import os
import unittest
import tempfile
import time
from ganeti import http
import ganeti.http.server
import ganeti.http.client
# TODO: Write unittests
class TestStartLines(unittest.TestCase):
"""Test cases for start line classes"""
def testClientToServerStartLine(self):
"""Test client to server start line (HTTP request)"""
start_line = http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1")
self.assertEqual(str(start_line), "GET / HTTP/1.1")
def testServerToClientStartLine(self):
"""Test server to client start line (HTTP response)"""
start_line = http.HttpServerToClientStartLine("HTTP/1.1", 200, "OK")
self.assertEqual(str(start_line), "HTTP/1.1 200 OK")
class TestMisc(unittest.TestCase):
"""Miscellaneous tests"""
def _TestDateTimeHeader(self, gmnow, expected):
self.assertEqual(http.server._DateTimeHeader(gmnow=gmnow), expected)
def testDateTimeHeader(self):
"""Test ganeti.http._DateTimeHeader"""
self._TestDateTimeHeader((2008, 1, 2, 3, 4, 5, 3, 0, 0),
"Thu, 02 Jan 2008 03:04:05 GMT")
self._TestDateTimeHeader((2008, 1, 1, 0, 0, 0, 0, 0, 0),
"Mon, 01 Jan 2008 00:00:00 GMT")
self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 0, 0, 0),
"Mon, 31 Dec 2008 00:00:00 GMT")
self._TestDateTimeHeader((2008, 12, 31, 23, 59, 59, 0, 0, 0),
"Mon, 31 Dec 2008 23:59:59 GMT")
self._TestDateTimeHeader((2008, 12, 31, 0, 0, 0, 6, 0, 0),
"Sun, 31 Dec 2008 00:00:00 GMT")
def testHttpServerRequest(self):
"""Test ganeti.http.server._HttpServerRequest"""
fake_request = http.HttpMessage()
fake_request.start_line = \
http.HttpClientToServerStartLine("GET", "/", "HTTP/1.1")
server_request = http.server._HttpServerRequest(fake_request)
# These are expected by users of the HTTP server
self.assert_(hasattr(server_request, "request_method"))
self.assert_(hasattr(server_request, "request_path"))
self.assert_(hasattr(server_request, "request_headers"))
self.assert_(hasattr(server_request, "request_body"))
self.assert_(isinstance(server_request.resp_headers, dict))
def testServerSizeLimits(self):
"""Test HTTP server size limits"""
message_reader_class = http.server._HttpClientToServerMessageReader
self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0)
self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0)
def testClientSizeLimits(self):
"""Test HTTP client size limits"""
message_reader_class = http.client._HttpServerToClientMessageReader
self.assert_(message_reader_class.START_LINE_LENGTH_MAX > 0)
self.assert_(message_reader_class.HEADER_LENGTH_MAX > 0)
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment