#!/usr/bin/env python
from unittest import TestCase
import mechanize
import mechanize._form
from mechanize._response import test_html_response
from mechanize._html import content_parser, get_title, Factory
class RegressionTests(TestCase):
def test_close_base_tag(self):
# any document containing a tag used to cause an exception
br = mechanize.Browser()
br.set_html("")
list(br.links())
def test_bad_base_tag(self):
# a document with a base tag with no href used to cause an exception
br = mechanize.Browser()
br.set_html(
"
Blah.
""") self.assertEqual(get_title(content_parser(html)), 'Title Test') class MiscTests(TestCase): def test_util_func(self): headers1 = str(test_html_response('').info()) headers2 = str(test_html_response('').info()) self.assertEqual(headers1, headers2) def test_link_parsing(self): def get_first_link_text(html): factory = Factory() response = test_html_response(html, url="http://example.com/") factory.set_response(response) return list(factory.links())[0].text html = ("""\
The quick\tbrown fox jumps over the lazy dog """) self.assertEqual( get_first_link_text(html), u'The quick brown fox jumps over the lazy dog') html = ("""\
""") self.assertEqual(get_first_link_text(html), '') html = ("""\
""") self.assertEqual(get_first_link_text(html), '') def test_title_parsing(self): def get_title(html): factory = Factory() response = test_html_response(html) factory.set_response(response) return factory.title html = (b"""\
Blah.
""") self.assertEqual(get_title(html), u'T>itle') html = ("""\
Blah.
""") self.assertEqual( str(get_title(html)), 'Ti tle &&') html = ("""\