client_server_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  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. var assert = require('assert');
  34. var fs = require('fs');
  35. var path = require('path');
  36. var grpc = require('bindings')('grpc.node');
  37. var Server = require('../src/server');
  38. var client = require('../src/client');
  39. var common = require('../src/common');
  40. var _ = require('highland');
  41. var ca_path = path.join(__dirname, 'data/ca.pem');
  42. var key_path = path.join(__dirname, 'data/server1.key');
  43. var pem_path = path.join(__dirname, 'data/server1.pem');
  44. /**
  45. * Helper function to return an absolute deadline given a relative timeout in
  46. * seconds.
  47. * @param {number} timeout_secs The number of seconds to wait before timing out
  48. * @return {Date} A date timeout_secs in the future
  49. */
  50. function getDeadline(timeout_secs) {
  51. var deadline = new Date();
  52. deadline.setSeconds(deadline.getSeconds() + timeout_secs);
  53. return deadline;
  54. }
  55. /**
  56. * Responds to every request with the same data as a response
  57. * @param {Stream} stream
  58. */
  59. function echoHandler(stream) {
  60. stream.pipe(stream);
  61. }
  62. /**
  63. * Responds to every request with an error status
  64. * @param {Stream} stream
  65. */
  66. function errorHandler(stream) {
  67. throw {
  68. 'code' : grpc.status.UNIMPLEMENTED,
  69. 'details' : 'error details'
  70. };
  71. }
  72. /**
  73. * Wait for a cancellation instead of responding
  74. * @param {Stream} stream
  75. */
  76. function cancelHandler(stream) {
  77. // do nothing
  78. }
  79. describe('echo client', function() {
  80. var server;
  81. var channel;
  82. before(function() {
  83. server = new Server();
  84. var port_num = server.bind('0.0.0.0:0');
  85. server.register('echo', echoHandler);
  86. server.register('error', errorHandler);
  87. server.register('cancellation', cancelHandler);
  88. server.start();
  89. channel = new grpc.Channel('localhost:' + port_num);
  90. });
  91. after(function() {
  92. server.shutdown();
  93. });
  94. it('should receive echo responses', function(done) {
  95. var messages = ['echo1', 'echo2', 'echo3', 'echo4'];
  96. var stream = client.makeRequest(
  97. channel,
  98. 'echo');
  99. _(messages).map(function(val) {
  100. return new Buffer(val);
  101. }).pipe(stream);
  102. var index = 0;
  103. stream.on('data', function(chunk) {
  104. assert.equal(messages[index], chunk.toString());
  105. index += 1;
  106. });
  107. stream.on('end', function() {
  108. done();
  109. });
  110. });
  111. it('should get an error status that the server throws', function(done) {
  112. var stream = client.makeRequest(
  113. channel,
  114. 'error',
  115. null,
  116. getDeadline(1));
  117. stream.on('data', function() {});
  118. stream.write(new Buffer('test'));
  119. stream.end();
  120. stream.on('status', function(status) {
  121. assert.equal(status.code, grpc.status.UNIMPLEMENTED);
  122. assert.equal(status.details, 'error details');
  123. done();
  124. });
  125. });
  126. it('should be able to cancel a call', function(done) {
  127. var stream = client.makeRequest(
  128. channel,
  129. 'cancellation',
  130. null,
  131. getDeadline(1));
  132. stream.cancel();
  133. stream.on('status', function(status) {
  134. assert.equal(status.code, grpc.status.CANCELLED);
  135. done();
  136. });
  137. });
  138. });
  139. /* TODO(mlumish): explore options for reducing duplication between this test
  140. * and the insecure echo client test */
  141. describe('secure echo client', function() {
  142. var server;
  143. var channel;
  144. before(function(done) {
  145. fs.readFile(ca_path, function(err, ca_data) {
  146. assert.ifError(err);
  147. fs.readFile(key_path, function(err, key_data) {
  148. assert.ifError(err);
  149. fs.readFile(pem_path, function(err, pem_data) {
  150. assert.ifError(err);
  151. var creds = grpc.Credentials.createSsl(ca_data);
  152. var server_creds = grpc.ServerCredentials.createSsl(null,
  153. key_data,
  154. pem_data);
  155. server = new Server({'credentials' : server_creds});
  156. var port_num = server.bind('0.0.0.0:0', true);
  157. server.register('echo', echoHandler);
  158. server.start();
  159. channel = new grpc.Channel('localhost:' + port_num, {
  160. 'grpc.ssl_target_name_override' : 'foo.test.google.com',
  161. 'credentials' : creds
  162. });
  163. done();
  164. });
  165. });
  166. });
  167. });
  168. after(function() {
  169. server.shutdown();
  170. });
  171. it('should recieve echo responses', function(done) {
  172. var messages = ['echo1', 'echo2', 'echo3', 'echo4'];
  173. var stream = client.makeRequest(
  174. channel,
  175. 'echo');
  176. _(messages).map(function(val) {
  177. return new Buffer(val);
  178. }).pipe(stream);
  179. var index = 0;
  180. stream.on('data', function(chunk) {
  181. assert.equal(messages[index], chunk.toString());
  182. index += 1;
  183. });
  184. stream.on('end', function() {
  185. server.shutdown();
  186. done();
  187. });
  188. });
  189. });