class Response(object): msg = create_response_info(BytesIO(b"")) status = 200 reason = "OK" fp = BytesIO(b'') def read(self__, sz=-1): return b"" def close(self): self.fp = None def readinto(self__, b): pass
def test_encoding(self): import mechanize # always take first encoding, since that's the one from the real HTTP # headers, rather than from HTTP-EQUIV b = mechanize.Browser() for s, ct in [ ("", mechanize._html.DEFAULT_ENCODING), ("Foo: Bar\r\n\r\n", mechanize._html.DEFAULT_ENCODING), ("Content-Type: text/html; charset=UTF-8\r\n\r\n", "UTF-8"), ("Content-Type: text/html; charset=UTF-8\r\n" "Content-Type: text/html; charset=KOI8-R\r\n\r\n", "UTF-8"), ]: if not isinstance(s, bytes): s = s.encode('ascii') msg = create_response_info(BytesIO(s)) r = addinfourl(BytesIO(b""), msg, "http://www.example.com/") b.set_response(r) self.assertEqual(b.encoding(), ct)
def test_str(self): from mechanize import _response br = TestBrowser() self.assertEqual(str(br), "<TestBrowser (not visiting a URL)>") fp = BytesIO(b'<html><form name="f"><input /></form></html>') headers = create_response_info(BytesIO(b"Content-type: text/html")) response = _response.response_seek_wrapper( _response.closeable_response(fp, headers, "http://example.com/", 200, "OK")) br.set_response(response) self.assertEqual(str(br), "<TestBrowser visiting http://example.com/>") br.select_form(nr=0) self.assertEqual( str(br), """\ <TestBrowser visiting http://example.com/ selected form: <f GET http://example.com/ application/x-www-form-urlencoded <TextControl(<None>=)>> >""")
def test_select_form(self): from mechanize import _response br = TestBrowser() fp = BytesIO(b'''<html> <form name="a"></form> <form name="b" data-ac="123"></form> <form name="c" class="x"></form> </html>''') headers = create_response_info(BytesIO(b"Content-type: text/html")) response = _response.response_seek_wrapper( _response.closeable_response(fp, headers, "http://example.com/", 200, "OK")) br.set_response(response) for i, n in enumerate('abc'): br.select_form(nr=i) self.assertEqual(br.form.name, n) br.select_form(nr=0), br.select_form(name=n) self.assertEqual(br.form.name, n) br.select_form(data_ac=re.compile(r'\d+')) self.assertEqual(br.form.name, 'b') br.select_form(class_=lambda x: x == 'x') self.assertEqual(br.form.name, 'c')