| 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 |
|
| 19 |
check(["text/html"], ".txt", True) |
| 20 |
check(["text/plain"], ".html", False) |
| 21 |
|
| 22 |
check([], ".html", True) |
| 23 |
check([], ".gif", False) |
| 24 |
|
| 25 |
|
| 26 |
check([], ".xhtml", allow_xhtml) |
| 27 |
check(["text/xhtml"], ".xhtml", allow_xhtml) |
| 28 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 58 |
|
| 59 |
from mechanize._headersutil import parse_ns_headers |
| 60 |
|
| 61 |
|
| 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 |
|
| 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() |