Skip to content

Commit

Permalink
Use Buffer.byteLength() to calculate content length. Fixes #51 - Mult…
Browse files Browse the repository at this point in the history
…i-byte characters handled incorrectly.
  • Loading branch information
scottgonzalez committed Apr 28, 2012
1 parent 7310dfe commit 3f0320d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Client.prototype.methodCall = function methodCall(method, params, callback) {
, transport = this.isSecure ? https : http
, options = this.options

options.headers['Content-Length'] = xml.length
options.headers['Content-Length'] = Buffer.byteLength(xml, 'utf8')

var request = transport.request(options, function(response) {
var deserializer = new Deserializer(options.responseEncoding)
Expand Down
39 changes: 39 additions & 0 deletions test/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,44 @@ vows.describe('Client').addBatch({
assert.deepEqual(value, 'äè12')
}
}
, 'with a multi-byte character in request' : {
topic: function () {
var that = this
, requestBody = ""
http.createServer(function (request, response) {
request.setEncoding('utf8')
request.on('data', function (chunk) {
requestBody += chunk
})
request.on('end', function () {
response.writeHead(200, {'Content-Type': 'text/xml'})
var data = '<?xml version="2.0" encoding="UTF-8"?>'
+ '<methodResponse>'
+ '<params>'
+ '<param><value><string>ok</string></value></param>'
+ '</params>'
+ '</methodResponse>'
response.write(data)
response.end()
})
}).listen(9095, 'localhost')
// Waits briefly to give the server time to start up and start listening
setTimeout(function () {
var client = new Client({ host: 'localhost', port: 9095, path: '/'}, false)
client.methodCall('multiByte', ['ö'], function (error) {
that.callback(error, requestBody)
})
}, 500)
}
, 'contains full request' : function (error, value) {
var data = '<?xml version="1.0"?>' +
'<methodCall>' +
'<methodName>multiByte</methodName>' +
'<params><param><value><string>ö</string></value></param></params>' +
'</methodCall>'
assert.isNull(error)
assert.deepEqual(value, data)
}
}
}
}).export(module)

0 comments on commit 3f0320d

Please sign in to comment.