[wwwsearch-commits] r21705 - in wwwsearch/ClientCookie/trunk:
ClientCookie test
jjlee at codespeak.net
jjlee at codespeak.net
Thu Jan 5 02:17:41 CET 2006
Author: jjlee
Date: Thu Jan 5 02:17:40 2006
New Revision: 21705
Modified:
wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py
wwwsearch/ClientCookie/trunk/test/test_misc.py
wwwsearch/ClientCookie/trunk/test/test_urllib2.py
Log:
Make copy.copy(response) copy headers as well as data
Modified: wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py
==============================================================================
--- wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py (original)
+++ wwwsearch/ClientCookie/trunk/ClientCookie/_Util.py Thu Jan 5 02:17:40 2006
@@ -581,12 +581,23 @@
"""
+ def __init__(self, wrapped):
+ seek_wrapper.__init__(self, wrapped)
+ self._headers = self.wrapped.info()
+
def close(self):
- self.headers = self.wrapped.headers
self.url = self.wrapped.url
self.wrapped.close()
self.wrapped = eoffile()
+ def info(self):
+ return self._headers
+
+ def __copy__(self):
+ cpy = seek_wrapper.__copy__(self)
+ cpy._headers = self.wrapped.info().copy()
+ return cpy
+
def __getstate__(self):
# There are three obvious options here:
# 1. truncate
Modified: wwwsearch/ClientCookie/trunk/test/test_misc.py
==============================================================================
--- wwwsearch/ClientCookie/trunk/test/test_misc.py (original)
+++ wwwsearch/ClientCookie/trunk/test/test_misc.py Thu Jan 5 02:17:40 2006
@@ -24,21 +24,31 @@
self.log.append(("readlines", sizehint))
return self._file.readlines(sizehint)
+class TestUnSeekableResponse(TestUnSeekable):
+ def __init__(self, text, headers):
+ TestUnSeekable.__init__(self, text)
+ self.headers = headers
+
+ def info(self):
+ return self.headers
+
+
class SeekableTests(TestCase):
- def testSeekable(self):
- import copy
- try:
- from exceptions import StopIteration
- except ImportError:
- from ClientCookie._ClientCookie import StopIteration
- from ClientCookie._Util import seek_wrapper
- text = """\
+
+ text = """\
The quick brown fox
jumps over the lazy
dog.
"""
+ def testSeekable(self):
+ try:
+ from exceptions import StopIteration
+ except ImportError:
+ from ClientCookie._ClientCookie import StopIteration
+ from ClientCookie._Util import seek_wrapper
+ text = self.text
text_lines = map(lambda l: l+"\n", string.split(text, "\n")[:-1])
fh = TestUnSeekable(text)
sfh = seek_wrapper(fh)
@@ -120,12 +130,30 @@
# copies have independent seek positions
fh = TestUnSeekable(text)
sfh = seek_wrapper(fh)
+ self._testCopy(sfh)
+
+ def _testCopy(self, sfh):
+ import copy
sfh2 = copy.copy(sfh)
sfh.read(10)
+ text = self.text
self.assertEqual(sfh2.read(10), text[:10])
sfh2.seek(5)
self.assertEqual(sfh.read(10), text[10:20])
self.assertEqual(sfh2.read(10), text[5:15])
+ sfh.seek(0)
+ sfh2.seek(0)
+ return sfh2
+
+ def testResponseSeekWrapper(self):
+ hdrs = {"Content-type": "text/html"}
+ from ClientCookie import response_seek_wrapper
+ r = TestUnSeekableResponse(self.text, hdrs)
+ rsw = response_seek_wrapper(r)
+ rsw2 = self._testCopy(rsw)
+ self.assert_(rsw is not rsw2)
+ self.assertEqual(rsw.info(), rsw2.info())
+ self.assert_(rsw.info() is not rsw2.info())
if __name__ == "__main__":
Modified: wwwsearch/ClientCookie/trunk/test/test_urllib2.py
==============================================================================
--- wwwsearch/ClientCookie/trunk/test/test_urllib2.py (original)
+++ wwwsearch/ClientCookie/trunk/test/test_urllib2.py Thu Jan 5 02:17:40 2006
@@ -664,7 +664,8 @@
o = h.parent = MockOpener()
req = urllib2.Request("http://example.com/")
- class MockUnseekableResponse: pass
+ class MockUnseekableResponse:
+ def info(self): pass
r = MockUnseekableResponse()
newr = h.http_response(req, r)
self.assert_(not hasattr(r, "seek"))
More information about the wwwsearch-commits
mailing list