Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcard method call #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions example/client_server.js
Expand Up @@ -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()
})


////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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);
});
Expand Down
39 changes: 23 additions & 16 deletions lib/server.js
Expand Up @@ -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)
}
})
}
Expand Down
32 changes: 32 additions & 0 deletions test/server_test.js
Expand Up @@ -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 = '<?xml version="1.0" encoding="UTF-8"?>'
+ '<methodCall>'
+ '<methodName>testMethod</methodName>'
+ '<params>'
+ '<param>'
+ '<value><string>Param A</string></value>'
+ '</param>'
+ '<param>'
+ '<value><string>Param B</string></value>'
+ '</param>'
+ '</params>'
+ '</methodCall>'
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': {
Expand Down Expand Up @@ -115,4 +146,5 @@ vows.describe('Server').addBatch({
assert.ifError(error)
}
}

}).export(module)