Skip to content

Commit

Permalink
Fix #296 - Only set Content-Type if body exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Sep 25, 2012
1 parent e82dd8a commit 4797f88
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
9 changes: 7 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ Request.prototype.init = function (options) {
}
}
}
if (self._json && !self.headers['content-type'] && !self.headers['Content-Type'])
self.headers['content-type'] = 'application/json'
if (src.method && !self.method) {
self.method = src.method
}
Expand Down Expand Up @@ -789,12 +791,15 @@ Request.prototype.multipart = function (multipart) {
return self
}
Request.prototype.json = function (val) {
this.setHeader('content-type', 'application/json')
this.setHeader('accept', 'application/json')
this._json = true
if (typeof val === 'boolean') {
if (typeof this.body === 'object') this.body = JSON.stringify(this.body)
if (typeof this.body === 'object') {
this.setHeader('content-type', 'application/json')
this.body = JSON.stringify(this.body)
}
} else {
this.setHeader('content-type', 'application/json')
this.body = JSON.stringify(val)
}
return this
Expand Down
6 changes: 5 additions & 1 deletion tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports.createPostStream = function (text) {
setTimeout(function () {postStream.emit('data', new Buffer(text)); postStream.emit('end')}, 0);
return postStream;
}
exports.createPostValidator = function (text) {
exports.createPostValidator = function (text, reqContentType) {
var l = function (req, resp) {
var r = '';
req.on('data', function (chunk) {r += chunk})
Expand All @@ -57,6 +57,10 @@ exports.createPostValidator = function (text) {
}
if (r !== text) console.log(r, text);
assert.equal(r, text)
if (reqContentType) {
assert.ok(req.headers['content-type'])
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
}
resp.writeHead(200, {'content-type':'text/plain'})
resp.write('OK')
resp.end()
Expand Down
17 changes: 16 additions & 1 deletion tests/test-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ s.listen(s.port, function () {

s.on('/post', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers['content-type'], 'application/json');
assert.equal(req.headers['content-type'], null);
assert.equal(req.method, 'POST')
resp.writeHead(200, {'Content-Type': 'application/json'});
resp.end(JSON.stringify({foo:'bar'}));
Expand All @@ -36,6 +36,21 @@ s.listen(s.port, function () {
counter += 1;
});

s.on('/post-body', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers['content-type'], 'application/json');
assert.equal(req.method, 'POST')
resp.writeHead(200, {'Content-Type': 'application/json'});
resp.end(JSON.stringify({foo:'bar'}));
});

// test post(string, object, function) with body
request.defaults({headers:{foo:"bar"}}).post(s.url + '/post-body', {json: true, body:{bar:"baz"}}, function (e, r, b){
if (e) throw e;
assert.deepEqual('bar', b.foo);
counter += 1;
});

s.on('/del', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.method, 'DELETE')
Expand Down
14 changes: 14 additions & 0 deletions tests/test-pipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ s.listen(s.port, function () {
mydata.emit('data', 'mydata');
mydata.emit('end');

// Test pipeing to a request object with a json body
s.once('/push-json', server.createPostValidator("{\"foo\":\"bar\"}", "application/json"));

var mybodydata = new stream.Stream();
mybodydata.readable = true

counter++
var r2 = request.put({url:'http://localhost:3453/push-json',json:true}, function () {
check();
})
mybodydata.pipe(r2)

mybodydata.emit('data', JSON.stringify({foo:"bar"}));
mybodydata.emit('end');

// Test pipeing from a request object.
s.once('/pull', server.createGetResponse("mypulldata"));
Expand Down

0 comments on commit 4797f88

Please sign in to comment.