Skip to content

Commit

Permalink
implement FormData.get and FormData.getAll
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Oct 17, 2018
1 parent b16916a commit 3d52f6c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/form_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function FormData(options) {
this._overheadLength = 0;
this._valueLength = 0;
this._valuesToMeasure = [];
this._entryList = {};

CombinedStream.call(this);

Expand Down Expand Up @@ -76,6 +77,19 @@ FormData.prototype.append = function(field, value, options) {

// pass along options.knownLength
this._trackLength(header, value, options);

if (!this._entryList[field]) {
this._entryList[field] = [];
}
this._entryList[field].push(value);
};

FormData.prototype.get = function(field) {
return this._entryList[field] ? this._entryList[field][0] : null;
};

FormData.prototype.getAll = function(field) {
return this._entryList[field] || [];
};

FormData.prototype._trackLength = function(header, value, options) {
Expand Down
33 changes: 33 additions & 0 deletions test/integration/test-form-get-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var common = require('../common');
var assert = common.assert;
var FormData = require(common.dir.lib + '/form_data');

(function testEmptyForm() {
var form = new FormData();

// Make sure our response is async
assert.strictEqual(form.get('foo'), null);
assert.deepEqual(form.getAll('foo'), []);
})();

(function testWithOneValue() {
var form = new FormData();

form.append('foo', 'bar');

// Make sure our response is async
assert.strictEqual(form.get('foo'), 'bar');
assert.deepEqual(form.getAll('foo'), ['bar']);
})();

(function testWithMultipleValue() {
var form = new FormData();

form.append('foo', 'bar');
form.append('foo', 'baz');
form.append('foo', 'bad');

// Make sure our response is async
assert.strictEqual(form.get('foo'), 'bar');
assert.deepEqual(form.getAll('foo'), ['bar', 'baz', 'bad']);
})();

0 comments on commit 3d52f6c

Please sign in to comment.