From 479b64eb3b4b1f88d4e8e5740d910198877a11a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sun, 27 Nov 2016 19:57:34 +0000 Subject: [PATCH] Objects are allowed in .field() Closes #728 --- package.json | 1 + test/form.js | 28 +++++++++++++++++++++++++++- test/support/server.js | 10 ++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 764cc7873..390309226 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/form.js b/test/form.js index 5db150e7e..96a510c5a 100644 --- a/test/form.js +++ b/test/form.js @@ -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(){ @@ -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 @@ -48,4 +74,4 @@ describe('req.field', function(){ .field('name') }, /val/); }); -}); \ No newline at end of file +}); diff --git a/test/support/server.js b/test/support/server.js index d77424211..6d8a21564 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -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'); @@ -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());