Skip to content

Commit

Permalink
Merge pull request #1119 from visionmedia/boolfield
Browse files Browse the repository at this point in the history
Support bools and arrays in .field()
  • Loading branch information
kornelski authored Nov 27, 2016
2 parents 8f1a90c + 7de30ef commit 47557d6
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ Request.prototype.end = function(fn){
if (this._withCredentials) xhr.withCredentials = true;

// body
if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !this._isHost(data)) {
if (!this._formData && 'GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !this._isHost(data)) {
// serialize stuff
var contentType = this._header['content-type'];
var serialize = this._serializer || request.serialize[contentType ? contentType.split(';')[0] : ''];
Expand Down
10 changes: 10 additions & 0 deletions lib/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,20 @@ RequestBase.prototype.field = function(name, val) {
return this;
}

if (Array.isArray(val)) {
for (var i in val) {
this.field(name, val[i]);
}
return this;
}

// val should be defined now
if (null === val || undefined === val) {
throw new Error('.field(name, val) val can not be empty');
}
if ('boolean' === typeof val) {
val = '' + val;
}
this._getFormData().append(name, val);
return this;
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"express-session": "^1.13.0",
"marked": "^0.3.5",
"mocha": "^3.1.2",
"multer": "^1.2.0",
"should": "^11.1.1",
"should-http": "^0.0.4",
"zuul": "^3.11.1"
Expand Down
68 changes: 67 additions & 1 deletion test/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ var base = setup.uri;
var should = require('should');
var request = require('../');

var assert = require('assert');
if (!assert.deepStrictEqual) assert.deepStrictEqual = assert.deepEqual;

var formDataSupported = setup.NODE || 'undefined' !== FormData;

describe('req.send(Object) as "form"', function(){
describe('with req.type() set to form', function(){
it('should send x-www-form-urlencoded data', function(done){
Expand Down Expand Up @@ -35,6 +40,67 @@ describe('req.send(Object) as "form"', function(){
})

describe('req.field', function(){
it('allow bools', function(done){
if (!formDataSupported) {
return done();
}

request
.post(base + '/formecho')
.field('bools', true)
.field('strings', 'true')
.end(function(err, res){
assert.ifError(err);
assert.deepStrictEqual(res.body, {bools:'true', strings:'true'});
done();
});
});

it('allow objects', function(done){
if (!formDataSupported) {
return done();
}

request
.post(base + '/formecho')
.field({bools: true, strings: 'true'})
.end(function(err, res){
assert.ifError(err);
assert.deepStrictEqual(res.body, {bools:'true', strings:'true'});
done();
});
});

it('works with arrays in objects', function(done){
if (!formDataSupported) {
return done();
}

request
.post(base + '/formecho')
.field({numbers: [1,2,3]})
.end(function(err, res){
assert.ifError(err);
assert.deepStrictEqual(res.body, {numbers:['1','2','3']});
done();
});
});

it('works with arrays', function(done){
if (!formDataSupported) {
return done();
}

request
.post(base + '/formecho')
.field('letters', ['a', 'b', 'c'])
.end(function(err, res){
assert.ifError(err);
assert.deepStrictEqual(res.body, {letters: ['a', 'b', 'c']});
done();
});
});

it('throw when empty', function(){
should.throws(function(){
request
Expand All @@ -48,4 +114,4 @@ describe('req.field', function(){
.field('name')
}, /val/);
});
});
});
10 changes: 10 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var express = require('express');
var multer = require('multer');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var basicAuth = require('basic-auth-connect');
Expand All @@ -25,6 +26,15 @@ app.all('/unique', function(req, res){
});

app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer().none());

app.all('/formecho', function(req, res){
if (!/application\/x-www-form-urlencoded|multipart\/form-data/.test(req.headers['content-type'])) {
return res.status(400).end("wrong type");
}
res.json(req.body);
});

app.use(bodyParser.json());
app.use(cookieParser());

Expand Down

0 comments on commit 47557d6

Please sign in to comment.