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 | }); |
| 261 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 262 | describe('Other conditions', function() { |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 263 | var client; |
| 264 | var server; |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 265 | var port; |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 266 | before(function() { |
| 267 | var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto'); |
| 268 | var test_service = test_proto.lookup('TestService'); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 269 | server = new grpc.Server(); |
| 270 | server.addProtoService(test_service, { |
| 271 | unary: function(call, cb) { |
| 272 | var req = call.request; |
| 273 | if (req.error) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 274 | cb(new Error('Requested error'), null, {trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 275 | } else { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 276 | cb(null, {count: 1}, {trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 277 | } |
| 278 | }, |
| 279 | clientStream: function(stream, cb){ |
| 280 | var count = 0; |
| 281 | var errored; |
| 282 | stream.on('data', function(data) { |
| 283 | if (data.error) { |
| 284 | errored = true; |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 285 | cb(new Error('Requested error'), null, {trailer_present: ['yes']}); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 286 | } else { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 287 | count += 1; |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 288 | } |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 289 | }); |
| 290 | stream.on('end', function() { |
| 291 | if (!errored) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 292 | cb(null, {count: count}, {trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 293 | } |
| 294 | }); |
| 295 | }, |
| 296 | serverStream: function(stream) { |
| 297 | var req = stream.request; |
| 298 | if (req.error) { |
| 299 | var err = new Error('Requested error'); |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 300 | err.metadata = {trailer_present: ['yes']}; |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 301 | stream.emit('error', err); |
| 302 | } else { |
| 303 | for (var i = 0; i < 5; i++) { |
| 304 | stream.write({count: i}); |
| 305 | } |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 306 | stream.end({trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 307 | } |
| 308 | }, |
| 309 | bidiStream: function(stream) { |
| 310 | var count = 0; |
| 311 | stream.on('data', function(data) { |
| 312 | if (data.error) { |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 313 | var err = new Error('Requested error'); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 314 | err.metadata = { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 315 | trailer_present: ['yes'], |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 316 | count: ['' + count] |
| 317 | }; |
murgatroid99 | eb21bdd | 2015-03-26 15:19:54 -0700 | [diff] [blame] | 318 | stream.emit('error', err); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 319 | } else { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 320 | stream.write({count: count}); |
| 321 | count += 1; |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 322 | } |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 323 | }); |
| 324 | stream.on('end', function() { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 325 | stream.end({trailer_present: ['yes']}); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 326 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 327 | } |
| 328 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 329 | port = server.bind('localhost:0'); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 330 | var Client = surface_client.makeProtobufClientConstructor(test_service); |
| 331 | client = new Client('localhost:' + port); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 332 | server.start(); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 333 | }); |
| 334 | after(function() { |
| 335 | server.shutdown(); |
| 336 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 337 | describe('Server recieving bad input', function() { |
| 338 | var misbehavingClient; |
| 339 | var badArg = new Buffer([0xFF]); |
| 340 | before(function() { |
| 341 | var test_service_attrs = { |
| 342 | unary: { |
| 343 | path: '/TestService/Unary', |
| 344 | requestStream: false, |
| 345 | responseStream: false, |
| 346 | requestSerialize: _.identity, |
| 347 | responseDeserialize: _.identity |
| 348 | }, |
| 349 | clientStream: { |
| 350 | path: '/TestService/ClientStream', |
| 351 | requestStream: true, |
| 352 | responseStream: false, |
| 353 | requestSerialize: _.identity, |
| 354 | responseDeserialize: _.identity |
| 355 | }, |
| 356 | serverStream: { |
| 357 | path: '/TestService/ServerStream', |
| 358 | requestStream: false, |
| 359 | responseStream: true, |
| 360 | requestSerialize: _.identity, |
| 361 | responseDeserialize: _.identity |
| 362 | }, |
| 363 | bidiStream: { |
| 364 | path: '/TestService/BidiStream', |
| 365 | requestStream: true, |
| 366 | responseStream: true, |
| 367 | requestSerialize: _.identity, |
| 368 | responseDeserialize: _.identity |
| 369 | } |
| 370 | }; |
| 371 | var Client = surface_client.makeClientConstructor(test_service_attrs, |
| 372 | 'TestService'); |
| 373 | misbehavingClient = new Client('localhost:' + port); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 374 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 375 | it('should respond correctly to a unary call', function(done) { |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 376 | misbehavingClient.unary(badArg, function(err, data) { |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 377 | assert(err); |
| 378 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 379 | done(); |
| 380 | }); |
| 381 | }); |
| 382 | it('should respond correctly to a client stream', function(done) { |
| 383 | var call = misbehavingClient.clientStream(function(err, data) { |
| 384 | assert(err); |
| 385 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 386 | done(); |
| 387 | }); |
| 388 | call.write(badArg); |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 389 | // TODO(mlumish): Remove call.end() |
| 390 | call.end(); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 391 | }); |
| 392 | it('should respond correctly to a server stream', function(done) { |
| 393 | var call = misbehavingClient.serverStream(badArg); |
| 394 | call.on('data', function(data) { |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 395 | assert.fail(data, null, 'Unexpected data', '==='); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 396 | }); |
| 397 | call.on('error', function(err) { |
| 398 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 399 | done(); |
| 400 | }); |
| 401 | }); |
| 402 | it('should respond correctly to a bidi stream', function(done) { |
| 403 | var call = misbehavingClient.bidiStream(); |
| 404 | call.on('data', function(data) { |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 405 | assert.fail(data, null, 'Unexpected data', '==='); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 406 | }); |
| 407 | call.on('error', function(err) { |
| 408 | assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT); |
| 409 | done(); |
| 410 | }); |
| 411 | call.write(badArg); |
murgatroid99 | 04589a7 | 2015-05-19 09:51:26 -0700 | [diff] [blame] | 412 | // TODO(mlumish): Remove call.end() |
| 413 | call.end(); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 414 | }); |
| 415 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 416 | describe('Trailing metadata', function() { |
| 417 | it('should be present when a unary call succeeds', function(done) { |
| 418 | var call = client.unary({error: false}, function(err, data) { |
| 419 | assert.ifError(err); |
| 420 | }); |
| 421 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 422 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 423 | done(); |
| 424 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 425 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 426 | it('should be present when a unary call fails', function(done) { |
| 427 | var call = client.unary({error: true}, function(err, data) { |
| 428 | assert(err); |
| 429 | }); |
| 430 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 431 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 432 | done(); |
| 433 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 434 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 435 | it('should be present when a client stream call succeeds', function(done) { |
| 436 | var call = client.clientStream(function(err, data) { |
| 437 | assert.ifError(err); |
| 438 | }); |
| 439 | call.write({error: false}); |
| 440 | call.write({error: false}); |
| 441 | call.end(); |
| 442 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 443 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 444 | done(); |
| 445 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 446 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 447 | it('should be present when a client stream call fails', function(done) { |
| 448 | var call = client.clientStream(function(err, data) { |
| 449 | assert(err); |
| 450 | }); |
| 451 | call.write({error: false}); |
| 452 | call.write({error: true}); |
| 453 | call.end(); |
| 454 | call.on('status', function(status) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 455 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 456 | done(); |
| 457 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 458 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 459 | it('should be present when a server stream call succeeds', function(done) { |
| 460 | var call = client.serverStream({error: false}); |
| 461 | call.on('data', function(){}); |
| 462 | call.on('status', function(status) { |
| 463 | assert.strictEqual(status.code, grpc.status.OK); |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 464 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 465 | done(); |
| 466 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 467 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 468 | it('should be present when a server stream call fails', function(done) { |
| 469 | var call = client.serverStream({error: true}); |
| 470 | call.on('data', function(){}); |
| 471 | call.on('error', function(error) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 472 | assert.deepEqual(error.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 473 | done(); |
| 474 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 475 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 476 | it('should be present when a bidi stream succeeds', function(done) { |
| 477 | var call = client.bidiStream(); |
| 478 | call.write({error: false}); |
| 479 | call.write({error: false}); |
| 480 | call.end(); |
| 481 | call.on('data', function(){}); |
| 482 | call.on('status', function(status) { |
| 483 | assert.strictEqual(status.code, grpc.status.OK); |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 484 | assert.deepEqual(status.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 485 | done(); |
| 486 | }); |
murgatroid99 | eb21bdd | 2015-03-26 15:19:54 -0700 | [diff] [blame] | 487 | }); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 488 | it('should be present when a bidi stream fails', function(done) { |
| 489 | var call = client.bidiStream(); |
| 490 | call.write({error: false}); |
| 491 | call.write({error: true}); |
| 492 | call.end(); |
| 493 | call.on('data', function(){}); |
| 494 | call.on('error', function(error) { |
murgatroid99 | 02c160a | 2015-07-20 15:16:09 -0700 | [diff] [blame] | 495 | assert.deepEqual(error.metadata.trailer_present, ['yes']); |
murgatroid99 | 778c61b | 2015-05-18 16:52:47 -0700 | [diff] [blame] | 496 | done(); |
| 497 | }); |
murgatroid99 | eb21bdd | 2015-03-26 15:19:54 -0700 | [diff] [blame] | 498 | }); |
| 499 | }); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 500 | describe('Error object should contain the status', function() { |
| 501 | it('for a unary call', function(done) { |
| 502 | client.unary({error: true}, function(err, data) { |
| 503 | assert(err); |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 504 | assert.strictEqual(err.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 505 | assert.strictEqual(err.message, 'Requested error'); |
| 506 | done(); |
| 507 | }); |
| 508 | }); |
| 509 | it('for a client stream call', function(done) { |
| 510 | var call = client.clientStream(function(err, data) { |
| 511 | assert(err); |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 512 | assert.strictEqual(err.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 513 | assert.strictEqual(err.message, 'Requested error'); |
| 514 | done(); |
| 515 | }); |
| 516 | call.write({error: false}); |
| 517 | call.write({error: true}); |
| 518 | call.end(); |
| 519 | }); |
| 520 | it('for a server stream call', function(done) { |
| 521 | var call = client.serverStream({error: true}); |
| 522 | call.on('data', function(){}); |
| 523 | call.on('error', function(error) { |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 524 | assert.strictEqual(error.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 525 | assert.strictEqual(error.message, 'Requested error'); |
| 526 | done(); |
| 527 | }); |
| 528 | }); |
| 529 | it('for a bidi stream call', function(done) { |
| 530 | var call = client.bidiStream(); |
| 531 | call.write({error: false}); |
| 532 | call.write({error: true}); |
| 533 | call.end(); |
| 534 | call.on('data', function(){}); |
| 535 | call.on('error', function(error) { |
murgatroid99 | 560b82c | 2015-07-17 13:06:15 -0700 | [diff] [blame] | 536 | assert.strictEqual(error.code, grpc.status.UNKNOWN); |
murgatroid99 | 06f0972 | 2015-07-17 11:26:54 -0700 | [diff] [blame] | 537 | assert.strictEqual(error.message, 'Requested error'); |
| 538 | done(); |
| 539 | }); |
| 540 | }); |
| 541 | }); |
murgatroid99 | d07a278 | 2015-03-26 10:07:15 -0700 | [diff] [blame] | 542 | }); |
murgatroid99 | 4d2d0f0 | 2015-01-29 11:44:46 -0800 | [diff] [blame] | 543 | describe('Cancelling surface client', function() { |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 544 | var client; |
| 545 | var server; |
| 546 | before(function() { |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 547 | server = new grpc.Server(); |
| 548 | server.addProtoService(mathService, { |
| 549 | 'div': function(stream) {}, |
| 550 | 'divMany': function(stream) {}, |
| 551 | 'fib': function(stream) {}, |
| 552 | 'sum': function(stream) {} |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 553 | }); |
| 554 | var port = server.bind('localhost:0'); |
murgatroid99 | e023e98 | 2015-03-18 17:17:33 -0700 | [diff] [blame] | 555 | var Client = surface_client.makeProtobufClientConstructor(mathService); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 556 | client = new Client('localhost:' + port); |
murgatroid99 | 366e64d | 2015-07-15 17:01:49 -0700 | [diff] [blame] | 557 | server.start(); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 558 | }); |
| 559 | after(function() { |
| 560 | server.shutdown(); |
| 561 | }); |
| 562 | it('Should correctly cancel a unary call', function(done) { |
| 563 | var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) { |
| 564 | assert.strictEqual(err.code, surface_client.status.CANCELLED); |
| 565 | done(); |
| 566 | }); |
| 567 | call.cancel(); |
| 568 | }); |
| 569 | it('Should correctly cancel a client stream call', function(done) { |
| 570 | var call = client.sum(function(err, resp) { |
| 571 | assert.strictEqual(err.code, surface_client.status.CANCELLED); |
| 572 | done(); |
| 573 | }); |
| 574 | call.cancel(); |
| 575 | }); |
| 576 | it('Should correctly cancel a server stream call', function(done) { |
| 577 | var call = client.fib({'limit': 5}); |
murgatroid99 | 65b784e | 2015-05-06 16:46:19 -0700 | [diff] [blame] | 578 | call.on('error', function(error) { |
| 579 | assert.strictEqual(error.code, surface_client.status.CANCELLED); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 580 | done(); |
| 581 | }); |
| 582 | call.cancel(); |
| 583 | }); |
| 584 | it('Should correctly cancel a bidi stream call', function(done) { |
| 585 | var call = client.divMany(); |
murgatroid99 | 65b784e | 2015-05-06 16:46:19 -0700 | [diff] [blame] | 586 | call.on('error', function(error) { |
| 587 | assert.strictEqual(error.code, surface_client.status.CANCELLED); |
murgatroid99 | 55dd2ba | 2015-01-26 14:11:18 -0800 | [diff] [blame] | 588 | done(); |
| 589 | }); |
| 590 | call.cancel(); |
| 591 | }); |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 592 | }); |