Added BaseHTTPRequestHandler related tests.
This commit is contained in:
parent
a02653b38a
commit
b643fc6d66
|
@ -11,6 +11,7 @@ import CGIHTTPServer
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import re
|
||||||
import base64
|
import base64
|
||||||
import shutil
|
import shutil
|
||||||
import urllib
|
import urllib
|
||||||
|
@ -18,6 +19,9 @@ import httplib
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
from test import test_support
|
from test import test_support
|
||||||
threading = test_support.import_module('threading')
|
threading = test_support.import_module('threading')
|
||||||
|
|
||||||
|
@ -27,6 +31,22 @@ class NoLogRequestHandler:
|
||||||
# don't write log messages to stderr
|
# don't write log messages to stderr
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class SocketlessRequestHandler(BaseHTTPRequestHandler):
|
||||||
|
def __init__(self):
|
||||||
|
self.get_called = False
|
||||||
|
self.protocol_version = "HTTP/1.1"
|
||||||
|
|
||||||
|
def do_GET(self):
|
||||||
|
self.get_called = True
|
||||||
|
self.send_response(200)
|
||||||
|
self.send_header('Content-Type', 'text/html')
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write('<html><body>Data</body></html>\r\n')
|
||||||
|
|
||||||
|
def log_message(self, format, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestServerThread(threading.Thread):
|
class TestServerThread(threading.Thread):
|
||||||
def __init__(self, test_object, request_handler):
|
def __init__(self, test_object, request_handler):
|
||||||
|
@ -67,6 +87,63 @@ class BaseTestCase(unittest.TestCase):
|
||||||
self.connection.request(method, uri, body, headers)
|
self.connection.request(method, uri, body, headers)
|
||||||
return self.connection.getresponse()
|
return self.connection.getresponse()
|
||||||
|
|
||||||
|
class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
|
||||||
|
"""Test the functionaility of the BaseHTTPServer focussing on
|
||||||
|
BaseHTTPRequestHandler.
|
||||||
|
"""
|
||||||
|
|
||||||
|
HTTPResponseMatch = re.compile('HTTP/1.[0-9]+ 200 OK')
|
||||||
|
|
||||||
|
def setUp (self):
|
||||||
|
self.handler = SocketlessRequestHandler()
|
||||||
|
|
||||||
|
def send_typical_request(self, message):
|
||||||
|
input = StringIO(message)
|
||||||
|
output = StringIO()
|
||||||
|
self.handler.rfile = input
|
||||||
|
self.handler.wfile = output
|
||||||
|
self.handler.handle_one_request()
|
||||||
|
output.seek(0)
|
||||||
|
return output.readlines()
|
||||||
|
|
||||||
|
def verify_get_called(self):
|
||||||
|
self.assertTrue(self.handler.get_called)
|
||||||
|
|
||||||
|
def verify_expected_headers(self, headers):
|
||||||
|
for fieldName in 'Server: ', 'Date: ', 'Content-Type: ':
|
||||||
|
self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1)
|
||||||
|
|
||||||
|
def verify_http_server_response(self, response):
|
||||||
|
match = self.HTTPResponseMatch.search(response)
|
||||||
|
self.assertTrue(match is not None)
|
||||||
|
|
||||||
|
def test_http_1_1(self):
|
||||||
|
result = self.send_typical_request('GET / HTTP/1.1\r\n\r\n')
|
||||||
|
self.verify_http_server_response(result[0])
|
||||||
|
self.verify_expected_headers(result[1:-1])
|
||||||
|
self.verify_get_called()
|
||||||
|
self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
|
||||||
|
|
||||||
|
def test_http_1_0(self):
|
||||||
|
result = self.send_typical_request('GET / HTTP/1.0\r\n\r\n')
|
||||||
|
self.verify_http_server_response(result[0])
|
||||||
|
self.verify_expected_headers(result[1:-1])
|
||||||
|
self.verify_get_called()
|
||||||
|
self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
|
||||||
|
|
||||||
|
def test_http_0_9(self):
|
||||||
|
result = self.send_typical_request('GET / HTTP/0.9\r\n\r\n')
|
||||||
|
self.assertEqual(len(result), 1)
|
||||||
|
self.assertEqual(result[0], '<html><body>Data</body></html>\r\n')
|
||||||
|
self.verify_get_called()
|
||||||
|
|
||||||
|
def test_with_continue_1_0(self):
|
||||||
|
result = self.send_typical_request('GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n')
|
||||||
|
self.verify_http_server_response(result[0])
|
||||||
|
self.verify_expected_headers(result[1:-1])
|
||||||
|
self.verify_get_called()
|
||||||
|
self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n')
|
||||||
|
|
||||||
|
|
||||||
class BaseHTTPServerTestCase(BaseTestCase):
|
class BaseHTTPServerTestCase(BaseTestCase):
|
||||||
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
|
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
|
||||||
|
@ -402,10 +479,11 @@ class CGIHTTPServerTestCase(BaseTestCase):
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
try:
|
try:
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
test_support.run_unittest(BaseHTTPServerTestCase,
|
test_support.run_unittest(BaseHTTPRequestHandlerTestCase,
|
||||||
SimpleHTTPServerTestCase,
|
BaseHTTPServerTestCase,
|
||||||
CGIHTTPServerTestCase
|
SimpleHTTPServerTestCase,
|
||||||
)
|
CGIHTTPServerTestCase
|
||||||
|
)
|
||||||
finally:
|
finally:
|
||||||
os.chdir(cwd)
|
os.chdir(cwd)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue