Skip to content

Commit 4c1d7ca

Browse files
committed
bug fix in application.request
1 parent ec267d6 commit 4c1d7ca

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

test/application.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,20 @@ def g():
190190

191191
def testUnicodeInput(self):
192192
urls = (
193-
"/.*", "foo"
193+
"(/.*)", "foo"
194194
)
195195
class foo:
196-
def GET(self):
196+
def GET(self, path):
197197
i = web.input(name='')
198198
return repr(i.name)
199199

200-
def POST(self):
201-
return repr(web.data())
202-
i = web.input(name={})
203-
return repr(i)
200+
def POST(self, path):
201+
if path == '/multipart':
202+
i = web.input(file={})
203+
return i.file.value
204+
else:
205+
i = web.input()
206+
return repr(dict(i))
204207

205208
app = web.application(urls, locals())
206209

@@ -210,11 +213,14 @@ def f(name):
210213

211214
f(u'\u1234')
212215
f(u'foo')
216+
217+
response = app.request('/', method='POST', data=dict(name='foo'))
218+
self.assertEquals(response.data, "{'name': u'foo'}")
213219

214-
data = '--boundary\r\nContent-Disposition: form-data; name="name"; filename="a.txt"\r\nContent-Type: text/plain\r\n\r\na\r\n--boundary--\r\n'
215-
headers = {'Content-Type': 'multipart/form-data; boundary=--boundary'}
216-
response = app.request('/', method="POST", data=data, headers=headers)
217-
#self.assertEquals(response.data, 'a')
220+
data = '--boundary\r\nContent-Disposition: form-data; name="x"\r\nfoo\r\n--boundary\r\nContent-Disposition: form-data; name="file"; filename="a.txt"\r\nContent-Type: text/plain\r\n\r\na\r\n--boundary--\r\n'
221+
headers = {'Content-Type': 'multipart/form-data; boundary=boundary'}
222+
response = app.request('/multipart', method="POST", data=data, headers=headers)
223+
self.assertEquals(response.data, 'a')
218224

219225
def testCustomNotFound(self):
220226
urls_a = ("/", "a")
@@ -242,6 +248,6 @@ def assert_notfound(path, message):
242248
app.notfound = lambda: web.HTTPError("404 Not Found", {}, "not found 2")
243249
assert_notfound("/a/foo", "not found 1")
244250
assert_notfound("/b/foo", "not found 2")
245-
251+
246252
if __name__ == '__main__':
247253
webtest.main()

web/application.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,25 @@ def request(self, localpart='/', method='GET', data=None,
165165
env = {}
166166
env = dict(env, HTTP_HOST=host, REQUEST_METHOD=method, PATH_INFO=path, QUERY_STRING=query, HTTPS=str(https))
167167
headers = headers or {}
168+
168169
for k, v in headers.items():
169170
env['HTTP_' + k.upper().replace('-', '_')] = v
170-
171+
172+
if 'HTTP_CONTENT_LENGTH' in env:
173+
env['CONTENT_LENGTH'] = env.pop('HTTP_CONTENT_LENGTH')
174+
175+
if 'HTTP_CONTENT_TYPE' in env:
176+
env['CONTENT_TYPE'] = env.pop('HTTP_CONTENT_TYPE')
177+
171178
if data:
172179
import StringIO
173180
if isinstance(data, dict):
174181
q = urllib.urlencode(data)
175182
else:
176183
q = data
177184
env['wsgi.input'] = StringIO.StringIO(q)
185+
if not env.get('CONTENT_TYPE', '').lower().startswith('multipart/') and 'CONTENT_LENGTH' not in env:
186+
env['CONTENT_LENGTH'] = len(q)
178187
response = web.storage()
179188
def start_response(status, headers):
180189
response.status = status

0 commit comments

Comments
 (0)