Skip to content

Commit

Permalink
Somewhat smarter way of knowing whether we should send JSON or not. S…
Browse files Browse the repository at this point in the history
…hould fix #130 (it's never too late)
  • Loading branch information
tomas committed Nov 25, 2016
1 parent 81880ae commit 7d3e5a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/needle.js
Expand Up @@ -263,6 +263,9 @@ Needle.prototype.start = function() {

var body, config = this.setup(uri, options);

// assume boss wants JSON either if json is true, or if content-type was set and JSON ain't false (weird).
var json = options.json || (options.json !== false && config.headers['content-type'] == 'application/json');

if (data) {

if (options.multipart) { // boss says we do multipart. so we do it.
Expand All @@ -286,7 +289,7 @@ Needle.prototype.start = function() {

body = data; // use the raw buffer or stream as request body.

} else if (method.toUpperCase() == 'GET' && !options.json) {
} else if (method.toUpperCase() == 'GET' && !json) {

// append the data to the URI as a querystring.
uri = uri.replace(/\?.*|$/, '?' + stringify(data));
Expand All @@ -295,7 +298,7 @@ Needle.prototype.start = function() {

// if string, leave it as it is, otherwise, stringify.
body = (typeof(data) === 'string') ? data
: options.json ? JSON.stringify(data) : stringify(data);
: json ? JSON.stringify(data) : stringify(data);

// ensure we have a buffer so bytecount is correct.
body = new Buffer(body, config.encoding);
Expand All @@ -309,12 +312,12 @@ Needle.prototype.start = function() {

// if no content-type was passed, determine if json or not.
if (!config.headers['content-type']) {
config.headers['content-type'] = options.json
config.headers['content-type'] = json
? 'application/json; charset=utf-8'
: 'application/x-www-form-urlencoded'; // no charset says W3 spec.
}

// unless a specific accept header was passed, assume json wants json back.
// unless a specific accept header is set, assume json:true wants json back.
if (options.json && config.headers['accept'] === defaults.accept)
config.headers['accept'] = 'application/json';
}
Expand Down
40 changes: 39 additions & 1 deletion test/post_data_spec.js
Expand Up @@ -593,6 +593,44 @@ describe('post data (e.g. request body)', function() {

})

describe('with json: undefined but content-type = application/json', function() {

var opts = { headers: { 'content-type': 'application/json' } };

it('sends request', function(done) {
spystub_request();

post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
check_request('post');
done();
})
})

it('doesnt change Content-Type header', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
resp.body.headers['content-type'].should.equal('application/json');
done();
})
})

it('leaves default Accept header', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
resp.body.headers['accept'].should.equal('*/*');
done();
})
})

it('writes JSON.stringified object', function(done) {
post({ foo: 'bar', test: '测试' }, opts, function(err, resp) {
spy.called.should.be.true;
var json = JSON.stringify({ foo: 'bar', test: '测试' })
spy.args[0][0].toString().should.eql(json)
resp.body.body.should.eql(json);
done();
})
})
})

describe('with json: true', function() {

it('sends request', function(done) {
Expand Down Expand Up @@ -781,7 +819,7 @@ describe('post data (e.g. request body)', function() {
})
})

it('writes JSON.stringified object', function(done) {
it('passes raw buffer (assuming its a JSON string beneath)', function(done) {
post(new Buffer('foobar'), { json: true }, function(err, resp) {
spy.called.should.be.true;
spy.args[0][0].toString().should.eql('foobar')
Expand Down

0 comments on commit 7d3e5a5

Please sign in to comment.