[wwwsearch-commits] r32768 - wwwsearch/mechanize/trunk/test
jjlee at codespeak.net
jjlee at codespeak.net
Sat Sep 30 20:43:27 CEST 2006
Author: jjlee
Date: Sat Sep 30 20:43:24 2006
New Revision: 32768
Added:
wwwsearch/mechanize/trunk/test/test_html.py
wwwsearch/mechanize/trunk/test/test_opener.py
wwwsearch/mechanize/trunk/test/test_useragent.py
Modified:
wwwsearch/mechanize/trunk/test/test_mechanize.py
wwwsearch/mechanize/trunk/test/test_misc.py
wwwsearch/mechanize/trunk/test/test_urllib2.py
Log:
Split up test_mechanize.py and update some comments / docstrings
Added: wwwsearch/mechanize/trunk/test/test_html.py
==============================================================================
--- (empty file)
+++ wwwsearch/mechanize/trunk/test/test_html.py Sat Sep 30 20:43:24 2006
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+
+from unittest import TestCase
+
+import mechanize
+
+
+class RegressionTests(TestCase):
+
+ def test_close_base_tag(self):
+ # any document containing a </base> tag used to cause an exception
+ br = mechanize.Browser()
+ response = mechanize.make_response(
+ "</base>", [("Content-type", "text/html")], "", 200, "OK")
+ br.set_response(response)
+ list(br.links())
+
+
+class CachingGeneratorFunctionTests(TestCase):
+
+ def _get_simple_cgenf(self, log):
+ from mechanize._html import CachingGeneratorFunction
+ todo = []
+ for ii in range(2):
+ def work(ii=ii):
+ log.append(ii)
+ return ii
+ todo.append(work)
+ def genf():
+ for a in todo:
+ yield a()
+ return CachingGeneratorFunction(genf())
+
+ def test_cache(self):
+ log = []
+ cgenf = self._get_simple_cgenf(log)
+ for repeat in range(2):
+ for ii, jj in zip(cgenf(), range(2)):
+ self.assertEqual(ii, jj)
+ self.assertEqual(log, range(2)) # work only done once
+
+ def test_interleaved(self):
+ log = []
+ cgenf = self._get_simple_cgenf(log)
+ cgen = cgenf()
+ self.assertEqual(cgen.next(), 0)
+ self.assertEqual(log, [0])
+ cgen2 = cgenf()
+ self.assertEqual(cgen2.next(), 0)
+ self.assertEqual(log, [0])
+ self.assertEqual(cgen.next(), 1)
+ self.assertEqual(log, [0, 1])
+ self.assertEqual(cgen2.next(), 1)
+ self.assertEqual(log, [0, 1])
+ self.assertRaises(StopIteration, cgen.next)
+ self.assertRaises(StopIteration, cgen2.next)
+
+
+class UnescapeTests(TestCase):
+
+ def test_unescape_charref(self):
+ from mechanize._html import unescape_charref
+ mdash_utf8 = u"\u2014".encode("utf-8")
+ for ref, codepoint, utf8, latin1 in [
+ ("38", 38, u"&".encode("utf-8"), "&"),
+ ("x2014", 0x2014, mdash_utf8, "—"),
+ ("8212", 8212, mdash_utf8, "—"),
+ ]:
+ self.assertEqual(unescape_charref(ref, None), unichr(codepoint))
+ self.assertEqual(unescape_charref(ref, 'latin-1'), latin1)
+ self.assertEqual(unescape_charref(ref, 'utf-8'), utf8)
+
+ def test_unescape(self):
+ import htmlentitydefs
+ from mechanize._html import unescape
+ data = "& < — — —"
+ mdash_utf8 = u"\u2014".encode("utf-8")
+ ue = unescape(data, htmlentitydefs.name2codepoint, "utf-8")
+ self.assertEqual("& < %s %s %s" % ((mdash_utf8,)*3), ue)
+
+ for text, expect in [
+ ("&a&", "&a&"),
+ ("a&", "a&"),
+ ]:
+ got = unescape(text, htmlentitydefs.name2codepoint, "latin-1")
+ self.assertEqual(got, expect)
+
+
+if __name__ == "__main__":
+ import unittest
+ unittest.main()
Modified: wwwsearch/mechanize/trunk/test/test_mechanize.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_mechanize.py (original)
+++ wwwsearch/mechanize/trunk/test/test_mechanize.py Sat Sep 30 20:43:24 2006
@@ -1,8 +1,9 @@
#!/usr/bin/env python
+"""Tests for mechanize.Browser."""
-import sys, os, random, math
+import sys, os, random
from unittest import TestCase
-import StringIO, re, UserDict, urllib2
+import StringIO, re, urllib2
import mechanize
FACTORY_CLASSES = [mechanize.DefaultFactory]
@@ -15,100 +16,8 @@
FACTORY_CLASSES.append(mechanize.RobustFactory)
-def killfile(filename):
- try:
- os.remove(filename)
- except OSError:
- if os.name=='nt':
- try:
- os.chmod(filename, stat.S_IWRITE)
- os.remove(filename)
- except OSError:
- pass
-
-
-class RegressionTests(TestCase):
-
- def test_close_base_tag(self):
- # any document containing a </base> tag used to cause an exception
- br = mechanize.Browser()
- response = mechanize.make_response(
- "</base>", [("Content-type", "text/html")], "", 200, "OK")
- br.set_response(response)
- list(br.links())
-
-
-class CachingGeneratorFunctionTests(TestCase):
-
- def _get_simple_cgenf(self, log):
- from mechanize._html import CachingGeneratorFunction
- todo = []
- for ii in range(2):
- def work(ii=ii):
- log.append(ii)
- return ii
- todo.append(work)
- def genf():
- for a in todo:
- yield a()
- return CachingGeneratorFunction(genf())
-
- def test_cache(self):
- log = []
- cgenf = self._get_simple_cgenf(log)
- for repeat in range(2):
- for ii, jj in zip(cgenf(), range(2)):
- self.assertEqual(ii, jj)
- self.assertEqual(log, range(2)) # work only done once
-
- def test_interleaved(self):
- log = []
- cgenf = self._get_simple_cgenf(log)
- cgen = cgenf()
- self.assertEqual(cgen.next(), 0)
- self.assertEqual(log, [0])
- cgen2 = cgenf()
- self.assertEqual(cgen2.next(), 0)
- self.assertEqual(log, [0])
- self.assertEqual(cgen.next(), 1)
- self.assertEqual(log, [0, 1])
- self.assertEqual(cgen2.next(), 1)
- self.assertEqual(log, [0, 1])
- self.assertRaises(StopIteration, cgen.next)
- self.assertRaises(StopIteration, cgen2.next)
-
-
-class UnescapeTests(TestCase):
-
- def test_unescape_charref(self):
- from mechanize._html import unescape_charref
- mdash_utf8 = u"\u2014".encode("utf-8")
- for ref, codepoint, utf8, latin1 in [
- ("38", 38, u"&".encode("utf-8"), "&"),
- ("x2014", 0x2014, mdash_utf8, "—"),
- ("8212", 8212, mdash_utf8, "—"),
- ]:
- self.assertEqual(unescape_charref(ref, None), unichr(codepoint))
- self.assertEqual(unescape_charref(ref, 'latin-1'), latin1)
- self.assertEqual(unescape_charref(ref, 'utf-8'), utf8)
-
- def test_unescape(self):
- import htmlentitydefs
- from mechanize._html import unescape
- data = "& < — — —"
- mdash_utf8 = u"\u2014".encode("utf-8")
- ue = unescape(data, htmlentitydefs.name2codepoint, "utf-8")
- self.assertEqual("& < %s %s %s" % ((mdash_utf8,)*3), ue)
-
- for text, expect in [
- ("&a&", "&a&"),
- ("a&", "a&"),
- ]:
- got = unescape(text, htmlentitydefs.name2codepoint, "latin-1")
- self.assertEqual(got, expect)
-
-
# XXX these 'mock' classes are badly in need of simplification / removal
+# (note this stuff is also used by test_useragent.py)
class MockMethod:
def __init__(self, meth_name, action, handle):
self.meth_name = meth_name
@@ -197,166 +106,8 @@
default_schemes = []
-class OpenerTests(TestCase):
-
- def test_retrieve(self):
- # The .retrieve() method deals with a number of different cases. In
- # each case, .read() should be called the expected number of times, the
- # progress callback should be called as expected, and we should end up
- # with a filename and some headers.
-
- class Opener(mechanize.OpenerDirector):
- def __init__(self, content_length=None):
- mechanize.OpenerDirector.__init__(self)
- self.calls = []
- self.block_size = mechanize.OpenerDirector.BLOCK_SIZE
- self.nr_blocks = 2.5
- self.data = int((self.block_size/8)*self.nr_blocks)*"01234567"
- self.total_size = len(self.data)
- self._content_length = content_length
- def open(self, fullurl, data=None):
- from mechanize import _response
- self.calls.append((fullurl, data))
- headers = [("Foo", "Bar")]
- if self._content_length is not None:
- if self._content_length is True:
- content_length = str(len(self.data))
- else:
- content_length = str(self._content_length)
- headers.append(("content-length", content_length))
- return _response.test_response(self.data, headers)
-
- class CallbackVerifier:
- def __init__(self, testcase, total_size, block_size):
- self.count = 0
- self._testcase = testcase
- self._total_size = total_size
- self._block_size = block_size
- def callback(self, block_nr, block_size, total_size):
- self._testcase.assertEqual(block_nr, self.count)
- self._testcase.assertEqual(block_size, self._block_size)
- self._testcase.assertEqual(total_size, self._total_size)
- self.count += 1
-
- # ensure we start without the test file present
- tfn = "mechanize_test_73940ukewrl.txt"
- killfile(tfn)
-
- # case 1: filename supplied
- op = Opener()
- verif = CallbackVerifier(self, -1, op.block_size)
- url = "http://example.com/"
- try:
- filename, headers = op.retrieve(
- url, tfn, reporthook=verif.callback)
- self.assertEqual(filename, tfn)
- self.assertEqual(headers["foo"], 'Bar')
- self.assertEqual(open(filename, "rb").read(), op.data)
- self.assertEqual(len(op.calls), 1)
- self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
- op.close()
- # .close()ing the opener does NOT remove non-temporary files
- self.assert_(os.path.isfile(filename))
- finally:
- killfile(filename)
-
- # case 2: no filename supplied, use a temporary file
- op = Opener(content_length=True)
- # We asked the Opener to add a content-length header to the response
- # this time. Verify the total size passed to the callback is that case
- # is according to the content-length (rather than -1).
- verif = CallbackVerifier(self, op.total_size, op.block_size)
- url = "http://example.com/"
- filename, headers = op.retrieve(url, reporthook=verif.callback)
- self.assertNotEqual(filename, tfn) # (some temp filename instead)
- self.assertEqual(headers["foo"], 'Bar')
- self.assertEqual(open(filename, "rb").read(), op.data)
- self.assertEqual(len(op.calls), 1)
- # .close()ing the opener removes temporary files
- self.assert_(os.path.exists(filename))
- op.close()
- self.failIf(os.path.exists(filename))
- self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
-
- # case 3: "file:" URL with no filename supplied
- # we DON'T create a temporary file, since there's a file there already
- op = Opener()
- verif = CallbackVerifier(self, -1, op.block_size)
- tifn = "input_for_"+tfn
- try:
- f = open(tifn, 'wb')
- try:
- f.write(op.data)
- finally:
- f.close()
- url = "file://" + tifn
- filename, headers = op.retrieve(url, reporthook=verif.callback)
- self.assertEqual(filename, None) # this may change
- self.assertEqual(headers["foo"], 'Bar')
- self.assertEqual(open(tifn, "rb").read(), op.data)
- # no .read()s took place, since we already have the disk file,
- # and we weren't asked to write it to another filename
- self.assertEqual(verif.count, 0)
- op.close()
- # .close()ing the opener does NOT remove the file!
- self.assert_(os.path.isfile(tifn))
- finally:
- killfile(tifn)
-
- # case 4: "file:" URL and filename supplied
- # we DO create a new file in this case
- op = Opener()
- verif = CallbackVerifier(self, -1, op.block_size)
- tifn = "input_for_"+tfn
- try:
- f = open(tifn, 'wb')
- try:
- f.write(op.data)
- finally:
- f.close()
- url = "file://" + tifn
- try:
- filename, headers = op.retrieve(
- url, tfn, reporthook=verif.callback)
- self.assertEqual(filename, tfn)
- self.assertEqual(headers["foo"], 'Bar')
- self.assertEqual(open(tifn, "rb").read(), op.data)
- self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
- op.close()
- # .close()ing the opener does NOT remove non-temporary files
- self.assert_(os.path.isfile(tfn))
- finally:
- killfile(tfn)
- finally:
- killfile(tifn)
-
- # Content-Length mismatch with real file length gives URLError
- big = 1024*32
- op = Opener(content_length=big)
- verif = CallbackVerifier(self, big, op.block_size)
- url = "http://example.com/"
- try:
- try:
- op.retrieve(url, reporthook=verif.callback)
- except mechanize.ContentTooShortError, exc:
- filename, headers = exc.result
- self.assertNotEqual(filename, tfn)
- self.assertEqual(headers["foo"], 'Bar')
- # We still read and wrote to disk everything available, despite
- # the exception.
- self.assertEqual(open(filename, "rb").read(), op.data)
- self.assertEqual(len(op.calls), 1)
- self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
- # cleanup should still take place
- self.assert_(os.path.isfile(filename))
- op.close()
- self.failIf(os.path.isfile(filename))
- else:
- self.fail()
- finally:
- killfile(filename)
-
class BrowserTests(TestCase):
+
def test_referer(self):
b = TestBrowser()
url = "http://www.example.com/"
@@ -472,8 +223,9 @@
error = urllib2.HTTPError("http://example.com/bad", 503, "Oops",
MockHeaders(), StringIO.StringIO())
b.add_handler(make_mock_handler()([("https_open", error)]))
- self.assertRaises(urllib2.HTTPError, b.open, "https://example.com/")
+ self.assertRaises(urllib2.HTTPError, b.open, "https://example.com/badreq")
self.assertEqual(b.response().geturl(), error.geturl())
+ self.assertEqual(b.request.get_full_url(), "https://example.com/badreq")
self.assert_(same_response(b.back(), r8))
b.close()
@@ -898,6 +650,7 @@
class ResponseTests(TestCase):
+
def test_set_response(self):
import copy
from mechanize import response_seek_wrapper
@@ -962,51 +715,6 @@
>""")
-class UserAgentTests(TestCase):
- def test_set_handled_schemes(self):
- import mechanize
- class MockHandlerClass(make_mock_handler()):
- def __call__(self): return self
- class BlahHandlerClass(MockHandlerClass): pass
- class BlahProcessorClass(MockHandlerClass): pass
- BlahHandler = BlahHandlerClass([("blah_open", None)])
- BlahProcessor = BlahProcessorClass([("blah_request", None)])
- class TestUserAgent(mechanize.UserAgent):
- default_others = []
- default_features = []
- handler_classes = mechanize.UserAgent.handler_classes.copy()
- handler_classes.update(
- {"blah": BlahHandler, "_blah": BlahProcessor})
- ua = TestUserAgent()
-
- self.assertEqual(len(ua.handlers), 5)
- ua.set_handled_schemes(["http", "https"])
- self.assertEqual(len(ua.handlers), 2)
- self.assertRaises(ValueError,
- ua.set_handled_schemes, ["blah", "non-existent"])
- self.assertRaises(ValueError,
- ua.set_handled_schemes, ["blah", "_blah"])
- ua.set_handled_schemes(["blah"])
-
- req = mechanize.Request("blah://example.com/")
- r = ua.open(req)
- exp_calls = [("blah_open", (req,), {})]
- assert len(ua.calls) == len(exp_calls)
- for got, expect in zip(ua.calls, exp_calls):
- self.assertEqual(expect, got[1:])
-
- ua.calls = []
- req = mechanize.Request("blah://example.com/")
- ua._set_handler("_blah", True)
- r = ua.open(req)
- exp_calls = [
- ("blah_request", (req,), {}),
- ("blah_open", (req,), {})]
- assert len(ua.calls) == len(exp_calls)
- for got, expect in zip(ua.calls, exp_calls):
- self.assertEqual(expect, got[1:])
- ua._set_handler("_blah", True)
-
if __name__ == "__main__":
import unittest
unittest.main()
Modified: wwwsearch/mechanize/trunk/test/test_misc.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_misc.py (original)
+++ wwwsearch/mechanize/trunk/test/test_misc.py Sat Sep 30 20:43:24 2006
@@ -1,4 +1,4 @@
-"""Miscellaneous pyunit tests."""
+"""Tests for mechanize._response.seek_wrapper and friends."""
import copy
import cStringIO
Added: wwwsearch/mechanize/trunk/test/test_opener.py
==============================================================================
--- (empty file)
+++ wwwsearch/mechanize/trunk/test/test_opener.py Sat Sep 30 20:43:24 2006
@@ -0,0 +1,178 @@
+#!/usr/bin/env python
+
+import os, math
+from unittest import TestCase
+
+import mechanize
+
+
+def killfile(filename):
+ try:
+ os.remove(filename)
+ except OSError:
+ if os.name=='nt':
+ try:
+ os.chmod(filename, stat.S_IWRITE)
+ os.remove(filename)
+ except OSError:
+ pass
+
+class OpenerTests(TestCase):
+
+ def test_retrieve(self):
+ # The .retrieve() method deals with a number of different cases. In
+ # each case, .read() should be called the expected number of times, the
+ # progress callback should be called as expected, and we should end up
+ # with a filename and some headers.
+
+ class Opener(mechanize.OpenerDirector):
+ def __init__(self, content_length=None):
+ mechanize.OpenerDirector.__init__(self)
+ self.calls = []
+ self.block_size = mechanize.OpenerDirector.BLOCK_SIZE
+ self.nr_blocks = 2.5
+ self.data = int((self.block_size/8)*self.nr_blocks)*"01234567"
+ self.total_size = len(self.data)
+ self._content_length = content_length
+ def open(self, fullurl, data=None):
+ from mechanize import _response
+ self.calls.append((fullurl, data))
+ headers = [("Foo", "Bar")]
+ if self._content_length is not None:
+ if self._content_length is True:
+ content_length = str(len(self.data))
+ else:
+ content_length = str(self._content_length)
+ headers.append(("content-length", content_length))
+ return _response.test_response(self.data, headers)
+
+ class CallbackVerifier:
+ def __init__(self, testcase, total_size, block_size):
+ self.count = 0
+ self._testcase = testcase
+ self._total_size = total_size
+ self._block_size = block_size
+ def callback(self, block_nr, block_size, total_size):
+ self._testcase.assertEqual(block_nr, self.count)
+ self._testcase.assertEqual(block_size, self._block_size)
+ self._testcase.assertEqual(total_size, self._total_size)
+ self.count += 1
+
+ # ensure we start without the test file present
+ tfn = "mechanize_test_73940ukewrl.txt"
+ killfile(tfn)
+
+ # case 1: filename supplied
+ op = Opener()
+ verif = CallbackVerifier(self, -1, op.block_size)
+ url = "http://example.com/"
+ try:
+ filename, headers = op.retrieve(
+ url, tfn, reporthook=verif.callback)
+ self.assertEqual(filename, tfn)
+ self.assertEqual(headers["foo"], 'Bar')
+ self.assertEqual(open(filename, "rb").read(), op.data)
+ self.assertEqual(len(op.calls), 1)
+ self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
+ op.close()
+ # .close()ing the opener does NOT remove non-temporary files
+ self.assert_(os.path.isfile(filename))
+ finally:
+ killfile(filename)
+
+ # case 2: no filename supplied, use a temporary file
+ op = Opener(content_length=True)
+ # We asked the Opener to add a content-length header to the response
+ # this time. Verify the total size passed to the callback is that case
+ # is according to the content-length (rather than -1).
+ verif = CallbackVerifier(self, op.total_size, op.block_size)
+ url = "http://example.com/"
+ filename, headers = op.retrieve(url, reporthook=verif.callback)
+ self.assertNotEqual(filename, tfn) # (some temp filename instead)
+ self.assertEqual(headers["foo"], 'Bar')
+ self.assertEqual(open(filename, "rb").read(), op.data)
+ self.assertEqual(len(op.calls), 1)
+ # .close()ing the opener removes temporary files
+ self.assert_(os.path.exists(filename))
+ op.close()
+ self.failIf(os.path.exists(filename))
+ self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
+
+ # case 3: "file:" URL with no filename supplied
+ # we DON'T create a temporary file, since there's a file there already
+ op = Opener()
+ verif = CallbackVerifier(self, -1, op.block_size)
+ tifn = "input_for_"+tfn
+ try:
+ f = open(tifn, 'wb')
+ try:
+ f.write(op.data)
+ finally:
+ f.close()
+ url = "file://" + tifn
+ filename, headers = op.retrieve(url, reporthook=verif.callback)
+ self.assertEqual(filename, None) # this may change
+ self.assertEqual(headers["foo"], 'Bar')
+ self.assertEqual(open(tifn, "rb").read(), op.data)
+ # no .read()s took place, since we already have the disk file,
+ # and we weren't asked to write it to another filename
+ self.assertEqual(verif.count, 0)
+ op.close()
+ # .close()ing the opener does NOT remove the file!
+ self.assert_(os.path.isfile(tifn))
+ finally:
+ killfile(tifn)
+
+ # case 4: "file:" URL and filename supplied
+ # we DO create a new file in this case
+ op = Opener()
+ verif = CallbackVerifier(self, -1, op.block_size)
+ tifn = "input_for_"+tfn
+ try:
+ f = open(tifn, 'wb')
+ try:
+ f.write(op.data)
+ finally:
+ f.close()
+ url = "file://" + tifn
+ try:
+ filename, headers = op.retrieve(
+ url, tfn, reporthook=verif.callback)
+ self.assertEqual(filename, tfn)
+ self.assertEqual(headers["foo"], 'Bar')
+ self.assertEqual(open(tifn, "rb").read(), op.data)
+ self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
+ op.close()
+ # .close()ing the opener does NOT remove non-temporary files
+ self.assert_(os.path.isfile(tfn))
+ finally:
+ killfile(tfn)
+ finally:
+ killfile(tifn)
+
+ # Content-Length mismatch with real file length gives URLError
+ big = 1024*32
+ op = Opener(content_length=big)
+ verif = CallbackVerifier(self, big, op.block_size)
+ url = "http://example.com/"
+ try:
+ try:
+ op.retrieve(url, reporthook=verif.callback)
+ except mechanize.ContentTooShortError, exc:
+ filename, headers = exc.result
+ self.assertNotEqual(filename, tfn)
+ self.assertEqual(headers["foo"], 'Bar')
+ # We still read and wrote to disk everything available, despite
+ # the exception.
+ self.assertEqual(open(filename, "rb").read(), op.data)
+ self.assertEqual(len(op.calls), 1)
+ self.assertEqual(verif.count, math.ceil(op.nr_blocks) + 1)
+ # cleanup should still take place
+ self.assert_(os.path.isfile(filename))
+ op.close()
+ self.failIf(os.path.isfile(filename))
+ else:
+ self.fail()
+ finally:
+ killfile(filename)
+
Modified: wwwsearch/mechanize/trunk/test/test_urllib2.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_urllib2.py (original)
+++ wwwsearch/mechanize/trunk/test/test_urllib2.py Sat Sep 30 20:43:24 2006
@@ -1,12 +1,20 @@
-"""Tests for ClientCookie._urllib2_support (and for urllib2)."""
+"""Tests for urllib2-level functionality.
+
+This is made up of:
+
+ - tests that I've contributed back to stdlib test_urllib2.py
+
+ - tests for features that aren't in urllib2, but works on the level of the
+ interfaces exported by urllib2, especially urllib2 "handler" interface,
+ but *excluding* the extended interfaces provided by mechanize.UserAgent
+ and mechanize.Browser.
+
+"""
# XXX
# Request (I'm too lazy)
# CacheFTPHandler (hard to write)
-# parse_keqv_list, parse_http_list (I'm leaving this for Anthony Baxter
-# and Greg Stein, since they're doing Digest Authentication)
-# Authentication stuff (ditto)
-# ProxyHandler, CustomProxy, CustomProxyHandler (I don't use a proxy)
+# parse_keqv_list, parse_http_list
# GopherHandler (haven't used gopher for a decade or so...)
import unittest, StringIO, os, sys, UserDict, httplib
Added: wwwsearch/mechanize/trunk/test/test_useragent.py
==============================================================================
--- (empty file)
+++ wwwsearch/mechanize/trunk/test/test_useragent.py Sat Sep 30 20:43:24 2006
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+
+from unittest import TestCase
+
+import mechanize
+
+from test_mechanize import make_mock_handler
+
+
+class UserAgentTests(TestCase):
+ def test_set_handled_schemes(self):
+ import mechanize
+ class MockHandlerClass(make_mock_handler()):
+ def __call__(self): return self
+ class BlahHandlerClass(MockHandlerClass): pass
+ class BlahProcessorClass(MockHandlerClass): pass
+ BlahHandler = BlahHandlerClass([("blah_open", None)])
+ BlahProcessor = BlahProcessorClass([("blah_request", None)])
+ class TestUserAgent(mechanize.UserAgent):
+ default_others = []
+ default_features = []
+ handler_classes = mechanize.UserAgent.handler_classes.copy()
+ handler_classes.update(
+ {"blah": BlahHandler, "_blah": BlahProcessor})
+ ua = TestUserAgent()
+
+ self.assertEqual(len(ua.handlers), 5)
+ ua.set_handled_schemes(["http", "https"])
+ self.assertEqual(len(ua.handlers), 2)
+ self.assertRaises(ValueError,
+ ua.set_handled_schemes, ["blah", "non-existent"])
+ self.assertRaises(ValueError,
+ ua.set_handled_schemes, ["blah", "_blah"])
+ ua.set_handled_schemes(["blah"])
+
+ req = mechanize.Request("blah://example.com/")
+ r = ua.open(req)
+ exp_calls = [("blah_open", (req,), {})]
+ assert len(ua.calls) == len(exp_calls)
+ for got, expect in zip(ua.calls, exp_calls):
+ self.assertEqual(expect, got[1:])
+
+ ua.calls = []
+ req = mechanize.Request("blah://example.com/")
+ ua._set_handler("_blah", True)
+ r = ua.open(req)
+ exp_calls = [
+ ("blah_request", (req,), {}),
+ ("blah_open", (req,), {})]
+ assert len(ua.calls) == len(exp_calls)
+ for got, expect in zip(ua.calls, exp_calls):
+ self.assertEqual(expect, got[1:])
+ ua._set_handler("_blah", True)
+
+
+if __name__ == "__main__":
+ import unittest
+ unittest.main()
More information about the wwwsearch-commits
mailing list