Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use qs stringify for post body #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -157,6 +158,9 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){

// Request body
if (options.body) {
if ('[object Object]' === Object.prototype.toString.call(options.body)) {
options.body = qs.stringify(options.body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to abstract these out a little with an object mapping content types to functions say something like:

exports.stringify['application/x-www-form-urlencoded'] = qs.stringify

so then we could have application/json etc

}
headers['Content-Length'] = options.body.length;
headers['Content-Type'] = headers['Content-Type'] || 'application/x-www-form-urlencoded';
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 14 additions & 3 deletions test/browser.navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,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();
Expand Down