diff options
author | murgatroid99 <michael.lumish@gmail.com> | 2015-01-29 11:44:46 -0800 |
---|---|---|
committer | murgatroid99 <michael.lumish@gmail.com> | 2015-01-29 11:44:46 -0800 |
commit | 4d2d0f0f3acd1e90037524803e7cb6b445f9c676 (patch) | |
tree | de4e3c34d47c19def424f1fd8a81a781a4877268 | |
parent | 6373ba01d4f5e9a800b10b5670ea3da60aa30b5a (diff) |
Added API for servers to provide metadata
-rw-r--r-- | src/node/interop/interop_server.js | 2 | ||||
-rw-r--r-- | src/node/src/server.js | 10 | ||||
-rw-r--r-- | src/node/src/surface_server.js | 9 | ||||
-rw-r--r-- | src/node/test/client_server_test.js | 28 | ||||
-rw-r--r-- | src/node/test/surface_test.js | 2 |
5 files changed, 37 insertions, 14 deletions
diff --git a/src/node/interop/interop_server.js b/src/node/interop/interop_server.js index ebf847876c..54e9715d1e 100644 --- a/src/node/interop/interop_server.js +++ b/src/node/interop/interop_server.js @@ -183,7 +183,7 @@ function getServer(port, tls) { fullDuplexCall: handleFullDuplex, halfDuplexCall: handleHalfDuplex } - }, options); + }, null, options); var port_num = server.bind('0.0.0.0:' + port, tls); return {server: server, port: port_num}; } diff --git a/src/node/src/server.js b/src/node/src/server.js index 03cdbe6f98..a5d737c68d 100644 --- a/src/node/src/server.js +++ b/src/node/src/server.js @@ -202,10 +202,13 @@ GrpcServerStream.prototype._write = function(chunk, encoding, callback) { * Constructs a server object that stores request handlers and delegates * incoming requests to those handlers * @constructor - * @param {Array} options Options that should be passed to the internal server + * @param {function(string, Object<string, Array<Buffer>>): + Object<string, Array<Buffer|string>>=} getMetadata Callback that gets + * metatada for a given method + * @param {Object=} options Options that should be passed to the internal server * implementation */ -function Server(options) { +function Server(getMetadata, options) { this.handlers = {}; var handlers = this.handlers; var server = new grpc.Server(options); @@ -249,6 +252,9 @@ function Server(options) { stream.emit('cancelled'); } }, 0); + if (getMetadata) { + call.addMetadata(getMetadata(data.method, data.metadata)); + } call.serverEndInitialMetadata(0); var stream = new GrpcServerStream(call, handler.serialize, handler.deserialize); diff --git a/src/node/src/surface_server.js b/src/node/src/surface_server.js index cf342fc9fd..a47d1fa23d 100644 --- a/src/node/src/surface_server.js +++ b/src/node/src/surface_server.js @@ -256,10 +256,13 @@ function makeServerConstructor(services) { * @constructor * @param {Object} service_handlers Map from service names to map from method * names to handlers - * @param {Object} options Options to pass to the underlying server + * @param {function(string, Object<string, Array<Buffer>>): + Object<string, Array<Buffer|string>>=} getMetadata Callback that + * gets metatada for a given method + * @param {Object=} options Options to pass to the underlying server */ - function SurfaceServer(service_handlers, options) { - var server = new Server(options); + function SurfaceServer(service_handlers, getMetadata, options) { + var server = new Server(getMetadata, options); this.inner_server = server; _.each(services, function(service) { var service_name = common.fullyQualifiedName(service); diff --git a/src/node/test/client_server_test.js b/src/node/test/client_server_test.js index 9e1b2a79c8..059dd1323a 100644 --- a/src/node/test/client_server_test.js +++ b/src/node/test/client_server_test.js @@ -84,6 +84,10 @@ function cancelHandler(stream) { // do nothing } +function metadataHandler(stream, metadata) { + stream.end(); +} + /** * Serialize a string to a Buffer * @param {string} value The string to serialize @@ -106,11 +110,14 @@ describe('echo client', function() { var server; var channel; before(function() { - server = new Server(); + server = new Server(function getMetadata(method, metadata) { + return {method: [method]}; + }); var port_num = server.bind('0.0.0.0:0'); server.register('echo', echoHandler); server.register('error', errorHandler); server.register('cancellation', cancelHandler); + server.register('metadata', metadataHandler); server.start(); channel = new grpc.Channel('localhost:' + port_num); @@ -142,12 +149,19 @@ describe('echo client', function() { done(); }); }); + it('should recieve metadata set by the server', function(done) { + var stream = client.makeRequest(channel, 'metadata'); + stream.on('metadata', function(metadata) { + assert.strictEqual(metadata.method[0].toString(), 'metadata'); + }); + stream.on('status', function(status) { + assert.equal(status.code, client.status.OK); + done(); + }); + stream.end(); + }); it('should get an error status that the server throws', function(done) { - var stream = client.makeRequest( - channel, - 'error', - null, - getDeadline(1)); + var stream = client.makeRequest(channel, 'error'); stream.on('data', function() {}); stream.write(new Buffer('test')); @@ -189,7 +203,7 @@ describe('secure echo client', function() { key_data, pem_data); - server = new Server({'credentials' : server_creds}); + server = new Server(null, {'credentials' : server_creds}); var port_num = server.bind('0.0.0.0:0', true); server.register('echo', echoHandler); server.start(); diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js index 85f4841d4b..1038f9ab33 100644 --- a/src/node/test/surface_test.js +++ b/src/node/test/surface_test.js @@ -75,7 +75,7 @@ describe('Surface server constructor', function() { }, /math.Math/); }); }); -describe('Surface client', function() { +describe('Cancelling surface client', function() { var client; var server; before(function() { |