]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Set altered HTTPRequest attributes as parameters. 945/head
authorChris McGuire <chris.r.mcguire@gmail.com>
Mon, 25 Nov 2013 18:08:03 +0000 (13:08 -0500)
committerChris McGuire <chris.r.mcguire@gmail.com>
Tue, 17 Dec 2013 18:13:56 +0000 (13:13 -0500)
tornado/httpclient.py
tornado/test/httpclient_test.py

index b58a834854936547dbdba904f7697486b113bc52..ca0305fd3df1cd82ec1df0adcddd705efe848a54 100644 (file)
@@ -346,8 +346,8 @@ class HTTPRequest(object):
         self.proxy_password = proxy_password
         self.url = url
         self.method = method
-        self.headers = headers
-        self.body = utf8(body)
+        self._headers = headers
+        self._body = utf8(body)
         self.auth_username = auth_username
         self.auth_password = auth_password
         self.auth_mode = auth_mode
@@ -358,9 +358,9 @@ class HTTPRequest(object):
         self.user_agent = user_agent
         self.use_gzip = use_gzip
         self.network_interface = network_interface
-        self.streaming_callback = stack_context.wrap(streaming_callback)
-        self.header_callback = stack_context.wrap(header_callback)
-        self.prepare_curl_callback = stack_context.wrap(prepare_curl_callback)
+        self._streaming_callback = stack_context.wrap(streaming_callback)
+        self._header_callback = stack_context.wrap(header_callback)
+        self._prepare_curl_callback = stack_context.wrap(prepare_curl_callback)
         self.allow_nonstandard_methods = allow_nonstandard_methods
         self.validate_cert = validate_cert
         self.ca_certs = ca_certs
@@ -369,6 +369,57 @@ class HTTPRequest(object):
         self.client_cert = client_cert
         self.start_time = time.time()
 
+    @property
+    def headers(self):
+        return self._headers
+
+    @headers.setter
+    def headers(self, value):
+        if value is None:
+            self._headers = httputil.HTTPHeaders()
+        else:
+            self._headers = value
+
+    @property
+    def if_modified_since(self):
+        return self.headers.get("If-Modified-Since")
+
+    @if_modified_since.setter
+    def if_modified_since(self, value):
+        self.headers["If-Modified-Since"] = httputil.format_timestamp(value)
+
+    @property
+    def body(self):
+        return self._body
+
+    @body.setter
+    def body(self, value):
+        self._body = utf8(value)
+
+    @property
+    def streaming_callback(self):
+        return self._streaming_callback
+
+    @streaming_callback.setter
+    def streaming_callback(self, value):
+        self._streaming_callback = stack_context.wrap(value)
+
+    @property
+    def header_callback(self):
+        return self._header_callback
+
+    @header_callback.setter
+    def header_callback(self, value):
+        self._header_callback = stack_context.wrap(value)
+
+    @property
+    def prepare_curl_callback(self):
+        return self._prepare_curl_callback
+
+    @prepare_curl_callback.setter
+    def prepare_curl_callback(self, value):
+        self._prepare_curl_callback = stack_context.wrap(value)
+
 
 class HTTPResponse(object):
     """HTTP Response object.
index b051e02ea143894e3c7cf4446f184570c915e37f..12a2981fe9ad02c11215a3c16b33f234716ee7dc 100644 (file)
@@ -8,6 +8,7 @@ from contextlib import closing
 import functools
 import sys
 import threading
+import time
 
 from tornado.escape import utf8
 from tornado.httpclient import HTTPRequest, HTTPResponse, _RequestProxy, HTTPError, HTTPClient
@@ -490,3 +491,44 @@ class SyncHTTPClientTest(unittest.TestCase):
         with self.assertRaises(HTTPError) as assertion:
             self.http_client.fetch(self.get_url('/notfound'))
         self.assertEqual(assertion.exception.code, 404)
+
+
+class HTTPRequestTestCase(unittest.TestCase):
+    def test_headers(self):
+        request = HTTPRequest('http://example.com', headers={'foo': 'bar'})
+        self.assertEqual(request.headers, {'foo': 'bar'})
+
+    def test_headers_setter(self):
+        request = HTTPRequest('http://example.com')
+        request.headers = {'bar': 'baz'}
+        self.assertEqual(request.headers, {'bar': 'baz'})
+
+    def test_null_headers_setter(self):
+        request = HTTPRequest('http://example.com')
+        request.headers = None
+        self.assertEqual(request.headers, {})
+
+    def test_if_modified_since(self):
+        request = HTTPRequest(
+            'http://example.com',
+            if_modified_since=time.strptime("01 Jan 10", "%d %b %y")
+        )
+        self.assertEqual(request.if_modified_since,
+                         'Fri, 01 Jan 2010 00:00:00 GMT')
+
+    def test_if_modified_since_setter(self):
+        request = HTTPRequest('http://example.com')
+        request.if_modified_since = time.strptime("02 Jan 10", "%d %b %y")
+        self.assertEqual(
+            request._headers,
+            {'If-Modified-Since': 'Sat, 02 Jan 2010 00:00:00 GMT'}
+        )
+
+    def test_body(self):
+        request = HTTPRequest('http://example.com', body='foo')
+        self.assertEqual(request.body, utf8('foo'))
+
+    def test_body_setter(self):
+        request = HTTPRequest('http://example.com')
+        request.body = 'foo'
+        self.assertEqual(request.body, utf8('foo'))