]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add unittests that verify documented behavior of public methods in Transport
authorJeremy Hylton <jeremy@alum.mit.edu>
Mon, 24 Nov 2008 22:00:29 +0000 (22:00 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Mon, 24 Nov 2008 22:00:29 +0000 (22:00 +0000)
class.

These methods can be overridden.  The tests verify that the overridden
methods are called, and that changes to the connection have a visible
effect on the request.

Lib/test/test_xmlrpc.py

index bbdb93eddd2ee66335cd01c7b447fa7e657cfcde..c9294b13605a8d8ae06f40faa4daf211236f1a62 100644 (file)
@@ -9,6 +9,7 @@ import threading
 import mimetools
 import httplib
 import socket
+import StringIO
 import os
 from test import test_support
 
@@ -639,9 +640,93 @@ class CGIHandlerTestCase(unittest.TestCase):
         os.remove("xmldata.txt")
         os.remove(test_support.TESTFN)
 
+class FakeSocket:
+
+    def __init__(self):
+        self.data = StringIO.StringIO()
+
+    def send(self, buf):
+        self.data.write(buf)
+        return len(buf)
+
+    def sendall(self, buf):
+        self.data.write(buf)
+
+    def getvalue(self):
+        return self.data.getvalue()
+
+    def makefile(self, x, y):
+        raise RuntimeError
+
+class FakeTransport(xmlrpclib.Transport):
+    """A Transport instance that records instead of sending a request.
+
+    This class replaces the actual socket used by httplib with a
+    FakeSocket object that records the request.  It doesn't provide a
+    response.
+    """
+
+    def make_connection(self, host):
+        conn = xmlrpclib.Transport.make_connection(self, host)
+        conn._conn.sock = self.fake_socket = FakeSocket()
+        return conn
+
+class TransportSubclassTestCase(unittest.TestCase):
+
+    def issue_request(self, transport_class):
+        """Return an HTTP request made via transport_class."""
+        transport = transport_class()
+        proxy = xmlrpclib.ServerProxy("http://example.com/",
+                                      transport=transport)
+        try:
+            proxy.pow(6, 8)
+        except RuntimeError:
+            return transport.fake_socket.getvalue()
+        return None
+
+    def test_custom_user_agent(self):
+        class TestTransport(FakeTransport):
+
+            def send_user_agent(self, conn):
+                xmlrpclib.Transport.send_user_agent(self, conn)
+                conn.putheader("X-Test", "test_custom_user_agent")
+
+        req = self.issue_request(TestTransport)
+        self.assert_("X-Test: test_custom_user_agent\r\n" in req)
+
+    def test_send_host(self):
+        class TestTransport(FakeTransport):
+
+            def send_host(self, conn, host):
+                xmlrpclib.Transport.send_host(self, conn, host)
+                conn.putheader("X-Test", "test_send_host")
+
+        req = self.issue_request(TestTransport)
+        self.assert_("X-Test: test_send_host\r\n" in req)
+
+    def test_send_request(self):
+        class TestTransport(FakeTransport):
+
+            def send_request(self, conn, url, body):
+                xmlrpclib.Transport.send_request(self, conn, url, body)
+                conn.putheader("X-Test", "test_send_request")
+
+        req = self.issue_request(TestTransport)
+        self.assert_("X-Test: test_send_request\r\n" in req)
+
+    def test_send_content(self):
+        class TestTransport(FakeTransport):
+
+            def send_content(self, conn, body):
+                conn.putheader("X-Test", "test_send_content")
+                xmlrpclib.Transport.send_content(self, conn, body)
+
+        req = self.issue_request(TestTransport)
+        self.assert_("X-Test: test_send_content\r\n" in req)
+
 def test_main():
     xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase,
-         BinaryTestCase, FaultTestCase]
+         BinaryTestCase, FaultTestCase, TransportSubclassTestCase]
 
     # The test cases against a SimpleXMLRPCServer raise a socket error
     # 10035 (WSAEWOULDBLOCK) in the server thread handle_request call when