Skip to content

Commit

Permalink
Objects are allowed in .field()
Browse files Browse the repository at this point in the history
Closes #728
  • Loading branch information
kornelski committed Nov 27, 2016
1 parent 96d08c4 commit 479b64e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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
28 changes: 27 additions & 1 deletion test/form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var setup = require('./support/setup');
var base = setup.uri;
var should = require('should');
var assert = require('assert');
var request = require('../');

describe('req.send(Object) as "form"', function(){
Expand Down Expand Up @@ -35,6 +36,31 @@ describe('req.send(Object) as "form"', function(){
})

describe('req.field', function(){
it('allow bools', function(done){
request
.post(base + '/formecho')
.type('form')
.field('bools', true)
.field('strings', 'true')
.end(function(err, res){
assert.ifError(err);
assert.deepEqual(res.body, {bools:'true', strings:'true'});
done();
});
});

it('allow objects', function(done){
request
.post(base + '/formecho')
.type('form')
.field({bools: true, strings: 'true'})
.end(function(err, res){
assert.ifError(err);
assert.deepEqual(res.body, {bools:'true', strings:'true'});
done();
});
});

it('throw when empty', function(){
should.throws(function(){
request
Expand All @@ -48,4 +74,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 479b64e

Please sign in to comment.