Debian Codegraph @

test/test_request.doctest

raw
1 >>> from mechanize import Request
2 >>> Request("http://example.com/foo#frag").get_selector()
3 '/foo'
4
5 >>> Request("http://example.com?query").get_selector()
6 '/?query'
7
8 >>> Request("http://example.com").get_selector()
9 '/'
10
11
12 Request Headers Dictionary
13 --------------------------
14
15 The Request.headers dictionary is not a documented interface. It should
16 stay that way, because the complete set of headers are only accessible
17 through the .get_header(), .has_header(), .header_items() interface.
18 However, .headers pre-dates those methods, and so real code will be using
19 the dictionary.
20
21 The introduction in 2.4 of those methods was a mistake for the same reason:
22 code that previously saw all (urllib2 user)-provided headers in .headers
23 now sees only a subset (and the function interface is ugly and incomplete).
24 A better change would have been to replace .headers dict with a dict
25 subclass (or UserDict.DictMixin instance?) that preserved the .headers
26 interface and also provided access to the "unredirected" headers. It's
27 probably too late to fix that, though.
28
29
30 Check case normalization:
31
32 >>> url = "http://example.com"
33 >>> Request(url, headers={"Spam-eggs": "blah"}).headers["Spam-Eggs"]
34 'blah'
35 >>> Request(url, headers={"spam-EggS": "blah"}).headers["Spam-Eggs"]
36 'blah'
37
38 Currently, Request(url, "Spam-eggs").headers["Spam-eggs"] raises KeyError,
39 but that could be changed in future.
40
41
42 Request Headers Methods
43 -----------------------
44
45 Note the case normalization of header names here, to .capitalize()-case.
46 This should be preserved for backwards-compatibility. (In the HTTP case,
47 normalization to .title()-case is done by urllib2 before sending headers to
48 httplib).
49
50 >>> url = "http://example.com"
51 >>> r = Request(url, headers={"Spam-eggs": "blah"})
52 >>> r.has_header("Spam-eggs")
53 True
54 >>> r.header_items()
55 [('Spam-Eggs', 'blah')]
56 >>> r.add_header("Foo-Bar", "baz")
57 >>> items = r.header_items()
58 >>> items.sort()
59 >>> items
60 [('Foo-Bar', 'baz'), ('Spam-Eggs', 'blah')]
61 >>> r.has_header("Not-there")
62 False
63 >>> print(r.get_header("Not-there"))
64 None
65 >>> r.get_header("Not-there", "default")
66 'default'
67
68
69 Header Order
70 -------------