File: test_forms.doctest

package info (click to toggle)
python-mechanize 1%3A0.4.10%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,316 kB
  • sloc: python: 16,656; makefile: 11; sh: 4
file content (46 lines) | stat: -rw-r--r-- 1,472 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Integration regression test for case where ClientForm handled RFC 3986
url unparsing incorrectly (it was using "" in place of None for
fragment, due to continuing to support use of stdlib module urlparse
as well as mechanize._rfc3986).  Fixed in ClientForm r33622 .

>>> import mechanize
>>> from mechanize._response import test_response

>>> def forms():
...     forms = []
...     for method in ["GET", "POST"]:
...         data = ('<form action="" method="%s">'
...         '<input type="submit" name="s"/></form>' % method
...         )
...         br = mechanize.Browser()
...         br.set_html(data, "http://example.com/")
...         br.select_form(nr=0)
...         forms.append(br.form)
...     return forms

>>> getform, postform = forms()
>>> getform.click().get_full_url()
'http://example.com/?s='
>>> postform.click().get_full_url()
'http://example.com/'


If something (e.g. calling .forms() triggers parsing, and parsing
fails, the next attempt should not succeed!  This used to happen
because the response held by LinksFactory etc was stale, since it had
already been .read().  Fixed by calling Factory.set_response() on
error.

>>> import mechanize
>>> br = mechanize.Browser()
>>> br.set_html("""\
... <form>
... <input type="text" name="foo" value="a"></input><!!!>
... <input type="text" name="bar" value="b"></input>
... </form>
... """)
>>> try:
...     br.select_form(nr=0)
... except mechanize.ParseError:
...     pass
>>> br.select_form(nr=0)