Debian Codegraph @

test/test_headers.py

raw symbols SCIP
1 """Tests for ClientCookie._HeadersUtil."""
2
3 import mechanize._headersutil
4 from mechanize._testcase import TestCase
5
6
7 class IsHtmlTests(TestCase):
8
9 def test_is_html(self):
10 def check(headers, extension, is_html):
11 url = "http://example.com/foo" + extension
12 self.assertEqual(
13 mechanize._headersutil.is_html(headers, url, allow_xhtml),
14 is_html)
15 for allow_xhtml in False, True:
16 check(["text/html"], ".html", True),
17 check(["text/html", "text/plain"], ".html", True)
18 # Content-type takes priority over file extension from URL
19 check(["text/html"], ".txt", True)
20 check(["text/plain"], ".html", False)
21 # use extension if no Content-Type
22 check([], ".html", True)
23 check([], ".gif", False)
24 # don't regard XHTML as HTML (unless user explicitly asks for it),
25 # since we don't yet handle XML properly
26 check([], ".xhtml", allow_xhtml)
27 check(["text/xhtml"], ".xhtml", allow_xhtml)
28 # header with empty value
29 check([""], ".txt", False)
30
31
32 class HeaderTests(TestCase):
33
34 def test_parse_ns_headers_expires(self):
35 from mechanize._headersutil import parse_ns_headers
36
37 # quotes should be stripped
38 self.assertEqual(
39 parse_ns_headers(['foo=bar; expires=01 Jan 2040 22:23:32 GMT']),
40 [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]])
41 self.assertEqual(
42 parse_ns_headers(['foo=bar; expires="01 Jan 2040 22:23:32 GMT"']),
43 [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]])
44
45 def test_parse_ns_headers_version(self):
46 from mechanize._headersutil import parse_ns_headers
47
48 # quotes should be stripped
49 expected = [[('foo', 'bar'), ('version', '1')]]
50 for hdr in [
51 'foo=bar; version="1"',
52 'foo=bar; Version="1"',
53 ]:
54 self.assertEqual(parse_ns_headers([hdr]), expected)
55
56 def test_parse_ns_headers_special_names(self):
57 # names such as 'expires' are not special in first name=value pair
58 # of Set-Cookie: header
59 from mechanize._headersutil import parse_ns_headers
60
61 # Cookie with name 'expires'
62 hdr = 'expires=01 Jan 2040 22:23:32 GMT'
63 expected = [
64 [("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
65 self.assertEqual(parse_ns_headers([hdr]), expected)
66
67 def test_join_header_words(self):
68 from mechanize._headersutil import join_header_words
69
70 assert join_header_words([[
71 ("foo", None), ("bar", "baz"), (None, "value")
72 ]]) == "foo; bar=baz; value"
73
74 assert join_header_words([[]]) == ""
75
76 def test_split_header_words(self):
77 from mechanize._headersutil import split_header_words
78
79 tests = [
80 ("foo", [[("foo", None)]]),
81 ("foo=bar", [[("foo", "bar")]]),
82 (" foo ", [[("foo", None)]]),
83 (" foo= ", [[("foo", "")]]),
84 (" foo=", [[("foo", "")]]),
85 (" foo= ; ", [[("foo", "")]]),
86 (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
87 ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
88 # doesn't really matter if this next fails, but it works ATM
89 ("foo= bar=baz", [[("foo", "bar=baz")]]),
90 ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
91 ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
92 ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
93 (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
94 [[("foo", None), ("bar", "baz")],
95 [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
96 ]
97
98 for arg, expect in tests:
99 try:
100 result = split_header_words([arg])
101 except Exception:
102 import traceback
103 from io import StringIO
104 f = StringIO()
105 traceback.print_exc(None, f)
106 result = "(error -- traceback follows)\n\n%s" % f.getvalue()
107 assert result == expect, """
108 When parsing: '%s'
109 Expected: '%s'
110 Got: '%s'
111 """ % (arg, expect, result)
112
113 def test_roundtrip(self):
114 from mechanize._headersutil import (
115 split_header_words, join_header_words)
116
117 tests = [
118 ("foo", "foo"),
119 ("foo=bar", "foo=bar"),
120 (" foo ", "foo"),
121 ("foo=", 'foo=""'),
122 ("foo=bar bar=baz", "foo=bar; bar=baz"),
123 ("foo=bar;bar=baz", "foo=bar; bar=baz"),
124 ('foo bar baz', "foo; bar; baz"),
125 (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
126 ('foo,,,bar', 'foo, bar'),
127 ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
128
129 ('text/html; charset=iso-8859-1',
130 'text/html; charset="iso-8859-1"'),
131
132 ('foo="bar"; port="80,81"; discard, bar=baz',
133 'foo=bar; port="80,81"; discard, bar=baz'),
134
135 (r'Basic realm="\"foo\\\\bar\""',
136 r'Basic; realm="\"foo\\\\bar\""')
137 ]
138
139 for arg, expect in tests:
140 input = split_header_words([arg])
141 res = join_header_words(input)
142 assert res == expect, """
143 When parsing: '%s'
144 Expected: '%s'
145 Got: '%s'
146 Input was: '%s'""" % (arg, expect, res, input)
147
148
149 if __name__ == "__main__":
150 import unittest
151 unittest.main()