Skip to content

Commit

Permalink
Tested with Node 0.3.7 in preparation for Node 0.4.0's arrival.
Browse files Browse the repository at this point in the history
Removed dependency on base64 library.

Using MD5 to check validity of uploaded file.

Fixed few tests failing due to Express 1.0.1 on Node 0.3.x.
  • Loading branch information
assaf committed Feb 1, 2011
1 parent 2d291fe commit bf31ec7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
12 changes: 4 additions & 8 deletions CHANGELOG.md
Expand Up @@ -4,20 +4,16 @@ zombie.js-changelog(7) -- Changelog

### Version 0.8.12 Pending

Tested with Node 0.3.7 in preparation for Node 0.4.0 (Assaf Arkin).

Fix firing the `change` event on `SELECT` elements when using jQuery
(Damian Janowski).

You can now run Zombie without base64 module, if you're not planning on
uploading binary files. If you are uploading binary files, you'll need
to `npm install base64` (Assaf Arkin).

Tested with Node 0.3.7 (Assaf Arkin).

Fix for `jQuery.ajax` receiving a non-string `data` option (Damian
Janowski).

283 Tests
4.6 sec to complete
284 Tests
4.9 sec to complete


### Version 0.8.11 2011-01-25
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -30,16 +30,15 @@
"test": "vows"
},
"engines": {
"node": ">= 0.2 < 0.3"
"node": ">= 0.2.5"
},
"dependencies": {
"html5": "0.2.12",
"jsdom": "0.1.23",
"mime": "1.2.1"
},
"devDependencies": {
"base64": ">1.0.1",
"coffee-script": ">= 1.0.0",
"coffee-script": ">= 1.0.1",
"docco": "0.3.0",
"express": "1.0.3",
"ronn": "0.3.5",
Expand Down
25 changes: 21 additions & 4 deletions spec/forms-spec.coffee
@@ -1,5 +1,7 @@
require("./helpers")
{ vows: vows, assert: assert, zombie: zombie, brains: brains } = require("vows")
fs = require("fs")
crypto = require("crypto")


brains.get "/forms/form", (req, res)-> res.send """
Expand Down Expand Up @@ -79,7 +81,18 @@ brains.get "/forms/form", (req, res)-> res.send """
</body>
</html>
"""
brains.post "/forms/submit", (req, res)-> res.send """
brains.post "/forms/submit", (req, res)->
# These fixes necessary with Express 1.0.3 under Node 0.3.x. Otherwise,
# bodyDecoder takes care of these mapping.
req.body.hungry ||= req.body["hungry[]"]
req.body.hobbies ||= req.body["hobbies[]"]
if req.body["addresses[][street]"]
req.body.addresses = []
for i,j of req.body["addresses[][street]"]
req.body.addresses.push street: j
req.body.addresses.push city: req.body["addresses[][city]"][i]

res.send """
<html>
<body>
<div id="name">#{req.body.name}</div>
Expand Down Expand Up @@ -119,10 +132,11 @@ brains.get "/forms/upload", (req, res)-> res.send """
"""
brains.post "/forms/upload", (req, res)->
[text, image] = [req.body.text, req.body.image]
digest = crypto.createHash("md5").update(image).digest("hex") if image
res.send """
<html>
<head><title>#{text?.filename || image?.filename}</title></head>
<body>#{text || image?.length}</body>
<body>#{text || digest}</body>
</html>
"""

Expand Down Expand Up @@ -405,9 +419,12 @@ vows.describe("Forms").addBatch(
"file upload (binary)":
zombie.wants "http://localhost:3003/forms/upload"
topic: (browser)->
filename = __dirname + "/data/zombie.jpg"
browser.attach("image", filename).pressButton "Upload", @callback
@filename = __dirname + "/data/zombie.jpg"
browser.attach("image", @filename).pressButton "Upload", @callback
"should upload include name": (browser)-> assert.equal browser.text("title"), "zombie.jpg"
"should upload file": (browser)->
digest = crypto.createHash("md5").update(fs.readFileSync(@filename)).digest("hex")
assert.equal browser.text("body").trim(), digest

"file upload (empty)":
zombie.wants "http://localhost:3003/forms/upload"
Expand Down
6 changes: 2 additions & 4 deletions spec/helpers.coffee
Expand Up @@ -108,9 +108,7 @@ express.bodyDecoder.decode["multipart/form-data"] = (body)->
headers
, {}

# Base64 encoding is optional.
if headers["content-transfer-encoding"] == "base64"
contents = require("base64").decode(contents)
contents = new Buffer(contents, "base64") if headers["content-transfer-encoding"] == "base64"
contents.mime = headers["content-type"].split(/;/)[0]

# We're looking for the content-disposition header, which has
Expand All @@ -124,7 +122,7 @@ express.bodyDecoder.decode["multipart/form-data"] = (body)->
# From content disposition we can tell the field name, if it's a
# file upload, also the file name. Content type is separate
# header.
contents = new String(contents)
contents = new String(contents) if typeof contents is "string"
contents.filename = pairs.filename if pairs.filename
parts[pairs.name] = contents if pairs.name
parts
Expand Down
19 changes: 7 additions & 12 deletions src/zombie/resources.coffee
Expand Up @@ -171,23 +171,18 @@ class Resources extends Array
content = value
mime = "text/plain"

switch encoding
when "base64" then content = content.toString("base64")
when "7bit" then content = content.toString("ascii")
when null
else throw new Error("Unsupported transfer encoding #{encoding}")

lines.push disp
lines.push "Content-Type: #{mime}"
lines.push "Content-Length: #{content.length}"
lines.push "Content-Transfer-Encoding: #{encoding}" if encoding
lines.push ""
switch encoding
when "base64"
# Base64 encoding is optional, loaded on demand.
try
base64 = require("base64")
catch ex
throw new Error("Base64 encoding support is optional, you need to `npm install base64`")
lines.push base64.encode(content).replace(/(.{76})/g, "$1\r\n")
when null
lines.push content
else
throw new Error("Unsupported transfer encoding #{encoding}")
lines.push content
lines.push "--#{boundary}"
)
if lines.length < 2
Expand Down

0 comments on commit bf31ec7

Please sign in to comment.