From 83e720767257d973997ff394498f858462b3a39c Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Tue, 28 Jun 2011 17:50:51 -0400 Subject: [PATCH 1/2] Allow passing an object as options.body to Browser.post. Use qs.stringify to turn it into a query string. --- lib/browser.js | 6 +++++- test/browser.navigation.test.js | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index 94cc38e..da2d44b 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -15,7 +15,8 @@ var EventEmitter = require('events').EventEmitter , jsdom = require('jsdom') , jQuery = require('./jquery/core') , url = require('url') - , http = require('http'); + , http = require('http') + , qs = require('qs'); /** * Starting portno. @@ -157,6 +158,9 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){ // Request body if (options.body) { + if (typeof options.body === 'object') { + options.body = qs.stringify(options.body); + } headers['Content-Length'] = options.body.length; headers['Content-Type'] = headers['Content-Type'] || 'application/x-www-form-urlencoded'; } diff --git a/test/browser.navigation.test.js b/test/browser.navigation.test.js index 72d5fed..11dcf83 100644 --- a/test/browser.navigation.test.js +++ b/test/browser.navigation.test.js @@ -294,11 +294,22 @@ module.exports = { }); }, - 'test .post(path) with passed body': function(done) { + 'test .post(path) with passed body string': function(done) { var browser = tobi.createBrowser(app); - browser.post('/form', {body: "foo=bar"}, function(res, $){ + browser.post('/form', {body: 'foo=bar'}, function(res, $){ res.should.have.status(200); - res.body.body.should.eql({foo:"bar"}); + res.body.body.should.eql({foo:'bar'}); + browser.should.have.property('path', '/form'); + browser.history.should.eql(['/form']); + done(); + }); + }, + + 'test .post(path) with passed body object': function(done) { + var browser = tobi.createBrowser(app); + browser.post('/form', {body: {foo:'bar'}}, function(res, $){ + res.should.have.status(200); + res.body.body.should.eql({foo:'bar'}); browser.should.have.property('path', '/form'); browser.history.should.eql(['/form']); done(); From c03e907771b5a76c7aa65e0fe952b29568cb04a8 Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Wed, 29 Jun 2011 14:58:52 -0400 Subject: [PATCH 2/2] Change object detection for options.body, update qs dependency to >= 0.2.0 --- lib/browser.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index da2d44b..105f00e 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -158,7 +158,7 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){ // Request body if (options.body) { - if (typeof options.body === 'object') { + if ('[object Object]' === Object.prototype.toString.call(options.body)) { options.body = qs.stringify(options.body); } headers['Content-Length'] = options.body.length; diff --git a/package.json b/package.json index 0d59b70..0bcf179 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "jsdom": ">= 0.1.21" , "htmlparser": ">= 1.7.3" , "should": ">= 0.0.4" - , "qs": ">= 0.1.0" + , "qs": ">= 0.2.0" } , "devDependencies": { "express": "2.3.x"