From 4b5a7b9cedce6d774737d8b42bad85ce7015fc1f Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Wed, 31 Oct 2012 10:53:56 -0700 Subject: [PATCH] add File#toJSON() tests and docs --- Readme.md | 5 +++++ lib/file.js | 5 ++--- test/unit/test-file.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/unit/test-file.js diff --git a/Readme.md b/Readme.md index 82d08da6..d431c975 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/lib/file.js b/lib/file.js index 52ffd010..b214e6cc 100644 --- a/lib/file.js +++ b/lib/file.js @@ -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; diff --git a/test/unit/test-file.js b/test/unit/test-file.js new file mode 100644 index 00000000..fc8f36e5 --- /dev/null +++ b/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); + } +}); \ No newline at end of file