Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add File#toJSON() tests and docs

  • Loading branch information...
commit 4b5a7b9cedce6d774737d8b42bad85ce7015fc1f 1 parent 1dd3767
@tj tj authored
Showing with 40 additions and 3 deletions.
  1. +5 −0 Readme.md
  2. +2 −3 lib/file.js
  3. +33 −0 test/unit/test-file.js
View
5 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
View
5 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;
View
33 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);
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.