Skip to content

Commit

Permalink
the 'Content-Length' header at typeof(options.data is string) w/ byte…
Browse files Browse the repository at this point in the history
… sized 'Content-Length' header(consider multi-byte)

when a use exchanges JSON data by string

added example to README
  • Loading branch information
nowelium committed Sep 29, 2011
1 parent f3340a0 commit 011b84e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ Example usage
sys.p(data);
});

// the JSON post
rest.post('http://example.com/action', {
data: JSON.stringify({ id: 334 }),
}).on('complete', function(data, response) {
// you can get at the raw response like this...
});


Running the tests
-----------------
Expand All @@ -144,4 +151,4 @@ Running the tests
TODO
----
* Deal with no utf-8 response bodies
* What do you need? Let me know or fork.
* What do you need? Let me know or fork.
5 changes: 5 additions & 0 deletions lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ function Request(uri, options) {
this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
this.headers['Content-Length'] = this.options.data.length;
}
if(typeof this.options.data == 'string') {
var buffer = new Buffer(this.options.data, this.options.encoding || 'utf8');
this.options.data = buffer;
this.headers['Content-Length'] = buffer.length;
}
}

var proto = (this.url.protocol == 'https:') ? https : http;
Expand Down
22 changes: 21 additions & 1 deletion test/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,24 @@ helper.testCase('Redirect Tests', helper.redirectServer, {
test.equal('Hell Yeah!', data, "returned " + sys.inspect(data));
});
}
});
});

helper.testCase('Content-Length Tests', helper.contentLengthServer, {
testRequestHeaderIncludesContentLengthWithJSONData: function(host, test){
var jsonString = JSON.stringify({ greeting: 'hello world' });
rest.post(host, {
data: jsonString
}).on('complete', function(data){
test.equal(26, data, 'should set content-length');
});
},
testJSONMultibyteContentLength: function (host, test){
var multibyteData = JSON.stringify({ greeting: 'こんにちは世界' });
rest.post(host, {
data: multibyteData
}).on('complete', function(data) {
test.notEqual(22, data, 'should unicode string length');
test.equal(36, data, 'should byte-size content-length');
});
}
});
19 changes: 19 additions & 0 deletions test/test_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ exports.redirectServer = function() {
return ["http://localhost:" + port, server];
}

exports.contentLengthServer = function (){
var port = exports.port++;

var server = http.createServer(function(request, response){
response.writeHead(200, { 'Content-Type': 'text/plain' });
if('content-length' in request.headers){
response.write(request.headers['content-length']);
} else {
response.write('content-length isnot set');
}

response.end();
server.close();
});

server.listen(port, 'localhost');
return ['http://localhost:' + port, server];
};

exports.port = 7000;

exports.testCase = function(caseName, serverFunc, tests) {
Expand Down

0 comments on commit 011b84e

Please sign in to comment.