From df96b2001f0e86f144845e08be40184185eab319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sun, 27 Nov 2016 15:31:45 +0000 Subject: [PATCH 1/6] Safer check than isHost --- lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 24417ec17..5dd132ded 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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] : '']; From ec392c0e35a0d3b0ae74a7727de7608258f218f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sun, 27 Nov 2016 13:44:49 +0000 Subject: [PATCH 2/6] Fix #994 --- lib/request-base.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/request-base.js b/lib/request-base.js index f053946d3..94837dd79 100644 --- a/lib/request-base.js +++ b/lib/request-base.js @@ -237,6 +237,9 @@ RequestBase.prototype.field = function(name, val) { 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; }; From 4c4fa98f6730be9790cd3c06f245207d6ac3893d 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 3/6] Objects are allowed in .field() Closes #728 --- package.json | 1 + test/form.js | 26 +++++++++++++++++++++++++- test/support/server.js | 10 ++++++++++ 3 files changed, 36 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..68d491cbf 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,29 @@ describe('req.send(Object) as "form"', function(){ }) describe('req.field', function(){ + it('allow bools', function(done){ + request + .post(base + '/formecho') + .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') + .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 +72,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()); From 37ce15ca7dfd5051537aad5e8e2cf574c6178c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sun, 27 Nov 2016 20:25:06 +0000 Subject: [PATCH 4/6] Use assert.deepStrictEqual where possible --- test/form.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/form.js b/test/form.js index 68d491cbf..1f026d012 100644 --- a/test/form.js +++ b/test/form.js @@ -4,6 +4,8 @@ var should = require('should'); var assert = require('assert'); var request = require('../'); +if (!assert.deepStrictEqual) assert.deepStrictEqual = assert.deepEqual; + 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){ @@ -43,7 +45,7 @@ describe('req.field', function(){ .field('strings', 'true') .end(function(err, res){ assert.ifError(err); - assert.deepEqual(res.body, {bools:'true', strings:'true'}); + assert.deepStrictEqual(res.body, {bools:'true', strings:'true'}); done(); }); }); @@ -54,7 +56,7 @@ describe('req.field', function(){ .field({bools: true, strings: 'true'}) .end(function(err, res){ assert.ifError(err); - assert.deepEqual(res.body, {bools:'true', strings:'true'}); + assert.deepStrictEqual(res.body, {bools:'true', strings:'true'}); done(); }); }); From a040d243940d4b2450112a94d7dc5bf85c51745d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sun, 27 Nov 2016 20:26:54 +0000 Subject: [PATCH 5/6] Support arrays in .field() --- lib/request-base.js | 7 +++++++ test/form.js | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/request-base.js b/lib/request-base.js index 94837dd79..358bc541b 100644 --- a/lib/request-base.js +++ b/lib/request-base.js @@ -233,6 +233,13 @@ 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'); diff --git a/test/form.js b/test/form.js index 1f026d012..f1df1d50f 100644 --- a/test/form.js +++ b/test/form.js @@ -61,6 +61,28 @@ describe('req.field', function(){ }); }); + it('works with arrays in objects', function(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){ + 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 From 7de30ef2ca01ca18377925ea2537674f0a09d5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sun, 27 Nov 2016 20:41:31 +0000 Subject: [PATCH 6/6] Skip test in IE9 --- test/form.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/form.js b/test/form.js index f1df1d50f..be17e39f1 100644 --- a/test/form.js +++ b/test/form.js @@ -1,11 +1,13 @@ var setup = require('./support/setup'); var base = setup.uri; var should = require('should'); -var assert = require('assert'); 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){ @@ -39,6 +41,10 @@ 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) @@ -51,6 +57,10 @@ describe('req.field', function(){ }); it('allow objects', function(done){ + if (!formDataSupported) { + return done(); + } + request .post(base + '/formecho') .field({bools: true, strings: 'true'}) @@ -62,6 +72,10 @@ describe('req.field', function(){ }); it('works with arrays in objects', function(done){ + if (!formDataSupported) { + return done(); + } + request .post(base + '/formecho') .field({numbers: [1,2,3]}) @@ -73,6 +87,10 @@ describe('req.field', function(){ }); it('works with arrays', function(done){ + if (!formDataSupported) { + return done(); + } + request .post(base + '/formecho') .field('letters', ['a', 'b', 'c'])