Skip to content

Commit

Permalink
add File#toJSON() tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 31, 2012
1 parent 1dd3767 commit 4b5a7b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Expand Up @@ -182,6 +182,11 @@ here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/weba

If hash calculation was set, you can read the hex digest out of this var.

#### Formidable.File#toJSON()

This method returns a JSON-representation of the file, allowing you to
`JSON.stringify()` the file which is useful for logging and responding
to requests.

### Events

Expand Down
5 changes: 2 additions & 3 deletions lib/file.js
Expand Up @@ -38,13 +38,12 @@ File.prototype.toJSON = function() {
path: this.path,
name: this.name,
type: this.type,
hash: this.hash,
mtime: this.lastModifiedDate,
length: this.length,
filename: this.filename,
mime: this.mime
}
}
};
};

File.prototype.write = function(buffer, cb) {
var self = this;
Expand Down
33 changes: 33 additions & 0 deletions test/unit/test-file.js
@@ -0,0 +1,33 @@
var common = require('../common');
var test = require('utest');
var assert = common.assert;
var File = common.require('file');

var file;
var now = new Date;
test('IncomingForm', {
before: function() {
file = new File({
size: 1024,
path: '/tmp/cat.png',
name: 'cat.png',
type: 'image/png',
lastModifiedDate: now,
filename: 'cat.png',
mime: 'image/png'
})
},

'#toJSON()': function() {
var obj = file.toJSON();
var len = Object.keys(obj).length;
assert.equal(1024, obj.size);
assert.equal('/tmp/cat.png', obj.path);
assert.equal('cat.png', obj.name);
assert.equal('image/png', obj.type);
assert.equal('image/png', obj.mime);
assert.equal('cat.png', obj.filename);
assert.equal(now, obj.mtime);
assert.equal(len, 8);
}
});

0 comments on commit 4b5a7b9

Please sign in to comment.