Skip to content

Commit

Permalink
Empty file uploads shuold not cause errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 4, 2011
1 parent 441ba41 commit 822377a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions spec/forms-spec.coffee
Expand Up @@ -78,11 +78,10 @@ brains.post "/upload", (req, res)->
res.send """
<html>
<head><title>#{text?.filename || image?.filename}</title></head>
<body>#{text || image.length}</body>
<body>#{text || image?.length}</body>
</html>
"""


vows.describe("Forms").addBatch(
"fill field":
zombie.wants "http://localhost:3003/form"
Expand Down Expand Up @@ -307,6 +306,13 @@ vows.describe("Forms").addBatch(
topic: (browser)->
@filename = __dirname + "/data/zombie.jpg"
browser.attach("image", @filename).pressButton "Upload", @callback
#"should upload file": (browser)-> assert.equal browser.text("body").trim(), "Random text"
"should upload include name": (browser)-> assert.equal browser.text("title"), @filename

"empty file upload":
zombie.wants "http://localhost:3003/upload"
topic: (browser)->
browser.attach "text", ""
browser.pressButton "Upload", @callback
"should not upload any file": (browser)-> assert.equal browser.text("body").trim(), "undefined"

).export(module)
5 changes: 4 additions & 1 deletion spec/helpers.coffee
Expand Up @@ -92,7 +92,8 @@ zombie.Browser.prototype.wants = (url, options, callback)->
# Handle multipart/form-data so we can test file upload.
express.bodyDecoder.decode["multipart/form-data"] = (body)->
# Find the boundary
if boundary = body.match(/^(--.*)\r\n(?:.|\n|\r)*\1--/m)[1]
match = body.match(/^(--.*)\r\n(?:.|\n|\r)*\1--/m)
if match && boundary = match[1]
# Split body at boundary, ignore first (opening) and last (closing)
# boundaries, and map the rest into name/value pairs.
body.split("#{boundary}").slice(1,-1).reduce (parts, part)->
Expand Down Expand Up @@ -125,6 +126,8 @@ express.bodyDecoder.decode["multipart/form-data"] = (body)->
parts[pairs.name] = contents if pairs.name
parts
, {}
else
{}


vows.zombie = zombie
Expand Down

0 comments on commit 822377a

Please sign in to comment.