[wwwsearch-commits] r31210 - in wwwsearch/mechanize/trunk: docs mechanize test
jjlee at codespeak.net
jjlee at codespeak.net
Thu Aug 10 00:01:04 CEST 2006
Author: jjlee
Date: Thu Aug 10 00:01:03 2006
New Revision: 31210
Added:
wwwsearch/mechanize/trunk/docs/
Modified:
wwwsearch/mechanize/trunk/mechanize/_http.py
wwwsearch/mechanize/trunk/test/test_urllib2.py
Log:
Fix HTTP-EQUIV with no content attribute case (Pratik Dam <pdam at juniper.net>); Fix assumption that httplib.HTTPMessage treats dict-style __setitem__ as append rather than set (where on earth did I get that from?)
Modified: wwwsearch/mechanize/trunk/mechanize/_http.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_http.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_http.py Thu Aug 10 00:01:03 2006
@@ -148,7 +148,7 @@
http_equiv = self.unescape_attr_if_required(value)
elif key == "content":
content = self.unescape_attr_if_required(value)
- if http_equiv is not None:
+ if http_equiv is not None and content is not None:
self.http_equiv.append((http_equiv, content))
def end_head(self):
@@ -280,9 +280,9 @@
def http_response(self, request, response):
if not hasattr(response, "seek"):
response = response_seek_wrapper(response)
- headers = response.info()
+ http_message = response.info()
url = response.geturl()
- ct_hdrs = response.info().getheaders("content-type")
+ ct_hdrs = http_message.getheaders("content-type")
if is_html(ct_hdrs, url, self._allow_xhtml):
try:
try:
@@ -294,8 +294,11 @@
pass
else:
for hdr, val in html_headers:
- # rfc822.Message interprets this as appending, not clobbering
- headers[hdr] = val
+ # add a header
+ http_message.dict[hdr.lower()] = val
+ text = hdr + ": " + val
+ for line in text.split("\n"):
+ http_message.headers.append(line + "\n")
return response
https_response = http_response
Modified: wwwsearch/mechanize/trunk/test/test_urllib2.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_urllib2.py (original)
+++ wwwsearch/mechanize/trunk/test/test_urllib2.py Thu Aug 10 00:01:03 2006
@@ -9,7 +9,7 @@
# ProxyHandler, CustomProxy, CustomProxyHandler (I don't use a proxy)
# GopherHandler (haven't used gopher for a decade or so...)
-import unittest, StringIO, os, sys, UserDict
+import unittest, StringIO, os, sys, UserDict, httplib
import mechanize
@@ -36,10 +36,18 @@
def readline(self, count=None): pass
def close(self): pass
-class MockHeaders(dict):
- def getheaders(self, name):
- name = name.lower()
- return [v for k, v in self.iteritems() if name == k.lower()]
+def http_message(mapping):
+ """
+ >>> http_message({"Content-Type": "text/html"}).items()
+ [('content-type', 'text/html')]
+
+ """
+ f = []
+ for kv in mapping.items():
+ f.append("%s: %s" % kv)
+ f.append("")
+ msg = httplib.HTTPMessage(StringIO.StringIO("\r\n".join(f)))
+ return msg
class MockResponse(StringIO.StringIO):
def __init__(self, code, msg, headers, data, url=None):
@@ -764,15 +772,19 @@
req = Request("http://example.com/")
r = MockResponse(
200, "OK",
- MockHeaders({"Foo": "Bar", "Content-type": "text/html"}),
+ http_message({"Foo": "Bar",
+ "Content-type": "text/html",
+ "Refresh": "blah"}),
'<html><head>'
'<meta http-equiv="Refresh" content="spam&eggs">'
- '</head></html>'
+ '</head></html>',
+ "http://example.com/"
)
newr = h.http_response(req, r)
headers = newr.info()
- self.assert_(headers["Refresh"] == "spam&eggs")
self.assert_(headers["Foo"] == "Bar")
+ self.assert_(headers["Refresh"] == "spam&eggs")
+ self.assert_(headers.getheaders("Refresh") == ["blah", "spam&eggs"])
def test_refresh(self):
# XXX test processor constructor optional args
@@ -786,8 +798,8 @@
]:
o = h.parent = MockOpener()
req = Request("http://example.com/")
- headers = MockHeaders({"refresh": val})
- r = MockResponse(200, "OK", headers, "")
+ headers = http_message({"refresh": val})
+ r = MockResponse(200, "OK", headers, "", "http://example.com/")
newr = h.http_response(req, r)
if valid:
self.assertEqual(o.proto, "http")
@@ -809,7 +821,7 @@
req.origin_req_host = "example.com" # XXX
try:
method(req, MockFile(), code, "Blah",
- MockHeaders({"location": to_url}))
+ http_message({"location": to_url}))
except mechanize.HTTPError:
# 307 in response to POST requires user OK
self.assert_(code == 307 and data is not None)
@@ -825,7 +837,7 @@
# loop detection
def redirect(h, req, url=to_url):
h.http_error_302(req, MockFile(), 302, "Blah",
- MockHeaders({"location": url}))
+ http_message({"location": url}))
# Note that the *original* request shares the same record of
# redirections with the sub-requests caused by the redirections.
@@ -1025,7 +1037,10 @@
<meta http-equiv="moo" content="cow">
</html>
""",
- [("refresh", "1; http://example.com/"), ("foo", "bar")])
+ [("refresh", "1; http://example.com/"), ("foo", "bar")]),
+ ("""<meta http-equiv="refresh">
+ """,
+ [])
]
for html, result in htmls:
self.assertEqual(parse_head(StringIO.StringIO(html), HeadParser()), result)
@@ -1114,4 +1129,6 @@
if __name__ == "__main__":
+ import doctest
+ doctest.testmod()
unittest.main()
More information about the wwwsearch-commits
mailing list