Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow passing a hash instead of a string in Browser#post's options.body. #42

Closed
wants to merge 1 commit into from

2 participants

@bantic

One more...

This changes it so that the "body" option for Browser.post can be a hash rather than a string. Makes tobi easier to use this way (before this I was having to properly escape the string by hand).

Allows one to do:

browser.post('/login', { body: {foo:"b&r",bar:"foo"} }, function(res, $){

});

The test includes the text "b&r" to ensure that the escaping is being done properly.

thanks.

@tj
tj commented

we should extend visionmedia/node-querystring (qs in npm) to add a stringify() method, handling the different cases

@bantic

I'll take a look at doing that.

@bantic

I made some changes to node-querystring and issued a pull request there...I'll re-open this later.

@bantic bantic closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 0 deletions.
  1. +9 −0 lib/browser.js
  2. +11 −0 test/browser.navigation.test.js
View
9 lib/browser.js
@@ -157,6 +157,15 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){
// Request body
if (options.body) {
+ if ("string" !== typeof options.body) {
+ var options_array = [];
+ for (var key in options.body) {
+ if (options.body.hasOwnProperty(key)) {
+ options_array.push( escape(key) + "=" + escape(options.body[key]) );
+ }
+ }
+ options.body = options_array.join("&");
+ }
headers['Content-Length'] = options.body.length;
headers['Content-Type'] = headers['Content-Type'] || 'application/x-www-form-urlencoded';
}
View
11 test/browser.navigation.test.js
@@ -305,6 +305,17 @@ module.exports = {
});
},
+ 'test .post(path) with passed body as a hash': function(done) {
+ var browser = tobi.createBrowser(app);
+ browser.post('/form', {body: {foo: "b&r", bar: "foo"}}, function(res, $){
+ res.should.have.status(200);
+ res.body.body.should.eql({foo:"b&r", bar: "foo"});
+ browser.should.have.property('path', '/form');
+ browser.history.should.eql(['/form']);
+ done();
+ });
+ },
+
'test .put(path)': function(done){
var browser = tobi.createBrowser(app);
browser.put('/', function(res, $){
Something went wrong with that request. Please try again.