diff options
Diffstat (limited to 'src/node/test')
-rw-r--r-- | src/node/test/common_test.js | 107 | ||||
-rw-r--r-- | src/node/test/credentials_test.js | 2 | ||||
-rw-r--r-- | src/node/test/surface_test.js | 115 | ||||
-rw-r--r-- | src/node/test/test_messages.proto | 17 |
4 files changed, 179 insertions, 62 deletions
diff --git a/src/node/test/common_test.js b/src/node/test/common_test.js index c57b7388f6..39ff6a5f1f 100644 --- a/src/node/test/common_test.js +++ b/src/node/test/common_test.js @@ -34,17 +34,26 @@ 'use strict'; var assert = require('assert'); +var _ = require('lodash'); -var common = require('../src/common.js'); +var common = require('../src/common'); +var protobuf_js_6_common = require('../src/protobuf_js_6_common'); + +var serializeCls = protobuf_js_6_common.serializeCls; +var deserializeCls = protobuf_js_6_common.deserializeCls; var ProtoBuf = require('protobufjs'); -var messages_proto = ProtoBuf.loadProtoFile( - __dirname + '/test_messages.proto').build(); +var messages_proto = new ProtoBuf.Root(); +messages_proto = messages_proto.loadSync( + __dirname + '/test_messages.proto', {keepCase: true}).resolveAll(); + +var default_options = common.defaultGrpcOptions; describe('Proto message long int serialize and deserialize', function() { - var longSerialize = common.serializeCls(messages_proto.LongValues); - var longDeserialize = common.deserializeCls(messages_proto.LongValues); + var longSerialize = serializeCls(messages_proto.LongValues); + var longDeserialize = deserializeCls(messages_proto.LongValues, + default_options); var pos_value = '314159265358979'; var neg_value = '-27182818284590'; it('should preserve positive int64 values', function() { @@ -88,8 +97,9 @@ describe('Proto message long int serialize and deserialize', function() { neg_value); }); it('should deserialize as a number with the right option set', function() { - var longNumDeserialize = common.deserializeCls(messages_proto.LongValues, - false, false); + var num_options = _.defaults({longsAsStrings: false}, default_options); + var longNumDeserialize = deserializeCls(messages_proto.LongValues, + num_options); var serialized = longSerialize({int_64: pos_value}); assert.strictEqual(typeof longDeserialize(serialized).int_64, 'string'); /* With the longsAsStrings option disabled, long values are represented as @@ -98,11 +108,12 @@ describe('Proto message long int serialize and deserialize', function() { }); }); describe('Proto message bytes serialize and deserialize', function() { - var sequenceSerialize = common.serializeCls(messages_proto.SequenceValues); - var sequenceDeserialize = common.deserializeCls( - messages_proto.SequenceValues); - var sequenceBase64Deserialize = common.deserializeCls( - messages_proto.SequenceValues, true); + var sequenceSerialize = serializeCls(messages_proto.SequenceValues); + var sequenceDeserialize = deserializeCls( + messages_proto.SequenceValues, default_options); + var b64_options = _.defaults({binaryAsBase64: true}, default_options); + var sequenceBase64Deserialize = deserializeCls( + messages_proto.SequenceValues, b64_options); var buffer_val = new Buffer([0x69, 0xb7]); var base64_val = 'abc='; it('should preserve a buffer', function() { @@ -120,19 +131,73 @@ describe('Proto message bytes serialize and deserialize', function() { var deserialized = sequenceBase64Deserialize(serialized); assert.strictEqual(deserialized.bytes_field, base64_val); }); - /* The next two tests are specific tests to verify that issue - * https://github.com/grpc/grpc/issues/5174 has been fixed. They are skipped - * because they will not pass until a protobuf.js release has been published - * with a fix for https://github.com/dcodeIO/protobuf.js/issues/390 */ - it.skip('should serialize a repeated field as packed by default', function() { - var expected_serialize = new Buffer([0x12, 0x01, 0x01, 0x0a]); + it('should serialize a repeated field as packed by default', function() { + var expected_serialize = new Buffer([0x12, 0x01, 0x0a]); var serialized = sequenceSerialize({repeated_field: [10]}); assert.strictEqual(expected_serialize.compare(serialized), 0); }); - it.skip('should deserialize packed or unpacked repeated', function() { - var serialized = new Buffer([0x12, 0x01, 0x01, 0x0a]); + it('should deserialize packed or unpacked repeated', function() { + var expectedDeserialize = { + bytes_field: new Buffer(''), + repeated_field: [10] + }; + var packedSerialized = new Buffer([0x12, 0x01, 0x0a]); + var unpackedSerialized = new Buffer([0x10, 0x0a]); + var packedDeserialized; + var unpackedDeserialized; assert.doesNotThrow(function() { - sequenceDeserialize(serialized); + packedDeserialized = sequenceDeserialize(packedSerialized); }); + assert.doesNotThrow(function() { + unpackedDeserialized = sequenceDeserialize(unpackedSerialized); + }); + assert.deepEqual(packedDeserialized, expectedDeserialize); + assert.deepEqual(unpackedDeserialized, expectedDeserialize); + }); +}); +describe('Proto message oneof serialize and deserialize', function() { + var oneofSerialize = serializeCls(messages_proto.OneOfValues); + var oneofDeserialize = deserializeCls( + messages_proto.OneOfValues, default_options); + it('Should have idempotent round trips', function() { + var test_message = {oneof_choice: 'int_choice', int_choice: 5}; + var serialized1 = oneofSerialize(test_message); + var deserialized1 = oneofDeserialize(serialized1); + assert.equal(deserialized1.int_choice, 5); + var serialized2 = oneofSerialize(deserialized1); + var deserialized2 = oneofDeserialize(serialized2); + assert.deepEqual(deserialized1, deserialized2); + }); + it('Should emit a property indicating which field was chosen', function() { + var test_message1 = {oneof_choice: 'int_choice', int_choice: 5}; + var serialized1 = oneofSerialize(test_message1); + var deserialized1 = oneofDeserialize(serialized1); + assert.equal(deserialized1.oneof_choice, 'int_choice'); + var test_message2 = {oneof_choice: 'string_choice', string_choice: 'abc'}; + var serialized2 = oneofSerialize(test_message2); + var deserialized2 = oneofDeserialize(serialized2); + assert.equal(deserialized2.oneof_choice, 'int_choice'); + }); +}); +describe('Proto message enum serialize and deserialize', function() { + var enumSerialize = serializeCls(messages_proto.EnumValues); + var enumDeserialize = deserializeCls( + messages_proto.EnumValues, default_options); + var enumIntOptions = _.defaults({enumsAsStrings: false}, default_options); + var enumIntDeserialize = deserializeCls( + messages_proto.EnumValues, enumIntOptions); + it('Should accept both names and numbers', function() { + var nameSerialized = enumSerialize({enum_value: 'ONE'}); + var numberSerialized = enumSerialize({enum_value: 1}); + assert.strictEqual(messages_proto.TestEnum.ONE, 1); + assert.deepEqual(enumDeserialize(nameSerialized), + enumDeserialize(numberSerialized)); + }); + it('Should deserialize as a string the enumsAsStrings option', function() { + var serialized = enumSerialize({enum_value: 'TWO'}); + var nameDeserialized = enumDeserialize(serialized); + var numberDeserialized = enumIntDeserialize(serialized); + assert.deepEqual(nameDeserialized, {enum_value: 'TWO'}); + assert.deepEqual(numberDeserialized, {enum_value: 2}); }); }); diff --git a/src/node/test/credentials_test.js b/src/node/test/credentials_test.js index 305843f665..b66b4bf5ea 100644 --- a/src/node/test/credentials_test.js +++ b/src/node/test/credentials_test.js @@ -228,7 +228,7 @@ describe('client credentials', function() { before(function() { var proto = grpc.load(__dirname + '/test_service.proto'); server = new grpc.Server(); - server.addProtoService(proto.TestService.service, { + server.addService(proto.TestService.service, { unary: function(call, cb) { call.sendMetadata(call.metadata); cb(null, {}); diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js index 1d739562a6..783028fa99 100644 --- a/src/node/test/surface_test.js +++ b/src/node/test/surface_test.js @@ -34,19 +34,22 @@ 'use strict'; var assert = require('assert'); +var _ = require('lodash'); var surface_client = require('../src/client.js'); +var common = require('../src/common'); var ProtoBuf = require('protobufjs'); var grpc = require('..'); -var math_proto = ProtoBuf.loadProtoFile(__dirname + - '/../../proto/math/math.proto'); +var math_proto = new ProtoBuf.Root(); +math_proto = math_proto.loadSync(__dirname + + '/../../proto/math/math.proto', {keepCase: true}); var mathService = math_proto.lookup('math.Math'); - -var _ = require('lodash'); +var mathServiceAttrs = grpc.loadObject( + mathService, common.defaultGrpcOptions).service; /** * This is used for testing functions with multiple asynchronous calls that @@ -87,11 +90,6 @@ describe('File loader', function() { grpc.load(__dirname + '/test_service.json', 'json'); }); }); - it('Should fail to load a file with an unknown format', function() { - assert.throws(function() { - grpc.load(__dirname + '/test_service.proto', 'fake_format'); - }); - }); }); describe('surface Server', function() { var server; @@ -132,15 +130,40 @@ describe('Server.prototype.addProtoService', function() { afterEach(function() { server.forceShutdown(); }); - it('Should succeed with a single service', function() { + it('Should succeed with a single proto service', function() { assert.doesNotThrow(function() { server.addProtoService(mathService, dummyImpls); }); }); + it('Should succeed with a single service attributes object', function() { + assert.doesNotThrow(function() { + server.addProtoService(mathServiceAttrs, dummyImpls); + }); + }); +}); +describe('Server.prototype.addService', function() { + var server; + var dummyImpls = { + 'div': function() {}, + 'divMany': function() {}, + 'fib': function() {}, + 'sum': function() {} + }; + beforeEach(function() { + server = new grpc.Server(); + }); + afterEach(function() { + server.forceShutdown(); + }); + it('Should succeed with a single service', function() { + assert.doesNotThrow(function() { + server.addService(mathServiceAttrs, dummyImpls); + }); + }); it('Should fail with conflicting method names', function() { - server.addProtoService(mathService, dummyImpls); + server.addService(mathServiceAttrs, dummyImpls); assert.throws(function() { - server.addProtoService(mathService, dummyImpls); + server.addService(mathServiceAttrs, dummyImpls); }); }); it('Should allow method names as originally written', function() { @@ -172,15 +195,15 @@ describe('Server.prototype.addProtoService', function() { it('Should fail if the server has been started', function() { server.start(); assert.throws(function() { - server.addProtoService(mathService, dummyImpls); + server.addService(mathServiceAttrs, dummyImpls); }); }); describe('Default handlers', function() { var client; beforeEach(function() { - server.addProtoService(mathService, {}); + server.addService(mathServiceAttrs, {}); var port = server.bind('localhost:0', server_insecure_creds); - var Client = surface_client.makeProtobufClientConstructor(mathService); + var Client = grpc.loadObject(mathService); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); @@ -252,7 +275,7 @@ describe('waitForClientReady', function() { server = new grpc.Server(); port = server.bind('localhost:0', grpc.ServerCredentials.createInsecure()); server.start(); - Client = surface_client.makeProtobufClientConstructor(mathService); + Client = grpc.loadObject(mathService); }); beforeEach(function() { client = new Client('localhost:' + port, grpc.credentials.createInsecure()); @@ -309,16 +332,18 @@ describe('Echo service', function() { var server; var client; before(function() { - var test_proto = ProtoBuf.loadProtoFile(__dirname + '/echo_service.proto'); + var test_proto = new ProtoBuf.Root(); + test_proto = test_proto.loadSync(__dirname + '/echo_service.proto', + {keepCase: true}); var echo_service = test_proto.lookup('EchoService'); + var Client = grpc.loadObject(echo_service); server = new grpc.Server(); - server.addProtoService(echo_service, { + server.addService(Client.service, { echo: function(call, callback) { callback(null, call.request); } }); var port = server.bind('localhost:0', server_insecure_creds); - var Client = surface_client.makeProtobufClientConstructor(echo_service); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); }); @@ -432,10 +457,13 @@ describe('Echo metadata', function() { var server; var metadata; before(function() { - var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); + var test_proto = new ProtoBuf.Root(); + test_proto = test_proto.loadSync(__dirname + '/test_service.proto', + {keepCase: true}); var test_service = test_proto.lookup('TestService'); + var Client = grpc.loadObject(test_service); server = new grpc.Server(); - server.addProtoService(test_service, { + server.addService(Client.service, { unary: function(call, cb) { call.sendMetadata(call.metadata); cb(null, {}); @@ -460,7 +488,6 @@ describe('Echo metadata', function() { } }); var port = server.bind('localhost:0', server_insecure_creds); - var Client = surface_client.makeProtobufClientConstructor(test_service); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); metadata = new grpc.Metadata(); @@ -533,7 +560,9 @@ describe('Client malformed response handling', function() { var client; var badArg = new Buffer([0xFF]); before(function() { - var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); + var test_proto = new ProtoBuf.Root(); + test_proto = test_proto.loadSync(__dirname + '/test_service.proto', + {keepCase: true}); var test_service = test_proto.lookup('TestService'); var malformed_test_service = { unary: { @@ -591,7 +620,7 @@ describe('Client malformed response handling', function() { } }); var port = server.bind('localhost:0', server_insecure_creds); - var Client = surface_client.makeProtobufClientConstructor(test_service); + var Client = grpc.loadObject(test_service); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); }); @@ -640,7 +669,9 @@ describe('Server serialization failure handling', function() { var client; var server; before(function() { - var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); + var test_proto = new ProtoBuf.Root(); + test_proto = test_proto.loadSync(__dirname + '/test_service.proto', + {keepCase: true}); var test_service = test_proto.lookup('TestService'); var malformed_test_service = { unary: { @@ -698,7 +729,7 @@ describe('Server serialization failure handling', function() { } }); var port = server.bind('localhost:0', server_insecure_creds); - var Client = surface_client.makeProtobufClientConstructor(test_service); + var Client = grpc.loadObject(test_service); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); }); @@ -747,12 +778,15 @@ describe('Other conditions', function() { var server; var port; before(function() { - var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); + var test_proto = new ProtoBuf.Root(); + test_proto = test_proto.loadSync(__dirname + '/test_service.proto', + {keepCase: true}); test_service = test_proto.lookup('TestService'); + Client = grpc.loadObject(test_service); server = new grpc.Server(); var trailer_metadata = new grpc.Metadata(); trailer_metadata.add('trailer-present', 'yes'); - server.addProtoService(test_service, { + server.addService(Client.service, { unary: function(call, cb) { var req = call.request; if (req.error) { @@ -812,7 +846,6 @@ describe('Other conditions', function() { } }); port = server.bind('localhost:0', server_insecure_creds); - Client = surface_client.makeProtobufClientConstructor(test_service); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); }); @@ -1093,17 +1126,19 @@ describe('Call propagation', function() { var client; var server; before(function() { - var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); + var test_proto = new ProtoBuf.Root(); + test_proto = test_proto.loadSync(__dirname + '/test_service.proto', + {keepCase: true}); test_service = test_proto.lookup('TestService'); server = new grpc.Server(); - server.addProtoService(test_service, { + Client = grpc.loadObject(test_service); + server.addService(Client.service, { unary: function(call) {}, clientStream: function(stream) {}, serverStream: function(stream) {}, bidiStream: function(stream) {} }); var port = server.bind('localhost:0', server_insecure_creds); - Client = surface_client.makeProtobufClientConstructor(test_service); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); }); @@ -1138,7 +1173,7 @@ describe('Call propagation', function() { }); call.cancel(); }; - proxy.addProtoService(test_service, proxy_impl); + proxy.addService(Client.service, proxy_impl); var proxy_port = proxy.bind('localhost:0', server_insecure_creds); proxy.start(); var proxy_client = new Client('localhost:' + proxy_port, @@ -1160,7 +1195,7 @@ describe('Call propagation', function() { }); call.cancel(); }; - proxy.addProtoService(test_service, proxy_impl); + proxy.addService(Client.service, proxy_impl); var proxy_port = proxy.bind('localhost:0', server_insecure_creds); proxy.start(); var proxy_client = new Client('localhost:' + proxy_port, @@ -1180,7 +1215,7 @@ describe('Call propagation', function() { }); call.cancel(); }; - proxy.addProtoService(test_service, proxy_impl); + proxy.addService(Client.service, proxy_impl); var proxy_port = proxy.bind('localhost:0', server_insecure_creds); proxy.start(); var proxy_client = new Client('localhost:' + proxy_port, @@ -1204,7 +1239,7 @@ describe('Call propagation', function() { }); call.cancel(); }; - proxy.addProtoService(test_service, proxy_impl); + proxy.addService(Client.service, proxy_impl); var proxy_port = proxy.bind('localhost:0', server_insecure_creds); proxy.start(); var proxy_client = new Client('localhost:' + proxy_port, @@ -1235,7 +1270,7 @@ describe('Call propagation', function() { } }); }; - proxy.addProtoService(test_service, proxy_impl); + proxy.addService(Client.service, proxy_impl); var proxy_port = proxy.bind('localhost:0', server_insecure_creds); proxy.start(); var proxy_client = new Client('localhost:' + proxy_port, @@ -1259,7 +1294,7 @@ describe('Call propagation', function() { done(); }); }; - proxy.addProtoService(test_service, proxy_impl); + proxy.addService(Client.service, proxy_impl); var proxy_port = proxy.bind('localhost:0', server_insecure_creds); proxy.start(); var proxy_client = new Client('localhost:' + proxy_port, @@ -1279,14 +1314,14 @@ describe('Cancelling surface client', function() { var server; before(function() { server = new grpc.Server(); - server.addProtoService(mathService, { + server.addService(mathServiceAttrs, { 'div': function(stream) {}, 'divMany': function(stream) {}, 'fib': function(stream) {}, 'sum': function(stream) {} }); var port = server.bind('localhost:0', server_insecure_creds); - var Client = surface_client.makeProtobufClientConstructor(mathService); + var Client = surface_client.makeClientConstructor(mathServiceAttrs); client = new Client('localhost:' + port, grpc.credentials.createInsecure()); server.start(); }); diff --git a/src/node/test/test_messages.proto b/src/node/test/test_messages.proto index a1a6a32833..ae70f6e152 100644 --- a/src/node/test/test_messages.proto +++ b/src/node/test/test_messages.proto @@ -41,3 +41,20 @@ message SequenceValues { bytes bytes_field = 1; repeated int32 repeated_field = 2; } + +message OneOfValues { + oneof oneof_choice { + int32 int_choice = 1; + string string_choice = 2; + } +} + +enum TestEnum { + ZERO = 0; + ONE = 1; + TWO = 2; +} + +message EnumValues { + TestEnum enum_value = 1; +}
\ No newline at end of file |