if "Pragma" not in request.headers:
request.headers["Pragma"] = ""
- # Request headers may be either a regular dict or HTTPHeaders object
- if isinstance(request.headers, httputil.HTTPHeaders):
- curl.setopt(pycurl.HTTPHEADER,
- [native_str("%s: %s" % i) for i in request.headers.get_all()])
- else:
- curl.setopt(pycurl.HTTPHEADER,
- [native_str("%s: %s" % i) for i in request.headers.items()])
+ curl.setopt(pycurl.HTTPHEADER,
+ ["%s: %s" % (native_str(k), native_str(v))
+ for k, v in request.headers.get_all()])
curl.setopt(pycurl.HEADERFUNCTION,
functools.partial(self._curl_header_callback,
from tornado.test.util import unittest, skipOnTravis
from tornado.util import u
from tornado.web import Application, RequestHandler, url
-from tornado.httputil import format_timestamp
+from tornado.httputil import format_timestamp, HTTPHeaders
class HelloWorldHandler(RequestHandler):
finally:
client.close()
+ def test_header_types(self):
+ # Header values may be passed as character or utf8 byte strings,
+ # in a plain dictionary or an HTTPHeaders object.
+ # Keys must always be the native str type.
+ # All combinations should have the same results on the wire.
+ for value in [u("MyUserAgent"), b"MyUserAgent"]:
+ for container in [dict, HTTPHeaders]:
+ headers = container()
+ headers['User-Agent'] = value
+ resp = self.fetch('/user_agent', headers=headers)
+ self.assertEqual(
+ resp.body, b"MyUserAgent",
+ "response=%r, value=%r, container=%r" %
+ (resp.body, value, container))
+
def test_304_with_content_length(self):
# According to the spec 304 responses SHOULD NOT include
# Content-Length or other entity headers, but some servers do it
python \
# py3*: -b turns on an extra warning when calling
# str(bytes), and -bb makes it an error.
- {py32,py33,py34,pypy3}: -bb \
+ {py3,py32,py33,py34,pypy3}: -bb \
# Python's optimized mode disables the assert statement, so
# run the tests in this mode to ensure we haven't fallen into
# the trap of relying on an assertion's side effects or using