Skip to content

Commit

Permalink
Merge pull request #104 from Lendix:feature/encoding + tests
Browse files Browse the repository at this point in the history
    - Add encoding option to client and serializer (mathieug)
    - Add unit tests
  • Loading branch information
patricklodder committed May 2, 2015
2 parents 41b736f + e5c7640 commit 3a3f23e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ function Client(options, isSecure) {
* - {mixed} value - The value returned in the method response.
*/
Client.prototype.methodCall = function methodCall(method, params, callback) {
var xml = Serializer.serializeMethodCall(method, params)
, transport = this.isSecure ? https : http
, options = this.options
var options = this.options
var xml = Serializer.serializeMethodCall(method, params, options.encoding)
var transport = this.isSecure ? https : http

options.headers['Content-Length'] = Buffer.byteLength(xml, 'utf8')
this.headersProcessors.composeRequest(options.headers)
Expand Down
10 changes: 8 additions & 2 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ var xmlBuilder = require('xmlbuilder')
* otherwise null.
* - {String} xml - The method call XML.
*/
exports.serializeMethodCall = function(method, params) {
exports.serializeMethodCall = function(method, params, encoding) {
var params = params || []

var xml = xmlBuilder.create('methodCall', { version: '1.0' })
var options = { version: '1.0' }

if (encoding) {
options.encoding = encoding
}

var xml = xmlBuilder.create('methodCall', options)
.ele('methodName')
.txt(method)
.up()
Expand Down
10 changes: 10 additions & 0 deletions test/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ vows.describe('Client').addBatch({
assert.equal(topic.port, 9999)
}
}
// Test passing encoding
, 'with an encoding passed': {
topic: function () {
var client = new Client({ url:'http://localhost:9999', encoding: 'utf-8' }, false)
return client.options
}
, 'caches the encoding option' : function (topic) {
assert.strictEqual(topic.encoding, 'utf-8')
}
}
}
//////////////////////////////////////////////////////////////////////
// Test method call functionality
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/good_food/encoded_call.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><methodCall><methodName>testMethod</methodName><params><param><value><string>Foo</string></value></param></params></methodCall>
7 changes: 7 additions & 0 deletions test/serializer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ vows.describe('Serializer').addBatch({
, 'contains the customType': assertXml('good_food/customtype_extended_call.xml')
}
}
, 'utf-8 encoding': {
topic: function () {
var value = "\x46\x6F\x6F"
return Serializer.serializeMethodCall('testMethod', [value], 'utf-8')
}
, 'contains the encoding attribute': assertXml('good_food/encoded_call.xml')
}
}

, 'serializeMethodResponse() called with': {
Expand Down

0 comments on commit 3a3f23e

Please sign in to comment.