From: Chris McGuire Date: Mon, 25 Nov 2013 18:08:03 +0000 (-0500) Subject: Set altered HTTPRequest attributes as parameters. X-Git-Tag: v3.2.0b1~22^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F945%2Fhead;p=thirdparty%2Ftornado.git Set altered HTTPRequest attributes as parameters. --- diff --git a/tornado/httpclient.py b/tornado/httpclient.py index b58a83485..ca0305fd3 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -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. diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index b051e02ea..12a2981fe 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -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'))