Skip to content

Commit 2acbeee

Browse files
author
Tom Willis
committed
syntax sugar: files upload syntax
1 parent 0b78bca commit 2acbeee

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

tests/test_quickstart.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def testUploadFiles(self):
228228
self.assert_("audio/mpeg" in req.body, l.PRINT_REQ(req))
229229
print l.PRINT_REQ(req)
230230

231+
231232
def testLogReqRes(self):
232233
IMG = b"\xff\xab"
233234
msg = l.PRINT_REQ(Request.blank("/", body=IMG, method="POST"))
@@ -286,6 +287,20 @@ def echo(environ, start_response):
286287
d.add("b", u"Nike %s" % TM)
287288
tc.get("/", query_string=d)
288289

290+
def testFiles(self):
291+
def echo(environ, start_response):
292+
r = Request(environ)
293+
return Response(r.body)(environ, start_response)
294+
tc = testing.TestClient(pipeline=echo)
295+
res = tc.post("/", files=dict(file1=("this.mp3", "i am music")))
296+
self.assertIn("this.mp3", res.body)
297+
298+
with self.assertRaises(KeyError):
299+
tc.post("/", post=dict(file1="xxx"),
300+
files=dict(file1=("this.mp3", "i am music")))
301+
302+
with self.assertRaises(ValueError):
303+
res = tc.post("/", files=dict(file1=("this.mp3")))
289304

290305

291306
class TestHoles(unittest.TestCase):

webobtoolkit/client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Client(object):
5555
"""
5656

5757
def __init__(self, pipeline=None, assert_=None):
58-
self._pipeline = pipeline or client_app
58+
self._pipeline = pipeline or client_pipeline()
5959
if assert_:
6060
self._assert_ = assert_
6161
else:
@@ -110,7 +110,7 @@ def head(self, url, query_string=None, headers={}, assert_=None):
110110
headers=headers,
111111
assert_=assert_)
112112

113-
def post(self, url, query_string=None, post={}, headers={}, assert_=None):
113+
def post(self, url, query_string=None, post={}, headers={}, assert_=None, files={}):
114114
"""
115115
make an HTTP POST Request and return the response
116116
@@ -130,6 +130,16 @@ def post(self, url, query_string=None, post={}, headers={}, assert_=None):
130130
present it will be ran for this call only rather than the one
131131
set on the client
132132
"""
133+
for k, v in files.items():
134+
try:
135+
filename, filevalue = v
136+
except ValueError:
137+
raise ValueError("files needs to contain 2 item sequences of (filename, filebody) %s" % v)
138+
139+
if k not in post:
140+
post[k] = (filename, filevalue)
141+
else:
142+
raise KeyError("%s already exists in post" % k)
133143

134144
return self(url=url,
135145
method="POST",

webobtoolkit/testing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111
from constants import STATUS_CODES
1212
from log import PRINT_REQ, PRINT_RES
13-
from client import Client, client_app
13+
from client import Client, client_pipeline
1414
from filters import auto_redirect_filter
1515

1616

@@ -53,7 +53,7 @@ class TestClient(Client):
5353
basically if anything other than 302 or 200 will result in a failure
5454
"""
5555
def __init__(self, pipeline=None, redirect=False):
56-
app = pipeline or client_app
56+
app = pipeline or client_pipeline()
5757
if redirect:
5858
app = auto_redirect_filter(app)
5959
else:
@@ -71,8 +71,8 @@ def head(self, url, query_string=None, headers={}, status=None):
7171
kw["assert_"] = get_assert(status)
7272
return Client.head(self, url, **kw)
7373

74-
def post(self, url, query_string=None, post={}, headers={}, status=None):
75-
kw = dict(query_string=query_string, headers=headers, post=post)
74+
def post(self, url, query_string=None, post={}, headers={}, files={}, status=None):
75+
kw = dict(query_string=query_string, headers=headers, post=post, files=files)
7676
kw["assert_"] = get_assert(status)
7777
return Client.post(self, url, **kw)
7878

0 commit comments

Comments
 (0)