[Python-checkins] r50807 - python/branches/hoxworth-stdlib_logging-soc/fakesocket.py python/branches/hoxworth-stdlib_logging-soc/new_soc_logging_test.py

jackilyn.hoxworth python-checkins at python.org
Mon Jul 24 20:15:55 CEST 2006


Author: jackilyn.hoxworth
Date: Mon Jul 24 20:15:55 2006
New Revision: 50807

Added:
   python/branches/hoxworth-stdlib_logging-soc/fakesocket.py
   python/branches/hoxworth-stdlib_logging-soc/new_soc_logging_test.py
Log:


Added: python/branches/hoxworth-stdlib_logging-soc/fakesocket.py
==============================================================================
--- (empty file)
+++ python/branches/hoxworth-stdlib_logging-soc/fakesocket.py	Mon Jul 24 20:15:55 2006
@@ -0,0 +1,33 @@
+import socket
+
+def getaddrinfo(host,port, *args):
+	"""no real addr info -- but the test data is copied from 'port' to 'sa'
+   for a bit of control over the resulting mock socket.
+	>>> getaddrinfo("MOCK", "Raise: connection prohibited")
+	("af", "socktype", "proto", "cannonname", "Raise: connection prohibited")
+   """
+
+	if host != "MOCK":
+		raise ValueError("Faked Socket Module for testing only")
+	# Use port for sa, so we can fake a raise
+	return ("af", "socktype", "proto", "cannonname", port)
+
+# Bad name, but it matches the real module
+class error(Exception): pass
+
+class socket(object):
+	"""Mock socket object"""
+	def __init__(self, af, socktype, proto): pass
+	def connect(self, sa):
+		"""Raise if the argument says raise, otherwise sa is treated as the response.
+		Wouldn't hurt to put doctests in here, too...
+		"""
+		if sa.startswith("Raise: "):
+			raise error(sa)
+		else:
+			self.incoming_msg = sa
+	def close(self): pass
+	def sendall(self, msg):
+		self.gotsent = msg
+	def makefile(self, mode, bufsize):
+		return cStringIO(self.incoming_msg)
\ No newline at end of file

Added: python/branches/hoxworth-stdlib_logging-soc/new_soc_logging_test.py
==============================================================================
--- (empty file)
+++ python/branches/hoxworth-stdlib_logging-soc/new_soc_logging_test.py	Mon Jul 24 20:15:55 2006
@@ -0,0 +1,40 @@
+import httplib
+import fakesocket
+import logging
+from cStringIO import StringIO
+
+origsocket=httplib.socket
+# monkeypatch -- replace httplib.socket with our own fake module
+httplib.socket=fakesocket
+
+# ... run the tests ...
+log=logging.getLogger("py.httplib")
+stringLog = StringIO()
+
+# define the handler and level
+handler = logging.StreamHandler(stringLog)
+log.setLevel(logging.INFO)
+
+# add the handler to the logger
+log.addHandler(handler)
+
+httplib._log.info("message 1") # 1st test
+
+myconn = httplib.HTTPConnection('www.google.com')
+myconn.set_debuglevel(43)
+if myconn.debuglevel > 0:
+	print "Debug level is > 0"
+
+myconn.connect()
+myconn.putrequest("GET", "/search?q=Jackilyn+Hoxworth")
+myconn.getresponse()
+
+print stringLog.getvalue()  # For testing purposes
+
+if stringLog.getvalue() != "Error:  It worked":
+    print "it worked"
+else:
+    print "it didn't work"
+
+# restore it to working order, so other tests won't fail
+httplib.socket=origsocket
\ No newline at end of file


More information about the Python-checkins mailing list