murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 34 | 'use strict'; |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 35 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 36 | var assert = require('assert'); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 37 | |
murgatroid99 | 10ac96c | 2015-02-12 13:28:25 -0800 | [diff] [blame] | 38 | var surface_client = require('../src/client.js'); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 39 | |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 40 | var ProtoBuf = require('protobufjs'); |
| 41 | |
| 42 | var grpc = require('..'); |
| 43 | |
| 44 | var math_proto = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto'); |
| 45 | |
| 46 | var mathService = math_proto.lookup('math.Math'); |
| 47 | |
murgatroid99 | 55739d5 | 2015-06-03 10:58:21 -0700 | [diff] [blame] | 48 | var _ = require('lodash'); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 49 | |
murgatroid99 | 71dbb86 | 2015-04-20 11:22:51 -0700 | [diff] [blame] | 50 | describe('File loader', function() { |
| 51 | it('Should load a proto file by default', function() { |
| 52 | assert.doesNotThrow(function() { |
| 53 | grpc.load(__dirname + '/test_service.proto'); |
| 54 | }); |
| 55 | }); |
| 56 | it('Should load a proto file with the proto format', function() { |
| 57 | assert.doesNotThrow(function() { |
| 58 | grpc.load(__dirname + '/test_service.proto', 'proto'); |
| 59 | }); |
| 60 | }); |
| 61 | it('Should load a json file with the json format', function() { |
| 62 | assert.doesNotThrow(function() { |
| 63 | grpc.load(__dirname + '/test_service.json', 'json'); |
| 64 | }); |
| 65 | }); |
| 66 | it('Should fail to load a file with an unknown format', function() { |
| 67 | assert.throws(function() { |
| 68 | grpc.load(__dirname + '/test_service.proto', 'fake_format'); |
| 69 | }); |
| 70 | }); |
| 71 | }); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 72 | describe('Server.prototype.addProtoService', function() { |
| 73 | var server; |
| 74 | var dummyImpls = { |
| 75 | 'div': function() {}, |
| 76 | 'divMany': function() {}, |
| 77 | 'fib': function() {}, |
| 78 | 'sum': function() {} |
| 79 | }; |
| 80 | beforeEach(function() { |
| 81 | server = new grpc.Server(); |
| 82 | }); |
| 83 | afterEach(function() { |
| 84 | server.shutdown(); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 85 | }); |
| 86 | it('Should succeed with a single service', function() { |
| 87 | assert.doesNotThrow(function() { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 88 | server.addProtoService(mathService, dummyImpls); |
| 89 | }); |
| 90 | }); |
| 91 | it('Should fail with conflicting method names', function() { |
| 92 | server.addProtoService(mathService, dummyImpls); |
| 93 | assert.throws(function() { |
| 94 | server.addProtoService(mathService, dummyImpls); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 95 | }); |
| 96 | }); |
| 97 | it('Should fail with missing handlers', function() { |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 98 | assert.throws(function() { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 99 | server.addProtoService(mathService, { |
| 100 | 'div': function() {}, |
| 101 | 'divMany': function() {}, |
| 102 | 'fib': function() {} |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 103 | }); |
| 104 | }, /math.Math.Sum/); |
| 105 | }); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 106 | it('Should fail if the server has been started', function() { |
| 107 | server.start(); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 108 | assert.throws(function() { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 109 | server.addProtoService(mathService, dummyImpls); |
| 110 | }); |
murgatroid99 | cca5ffa | 2015-01-15 14:06:56 -0800 | [diff] [blame] | 111 | }); |
| 112 | }); |
murgatroid99 | 5828fb9 | 2015-05-11 14:27:39 -0700 | [diff] [blame] | 113 | describe('Echo service', function() { |
| 114 | var server; |
| 115 | var client; |
| 116 | before(function() { |
| 117 | var test_proto = ProtoBuf.loadProtoFile(__dirname + '/echo_service.proto'); |
| 118 | var echo_service = test_proto.lookup('EchoService'); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 119 | server = new grpc.Server(); |
| 120 | server.addProtoService(echo_service, { |
| 121 | echo: function(call, callback) { |
| 122 | callback(null, call.request); |
murgatroid99 | 5828fb9 | 2015-05-11 14:27:39 -0700 | [diff] [blame] | 123 | } |
| 124 | }); |
| 125 | var port = server.bind('localhost:0'); |
| 126 | var Client = surface_client.makeProtobufClientConstructor(echo_service); |
| 127 | client = new Client('localhost:' + port); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 128 | server.start(); |
murgatroid99 | 5828fb9 | 2015-05-11 14:27:39 -0700 | [diff] [blame] | 129 | }); |
| 130 | after(function() { |
| 131 | server.shutdown(); |
| 132 | }); |
| 133 | it('should echo the recieved message directly', function(done) { |
| 134 | client.echo({value: 'test value', value2: 3}, function(error, response) { |
| 135 | assert.ifError(error); |
| 136 | assert.deepEqual(response, {value: 'test value', value2: 3}); |
| 137 | done(); |
| 138 | }); |
| 139 | }); |
| 140 | }); |
murgatroid99 | e0f24dc | 2015-03-19 11:16:48 -0700 | [diff] [blame] | 141 | describe('Generic client and server', function() { |
| 142 | function toString(val) { |
| 143 | return val.toString(); |
| 144 | } |
| 145 | function toBuffer(str) { |
| 146 | return new Buffer(str); |
| 147 | } |
| 148 | var string_service_attrs = { |
| 149 | 'capitalize' : { |
| 150 | path: '/string/capitalize', |
| 151 | requestStream: false, |
| 152 | responseStream: false, |
| 153 | requestSerialize: toBuffer, |
| 154 | requestDeserialize: toString, |
| 155 | responseSerialize: toBuffer, |
| 156 | responseDeserialize: toString |
| 157 | } |
| 158 | }; |
| 159 | describe('String client and server', function() { |
| 160 | var client; |
| 161 | var server; |
| 162 | before(function() { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 163 | server = new grpc.Server(); |
| 164 | server.addService(string_service_attrs, { |
| 165 | capitalize: function(call, callback) { |
| 166 | callback(null, _.capitalize(call.request)); |
murgatroid99 | e0f24dc | 2015-03-19 11:16:48 -0700 | [diff] [blame] | 167 | } |
| 168 | }); |
| 169 | var port = server.bind('localhost:0'); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 170 | server.start(); |
murgatroid99 | e0f24dc | 2015-03-19 11:16:48 -0700 | [diff] [blame] | 171 | var Client = grpc.makeGenericClientConstructor(string_service_attrs); |
| 172 | client = new Client('localhost:' + port); |
| 173 | }); |
| 174 | after(function() { |
| 175 | server.shutdown(); |
| 176 | }); |
| 177 | it('Should respond with a capitalized string', function(done) { |
| 178 | client.capitalize('abc', function(err, response) { |
| 179 | assert.ifError(err); |
| 180 | assert.strictEqual(response, 'Abc'); |
| 181 | done(); |
| 182 | }); |
| 183 | }); |
| 184 | }); |
| 185 | }); |
murgatroid99 | ef9fd53 | 2015-07-16 13:33:23 -0700 | [diff] [blame] | 186 | describe('Echo metadata', function() { |
| 187 | var client; |
| 188 | var server; |
| 189 | before(function() { |
| 190 | var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); |
| 191 | var test_service = test_proto.lookup('TestService'); |
| 192 | server = new grpc.Server(); |
| 193 | server.addProtoService(test_service, { |
| 194 | unary: function(call, cb) { |
| 195 | call.sendMetadata(call.metadata); |
| 196 | cb(null, {}); |
| 197 | }, |
| 198 | clientStream: function(stream, cb){ |
| 199 | stream.on('data', function(data) {}); |
| 200 | stream.on('end', function() { |
| 201 | stream.sendMetadata(stream.metadata); |
| 202 | cb(null, {}); |
| 203 | }); |
| 204 | }, |
| 205 | serverStream: function(stream) { |
| 206 | stream.sendMetadata(stream.metadata); |
| 207 | stream.end(); |
| 208 | }, |
| 209 | bidiStream: function(stream) { |
| 210 | stream.on('data', function(data) {}); |
| 211 | stream.on('end', function() { |
| 212 | stream.sendMetadata(stream.metadata); |
| 213 | stream.end(); |
| 214 | }); |
| 215 | } |
| 216 | }); |
| 217 | var port = server.bind('localhost:0'); |
| 218 | var Client = surface_client.makeProtobufClientConstructor(test_service); |
| 219 | client = new Client('localhost:' + port); |
| 220 | server.start(); |
| 221 | }); |
| 222 | after(function() { |
| 223 | server.shutdown(); |
| 224 | }); |
| 225 | it('with unary call', function(done) { |
| 226 | var call = client.unary({}, function(err, data) { |
| 227 | assert.ifError(err); |
| 228 | }, {key: ['value']}); |
| 229 | call.on('metadata', function(metadata) { |
| 230 | assert.deepEqual(metadata.key, ['value']); |
| 231 | done(); |
| 232 | }); |
| 233 | }); |
| 234 | it('with client stream call', function(done) { |
| 235 | var call = client.clientStream(function(err, data) { |
| 236 | assert.ifError(err); |
| 237 | }, {key: ['value']}); |
| 238 | call.on('metadata', function(metadata) { |
| 239 | assert.deepEqual(metadata.key, ['value']); |
| 240 | done(); |
| 241 | }); |
| 242 | call.end(); |
| 243 | }); |
| 244 | it('with server stream call', function(done) { |
| 245 | var call = client.serverStream({}, {key: ['value']}); |
| 246 | call.on('data', function() {}); |
| 247 | call.on('metadata', function(metadata) { |
| 248 | assert.deepEqual(metadata.key, ['value']); |
| 249 | done(); |
| 250 | }); |
| 251 | }); |
| 252 | it('with bidi stream call', function(done) { |
| 253 | var call = client.bidiStream({key: ['value']}); |
| 254 | call.on('data', function() {}); |
| 255 | call.on('metadata', function(metadata) { |
| 256 | assert.deepEqual(metadata.key, ['value']); |
| 257 | done(); |
| 258 | }); |
| 259 | call.end(); |
| 260 | }); |
murgatroid99 | 198a1ad | 2015-07-21 14:27:56 -0700 | [diff] [blame] | 261 | it('shows the correct user-agent string', function(done) { |
| 262 | var version = require('../package.json').version; |
| 263 | var call = client.unary({}, function(err, data) { |
| 264 | assert.ifError(err); |
| 265 | }, {key: ['value']}); |
| 266 | call.on('metadata', function(metadata) { |
| 267 | assert(_.startsWith(metadata['user-agent'], 'grpc-node/' + version)); |
| 268 | done(); |
| 269 | }); |
| 270 | }); |
murgatroid99 | ef9fd53 | 2015-07-16 13:33:23 -0700 | [diff] [blame] | 271 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 272 | describe('Other conditions', function() { |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 273 | var client; |
| 274 | var server; |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 275 | var port; |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 276 | before(function() { |
| 277 | var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); |
| 278 | var test_service = test_proto.lookup('TestService'); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 279 | server = new grpc.Server(); |
| 280 | server.addProtoService(test_service, { |
| 281 | unary: function(call, cb) { |
| 282 | var req = call.request; |
| 283 | if (req.error) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 284 | cb(new Error('Requested error'), null, {trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 285 | } else { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 286 | cb(null, {count: 1}, {trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 287 | } |
| 288 | }, |
| 289 | clientStream: function(stream, cb){ |
| 290 | var count = 0; |
| 291 | var errored; |
| 292 | stream.on('data', function(data) { |
| 293 | if (data.error) { |
| 294 | errored = true; |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 295 | cb(new Error('Requested error'), null, {trailer_present: ['yes']}); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 296 | } else { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 297 | count += 1; |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 298 | } |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 299 | }); |
| 300 | stream.on('end', function() { |
| 301 | if (!errored) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 302 | cb(null, {count: count}, {trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 303 | } |
| 304 | }); |
| 305 | }, |
| 306 | serverStream: function(stream) { |
| 307 | var req = stream.request; |
| 308 | if (req.error) { |
| 309 | var err = new Error('Requested error'); |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 310 | err.metadata = {trailer_present: ['yes']}; |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 311 | stream.emit('error', err); |
| 312 | } else { |
| 313 | for (var i = 0; i < 5; i++) { |
| 314 | stream.write({count: i}); |
| 315 | } |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 316 | stream.end({trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 317 | } |
| 318 | }, |
| 319 | bidiStream: function(stream) { |
| 320 | var count = 0; |
| 321 | stream.on('data', function(data) { |
| 322 | if (data.error) { |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 323 | var err = new Error('Requested error'); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 324 | err.metadata = { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 325 | trailer_present: ['yes'], |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 326 | count: ['' + count] |
| 327 | }; |
murgatroid99 | eb21bdd | 2015-03-26 15:19:54 -0700 | [diff] [blame] | 328 | stream.emit('error', err); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 329 | } else { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 330 | stream.write({count: count}); |
| 331 | count += 1; |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 332 | } |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 333 | }); |
| 334 | stream.on('end', function() { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 335 | stream.end({trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 336 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 337 | } |
| 338 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 339 | port = server.bind('localhost:0'); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 340 | var Client = surface_client.makeProtobufClientConstructor(test_service); |
| 341 | client = new Client('localhost:' + port); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 342 | server.start(); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 343 | }); |
| 344 | after(function() { |
| 345 | server.shutdown(); |
| 346 | }); |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 347 | it('channel.getTarget should be available', function() { |
| 348 | assert.strictEqual(typeof client.channel.getTarget(), 'string'); |
| 349 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 350 | describe('Server recieving bad input', function() { |
| 351 | var misbehavingClient; |
| 352 | var badArg = new Buffer([0xFF]); |
| 353 | before(function() { |
| 354 | var test_service_attrs = { |
| 355 | unary: { |
| 356 | path: '/TestService/Unary', |
| 357 | requestStream: false, |
| 358 | responseStream: false, |
| 359 | requestSerialize: _.identity, |
| 360 | responseDeserialize: _.identity |
| 361 | }, |
| 362 | clientStream: { |
| 363 | path: '/TestService/ClientStream', |
| 364 | requestStream: true, |
| 365 | responseStream: false, |
| 366 | requestSerialize: _.identity, |
| 367 | responseDeserialize: _.identity |
| 368 | }, |
| 369 | serverStream: { |
| 370 | path: '/TestService/ServerStream', |
| 371 | requestStream: false, |
| 372 | responseStream: true, |
| 373 | requestSerialize: _.identity, |
| 374 | responseDeserialize: _.identity |
| 375 | }, |
| 376 | bidiStream: { |
| 377 | path: '/TestService/BidiStream', |
| 378 | requestStream: true, |
| 379 | responseStream: true, |
| 380 | requestSerialize: _.identity, |
| 381 | responseDeserialize: _.identity |
| 382 | } |
| 383 | }; |
| 384 | var Client = surface_client.makeClientConstructor(test_service_attrs, |
| 385 | 'TestService'); |
| 386 | misbehavingClient = new Client('localhost:' + port); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 387 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 388 | it('should respond correctly to a unary call', function(done) { |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 389 | misbehavingClient.unary(badArg, function(err, data) { |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 390 | assert(err); |
| 391 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 392 | done(); |
| 393 | }); |
| 394 | }); |
| 395 | it('should respond correctly to a client stream', function(done) { |
| 396 | var call = misbehavingClient.clientStream(function(err, data) { |
| 397 | assert(err); |
| 398 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 399 | done(); |
| 400 | }); |
| 401 | call.write(badArg); |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 402 | // TODO(mlumish): Remove call.end() |
| 403 | call.end(); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 404 | }); |
| 405 | it('should respond correctly to a server stream', function(done) { |
| 406 | var call = misbehavingClient.serverStream(badArg); |
| 407 | call.on('data', function(data) { |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 408 | assert.fail(data, null, 'Unexpected data', '==='); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 409 | }); |
| 410 | call.on('error', function(err) { |
| 411 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 412 | done(); |
| 413 | }); |
| 414 | }); |
| 415 | it('should respond correctly to a bidi stream', function(done) { |
| 416 | var call = misbehavingClient.bidiStream(); |
| 417 | call.on('data', function(data) { |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 418 | assert.fail(data, null, 'Unexpected data', '==='); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 419 | }); |
| 420 | call.on('error', function(err) { |
| 421 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 422 | done(); |
| 423 | }); |
| 424 | call.write(badArg); |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 425 | // TODO(mlumish): Remove call.end() |
| 426 | call.end(); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 427 | }); |
| 428 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 429 | describe('Trailing metadata', function() { |
| 430 | it('should be present when a unary call succeeds', function(done) { |
| 431 | var call = client.unary({error: false}, function(err, data) { |
| 432 | assert.ifError(err); |
| 433 | }); |
| 434 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 435 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 436 | done(); |
| 437 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 438 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 439 | it('should be present when a unary call fails', function(done) { |
| 440 | var call = client.unary({error: true}, function(err, data) { |
| 441 | assert(err); |
| 442 | }); |
| 443 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 444 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 445 | done(); |
| 446 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 447 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 448 | it('should be present when a client stream call succeeds', function(done) { |
| 449 | var call = client.clientStream(function(err, data) { |
| 450 | assert.ifError(err); |
| 451 | }); |
| 452 | call.write({error: false}); |
| 453 | call.write({error: false}); |
| 454 | call.end(); |
| 455 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 456 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 457 | done(); |
| 458 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 459 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 460 | it('should be present when a client stream call fails', function(done) { |
| 461 | var call = client.clientStream(function(err, data) { |
| 462 | assert(err); |
| 463 | }); |
| 464 | call.write({error: false}); |
| 465 | call.write({error: true}); |
| 466 | call.end(); |
| 467 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 468 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 469 | done(); |
| 470 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 471 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 472 | it('should be present when a server stream call succeeds', function(done) { |
| 473 | var call = client.serverStream({error: false}); |
| 474 | call.on('data', function(){}); |
| 475 | call.on('status', function(status) { |
| 476 | assert.strictEqual(status.code, grpc.status.OK); |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 477 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 478 | done(); |
| 479 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 480 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 481 | it('should be present when a server stream call fails', function(done) { |
| 482 | var call = client.serverStream({error: true}); |
| 483 | call.on('data', function(){}); |
| 484 | call.on('error', function(error) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 485 | assert.deepEqual(error.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 486 | done(); |
| 487 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 488 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 489 | it('should be present when a bidi stream succeeds', function(done) { |
| 490 | var call = client.bidiStream(); |
| 491 | call.write({error: false}); |
| 492 | call.write({error: false}); |
| 493 | call.end(); |
| 494 | call.on('data', function(){}); |
| 495 | call.on('status', function(status) { |
| 496 | assert.strictEqual(status.code, grpc.status.OK); |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 497 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 498 | done(); |
| 499 | }); |
murgatroid99 | eb21bdd | 2015-03-26 15:19:54 -0700 | [diff] [blame] | 500 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 501 | it('should be present when a bidi stream fails', function(done) { |
| 502 | var call = client.bidiStream(); |
| 503 | call.write({error: false}); |
| 504 | call.write({error: true}); |
| 505 | call.end(); |
| 506 | call.on('data', function(){}); |
| 507 | call.on('error', function(error) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 508 | assert.deepEqual(error.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 509 | done(); |
| 510 | }); |
murgatroid99 | eb21bdd | 2015-03-26 15:19:54 -0700 | [diff] [blame] | 511 | }); |
| 512 | }); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 513 | describe('Error object should contain the status', function() { |
| 514 | it('for a unary call', function(done) { |
| 515 | client.unary({error: true}, function(err, data) { |
| 516 | assert(err); |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 517 | assert.strictEqual(err.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 518 | assert.strictEqual(err.message, 'Requested error'); |
| 519 | done(); |
| 520 | }); |
| 521 | }); |
| 522 | it('for a client stream call', function(done) { |
| 523 | var call = client.clientStream(function(err, data) { |
| 524 | assert(err); |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 525 | assert.strictEqual(err.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 526 | assert.strictEqual(err.message, 'Requested error'); |
| 527 | done(); |
| 528 | }); |
| 529 | call.write({error: false}); |
| 530 | call.write({error: true}); |
| 531 | call.end(); |
| 532 | }); |
| 533 | it('for a server stream call', function(done) { |
| 534 | var call = client.serverStream({error: true}); |
| 535 | call.on('data', function(){}); |
| 536 | call.on('error', function(error) { |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 537 | assert.strictEqual(error.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 538 | assert.strictEqual(error.message, 'Requested error'); |
| 539 | done(); |
| 540 | }); |
| 541 | }); |
| 542 | it('for a bidi stream call', function(done) { |
| 543 | var call = client.bidiStream(); |
| 544 | call.write({error: false}); |
| 545 | call.write({error: true}); |
| 546 | call.end(); |
| 547 | call.on('data', function(){}); |
| 548 | call.on('error', function(error) { |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 549 | assert.strictEqual(error.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 550 | assert.strictEqual(error.message, 'Requested error'); |
| 551 | done(); |
| 552 | }); |
| 553 | }); |
| 554 | }); |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 555 | describe('call.getPeer should return the peer', function() { |
| 556 | it('for a unary call', function(done) { |
| 557 | var call = client.unary({error: false}, function(err, data) { |
| 558 | assert.ifError(err); |
| 559 | done(); |
| 560 | }); |
| 561 | assert.strictEqual(typeof call.getPeer(), 'string'); |
| 562 | }); |
| 563 | it('for a client stream call', function(done) { |
| 564 | var call = client.clientStream(function(err, data) { |
| 565 | assert.ifError(err); |
| 566 | done(); |
| 567 | }); |
| 568 | assert.strictEqual(typeof call.getPeer(), 'string'); |
| 569 | call.write({error: false}); |
| 570 | call.end(); |
| 571 | }); |
| 572 | it('for a server stream call', function(done) { |
| 573 | var call = client.serverStream({error: false}); |
| 574 | assert.strictEqual(typeof call.getPeer(), 'string'); |
| 575 | call.on('data', function(){}); |
| 576 | call.on('status', function(status) { |
| 577 | assert.strictEqual(status.code, grpc.status.OK); |
| 578 | done(); |
| 579 | }); |
| 580 | }); |
| 581 | it('for a bidi stream call', function(done) { |
| 582 | var call = client.bidiStream(); |
| 583 | assert.strictEqual(typeof call.getPeer(), 'string'); |
| 584 | call.write({error: false}); |
| 585 | call.end(); |
| 586 | call.on('data', function(){}); |
| 587 | call.on('status', function(status) { |
| 588 | done(); |
| 589 | }); |
| 590 | }); |
| 591 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 592 | }); |
murgatroid99 | 4d2d0f0 | 2015-01-29 11:44:46 -0800 | [diff] [blame] | 593 | describe('Cancelling surface client', function() { |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 594 | var client; |
| 595 | var server; |
| 596 | before(function() { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 597 | server = new grpc.Server(); |
| 598 | server.addProtoService(mathService, { |
| 599 | 'div': function(stream) {}, |
| 600 | 'divMany': function(stream) {}, |
| 601 | 'fib': function(stream) {}, |
| 602 | 'sum': function(stream) {} |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 603 | }); |
| 604 | var port = server.bind('localhost:0'); |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 605 | var Client = surface_client.makeProtobufClientConstructor(mathService); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 606 | client = new Client('localhost:' + port); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 607 | server.start(); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 608 | }); |
| 609 | after(function() { |
| 610 | server.shutdown(); |
| 611 | }); |
| 612 | it('Should correctly cancel a unary call', function(done) { |
| 613 | var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) { |
| 614 | assert.strictEqual(err.code, surface_client.status.CANCELLED); |
| 615 | done(); |
| 616 | }); |
| 617 | call.cancel(); |
| 618 | }); |
| 619 | it('Should correctly cancel a client stream call', function(done) { |
| 620 | var call = client.sum(function(err, resp) { |
| 621 | assert.strictEqual(err.code, surface_client.status.CANCELLED); |
| 622 | done(); |
| 623 | }); |
| 624 | call.cancel(); |
| 625 | }); |
| 626 | it('Should correctly cancel a server stream call', function(done) { |
| 627 | var call = client.fib({'limit': 5}); |
murgatroid99 | 65b784e | 2015-05-06 16:46:19 -0700 | [diff] [blame] | 628 | call.on('error', function(error) { |
| 629 | assert.strictEqual(error.code, surface_client.status.CANCELLED); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 630 | done(); |
| 631 | }); |
| 632 | call.cancel(); |
| 633 | }); |
| 634 | it('Should correctly cancel a bidi stream call', function(done) { |
| 635 | var call = client.divMany(); |
murgatroid99 | 65b784e | 2015-05-06 16:46:19 -0700 | [diff] [blame] | 636 | call.on('error', function(error) { |
| 637 | assert.strictEqual(error.code, surface_client.status.CANCELLED); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 638 | done(); |
| 639 | }); |
| 640 | call.cancel(); |
| 641 | }); |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 642 | }); |