From 88aeb940b78287e3c20b9b2f6a059456a5a2eaa5 Mon Sep 17 00:00:00 2001 From: "Michele \"comick\" Comignano" Date: Sat, 22 Sep 2012 13:26:36 +0200 Subject: [PATCH] Add wildcard method handling with improved example and test --- example/client_server.js | 13 +++++++++++++ lib/server.js | 39 +++++++++++++++++++++++---------------- test/server_test.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/example/client_server.js b/example/client_server.js index cd85022..d680c34 100644 --- a/example/client_server.js +++ b/example/client_server.js @@ -134,6 +134,15 @@ server.on('fakeFault', function (error, params, callback) { serverContents.calls.push('fakeFault') callback({ faultCode: 2, faultString: 'Uh oh.'}, null) }) +// Handle wildcard message +server.on('*', function (error, params, foundCallback, notFoundCallback) { + if (error == 'wildcardManagedMethod') { + serverContents.calls.push(error) + foundCallback(null, serverContents.calls) + return + } + notFoundCallback() +}) //////////////////////////////////////////////////////////////////////// @@ -215,6 +224,10 @@ setTimeout(function () { console.log('Get Call Log Response: ' + value) }) + client.methodCall('wildcardManagedMethod', null, function (error, value) { + console.log('Get Call Log Response via wildcardManagedMethod: '+value); + }); + client.methodCall('notFound', null, function (error, value) { console.log('notFound: '+error); }); diff --git a/lib/server.js b/lib/server.js index 87a36de..87d3832 100644 --- a/lib/server.js +++ b/lib/server.js @@ -38,23 +38,30 @@ function Server(options, isSecure, onListening) { function handleMethodCall(request, response) { var deserializer = new Deserializer() deserializer.deserializeMethodCall(request, function(error, methodName, params) { - if (that._events.hasOwnProperty(methodName)) { - that.emit(methodName, null, params, function(error, value) { - var xml = null - if (error !== null) { - xml = Serializer.serializeFault(error) - } - else { - xml = Serializer.serializeMethodResponse(value) - } - response.writeHead(200, {'Content-Type': 'text/xml'}) - response.end(xml) - }) + + function handleMethodFound(error, value) { + var xml = null + if (error !== null) { + xml = Serializer.serializeFault(error) + } + else { + xml = Serializer.serializeMethodResponse(value) + } + response.writeHead(200, {'Content-Type': 'text/xml'}) + response.end(xml) + } + + function handleMethodNotFound() { + response.writeHead(404) + response.end() } - else { - that.emit('NotFound', methodName, params); - response.writeHead(404); - response.end(); + + if (that._events.hasOwnProperty(methodName)) { + that.emit(methodName, null, params, handleMethodFound) + } else if (that._events.hasOwnProperty('*')) { + that.emit('*', methodName, params, handleMethodFound, handleMethodNotFound) + } else { + that.emit('NotFound', methodName, params, handleMethodNotFound) } }) } diff --git a/test/server_test.js b/test/server_test.js index e594152..489e26e 100644 --- a/test/server_test.js +++ b/test/server_test.js @@ -82,6 +82,37 @@ vows.describe('Server').addBatch({ assert.deepEqual(value, ['Param A', 'Param B']) } } + , 'with wildcard server': { + topic: function() { + var server = new Server({ port: 9994, path: '/'}, false) + + server.on('*', this.callback); + setTimeout(function () { + var options = { host: 'localhost', port: 9994, path: '/', + method: 'POST' } + var req = http.request(options, function() {}) + var chunk1 = '' + + '' + + 'testMethod' + + '' + + '' + + 'Param A' + + '' + + '' + + 'Param B' + + '' + + '' + + '' + req.on('error', function(e) { assert.isNull(e); }) + req.write(chunk1) + req.end() + }, 500) + } + , 'known method' : function (method, params) { + assert.equal(method, 'testMethod') + assert.deepEqual(params, ['Param A', 'Param B']) + } + } } , 'Another call' :{ 'with an unknown method': { @@ -115,4 +146,5 @@ vows.describe('Server').addBatch({ assert.ifError(error) } } + }).export(module)