diff --git a/swift/common/swob.py b/swift/common/swob.py index 49cceb71ec..a461a53288 100644 --- a/swift/common/swob.py +++ b/swift/common/swob.py @@ -639,15 +639,16 @@ class Request(object): 'SERVER_PROTOCOL': 'HTTP/1.0', 'wsgi.version': (1, 0), 'wsgi.url_scheme': 'http', - 'wsgi.input': StringIO(body or ''), 'wsgi.errors': StringIO(''), 'wsgi.multithread': False, 'wsgi.multiprocess': False } - env.update(PATH_INFO=path_info) env.update(environ) if body is not None: - env.update(CONTENT_LENGTH=str(len(body))) + env['wsgi.input'] = StringIO(body) + env['CONTENT_LENGTH'] = str(len(body)) + elif 'wsgi.input' not in env: + env['wsgi.input'] = StringIO('') req = Request(env) for key, val in headers.iteritems(): req.headers[key] = val diff --git a/test/unit/common/test_swob.py b/test/unit/common/test_swob.py index b3d2329bad..f43343f1ab 100644 --- a/test/unit/common/test_swob.py +++ b/test/unit/common/test_swob.py @@ -17,6 +17,7 @@ import unittest import datetime +from StringIO import StringIO import swift.common.swob @@ -195,6 +196,16 @@ class TestRequest(unittest.TestCase): self.assertEquals(req.headers['Content-Type'], 'text/plain') self.assertEquals(req.method, 'POST') + def test_blank_body_precedence(self): + req = swift.common.swob.Request.blank( + '/', environ={'REQUEST_METHOD': 'POST', + 'wsgi.input': StringIO('')}, + headers={'Content-Type': 'text/plain'}, body='hi') + self.assertEquals(req.path_info, '/') + self.assertEquals(req.body, 'hi') + self.assertEquals(req.headers['Content-Type'], 'text/plain') + self.assertEquals(req.method, 'POST') + def test_params(self): req = swift.common.swob.Request.blank('/?a=b&c=d') self.assertEquals(req.params['a'], 'b') @@ -218,8 +229,8 @@ class TestRequest(unittest.TestCase): self.assertEquals(req.path_info_pop(), None) def test_copy_get(self): - req = swift.common.swob.Request.blank('/hi/there', - environ={'REQUEST_METHOD': 'POST'}) + req = swift.common.swob.Request.blank( + '/hi/there', environ={'REQUEST_METHOD': 'POST'}) self.assertEquals(req.method, 'POST') req2 = req.copy_get() self.assertEquals(req2.method, 'GET') diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 73df5c3e1c..b27f9b5215 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -33,6 +33,7 @@ from eventlet import sleep from swift.common.swob import Request from swift.common import wsgi + class TestWSGI(unittest.TestCase): """ Tests for swift.common.wsgi """ @@ -217,6 +218,10 @@ class TestWSGI(unittest.TestCase): {'QUERY_STRING': 'original'}, 'GET', 'path?') self.assertEquals(r.query_string, '') + def test_pre_auth_req_with_body(self): + r = wsgi.make_pre_authed_request( + {'QUERY_STRING': 'original'}, 'GET', 'path', 'the body') + self.assertEquals(r.body, 'the body') if __name__ == '__main__': unittest.main()