Skip to content
Browse files

the 'Content-Length' header at typeof(options.data is string) w/ byte…

… sized 'Content-Length' header(consider multi-byte)

when a use exchanges JSON data by string

added example to README
  • Loading branch information...
1 parent f3340a0 commit 011b84e75eb854a9c5f97b3c81da00692ec4fff1 Yusuke Hata committed Sep 29, 2011
Showing with 53 additions and 2 deletions.
  1. +8 −1 README.md
  2. +5 −0 lib/restler.js
  3. +21 −1 test/restler.js
  4. +19 −0 test/test_helper.js
View
9 README.md
@@ -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
-----------------
@@ -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.
View
5 lib/restler.js
@@ -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;
View
22 test/restler.js
@@ -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');
+ });
+ }
+});
View
19 test/test_helper.js
@@ -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) {

0 comments on commit 011b84e

Please sign in to comment.
Something went wrong with that request. Please try again.