From d0a2dfddd6afc1c975b01dfe3da41a26e821b6c4 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 31 Mar 2016 09:21:43 +0000 Subject: [PATCH 1/2] when strange unicode characters are in the string then content length is not correct --- node_modules/c9/rest_client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node_modules/c9/rest_client.js b/node_modules/c9/rest_client.js index 8631edf5..0aec2fff 100644 --- a/node_modules/c9/rest_client.js +++ b/node_modules/c9/rest_client.js @@ -36,7 +36,7 @@ function RestClient(host, port, config) { var headers = _.extend({ "Accept": "application/json", "Content-Type": "application/json", - "Content-Length": payload.length + "Content-Length": Buffer.byteLength(payload) }, config.headers || {}); var options = { From 8fa3f769c5c192bc7bed7be929a87da46e51c30f Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Thu, 31 Mar 2016 09:22:39 +0000 Subject: [PATCH 2/2] add unit test --- node_modules/c9/rest_client_test.js | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 node_modules/c9/rest_client_test.js diff --git a/node_modules/c9/rest_client_test.js b/node_modules/c9/rest_client_test.js new file mode 100644 index 00000000..a0f07779 --- /dev/null +++ b/node_modules/c9/rest_client_test.js @@ -0,0 +1,55 @@ +#!/usr/bin/env node + +/*global describe it before after beforeEach afterEach */ +"use strict"; + +"use server"; +"use mocha"; + +require("c9/inline-mocha")(module); +require("amd-loader"); + +var assert = require("assert"); +var http = require("http"); +var findFreePort = require("netutil").findFreePort; +var RestClient = require("./rest_client"); + +describe(__filename, function() { + + var port; + var MIN_API_PORT = 18500; + var MAX_API_PORT = MIN_API_PORT + 1000; + + beforeEach(function(next) { + findFreePort(MIN_API_PORT, MAX_API_PORT, 'localhost', function(err, _port) { + port = _port; + next(err); + }); + }); + + it("should send correct content length", function(next) { + var server = http.createServer(function(req, res) { + var body = ""; + req.on("data", function(d) { + body += d; + }); + + req.on("end", function() { + JSON.parse(body); + res.end("OK"); + }); + + }); + server.listen(port, function() { + var client = new RestClient("localhost", port, {}); + + // send body with "strange" unicode character + var body = {"cloneFromScm":"https://github.com/saasbook/ruby­-calisthenics"}; + client.request("POST", "/", body, function(err, res) { + assert(!err, err); + assert.equal(res, "OK"); + server.close(next); + }); + }); + }); +}); \ No newline at end of file