Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix #296 - Only set Content-Type if body exists

  • Loading branch information...
commit 4797f88b42c3cf8680cbde09bf473678a5707aed 1 parent e82dd8a
@Marsup authored
View
9 main.js
@@ -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
}
@@ -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
View
6 tests/server.js
@@ -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})
@@ -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()
View
17 tests/test-defaults.js
@@ -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'}));
@@ -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')
View
14 tests/test-pipes.js
@@ -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"));
Please sign in to comment.
Something went wrong with that request. Please try again.