Esempio n. 1
0
    def test_str(self):
        import mimetools
        from mechanize import _response

        br = TestBrowser()
        self.assertEqual(
            str(br),
            "<TestBrowser (not visiting a URL)>"
            )

        fp = StringIO.StringIO('<html><form name="f"><input /></form></html>')
        headers = mimetools.Message(
            StringIO.StringIO("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>=)>>
>""")
Esempio n. 2
0
 def open(self, *args, **kwargs):
     response = mechanize.Browser.open(self, *args, **kwargs)
     if not response or not response.get_data():
         return response
     html = response.get_data().replace("<![endif]-->", "<!--[endif]-->")
     patched_resp = closeable_response(StringIO(html), response._headers, response._url, response.code, response.msg)
     patched_resp = response_seek_wrapper(patched_resp)
     self.set_response(patched_resp)
     return patched_resp
Esempio n. 3
0
 def open(self, *args, **kwargs):
     response = mechanize.Browser.open(self, *args, **kwargs)
     if not response or not response.get_data():
         return response
     html = response.get_data().replace("<![endif]-->", "<!--[endif]-->")
     patched_resp = closeable_response(StringIO(html), response._headers,
                                       response._url, response.code,
                                       response.msg)
     patched_resp = response_seek_wrapper(patched_resp)
     self.set_response(patched_resp)
     return patched_resp
Esempio n. 4
0
    def test_str(self):
        from mechanize import _response

        br = TestBrowser()
        self.assertEqual(str(br), "<TestBrowser (not visiting a URL)>")

        fp = io.StringIO('<html><form name="f"><input /></form></html>')
        headers = email.message_from_string("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>=)>>
>""")
Esempio n. 5
0
 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')