blob: 331c3321e146b4aec3eaee672fa1401983e55618 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermuscha7608b02015-02-03 17:54:38 -080019using System;
Jan Tattermuscha1e60972015-12-10 19:54:10 -080020using System.Collections.Generic;
Jan Tattermusch337a2dd2015-02-13 15:41:41 -080021using System.Diagnostics;
Jan Tattermusch766d72b2015-07-21 20:09:25 -070022using System.Linq;
Jan Tattermusch30868622015-02-19 09:22:33 -080023using System.Threading;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080024using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080025using Grpc.Core;
26using Grpc.Core.Internal;
Jan Tattermusch452ca9b2015-10-29 10:38:03 -070027using Grpc.Core.Profiling;
Jan Tattermusch30868622015-02-19 09:22:33 -080028using Grpc.Core.Utils;
29using NUnit.Framework;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080030
Jan Tattermusch30868622015-02-19 09:22:33 -080031namespace Grpc.Core.Tests
Jan Tattermuscha7608b02015-02-03 17:54:38 -080032{
33 public class ClientServerTest
34 {
Jan Tattermusch062c3292015-07-23 20:28:42 -070035 const string Host = "127.0.0.1";
Jan Tattermuscha7608b02015-02-03 17:54:38 -080036
Jan Tattermusch0abb8472015-08-07 20:28:44 -070037 MockServiceHelper helper;
Jan Tattermusche5c44602015-05-01 11:12:34 -070038 Server server;
39 Channel channel;
Jan Tattermuscha5272b62015-04-30 11:56:46 -070040
Jan Tattermusche5c44602015-05-01 11:12:34 -070041 [SetUp]
42 public void Init()
43 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -070044 helper = new MockServiceHelper(Host);
45 server = helper.GetServer();
Jan Tattermusche5c44602015-05-01 11:12:34 -070046 server.Start();
Jan Tattermusch0abb8472015-08-07 20:28:44 -070047 channel = helper.GetChannel();
Jan Tattermusche5c44602015-05-01 11:12:34 -070048 }
49
50 [TearDown]
Jan Tattermusch607307d2015-02-18 11:05:45 -080051 public void Cleanup()
52 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070053 channel.ShutdownAsync().Wait();
Jan Tattermusche5c44602015-05-01 11:12:34 -070054 server.ShutdownAsync().Wait();
55 }
56
Jan Tattermuscha7608b02015-02-03 17:54:38 -080057 [Test]
Jan Tattermusch0abb8472015-08-07 20:28:44 -070058 public async Task UnaryCall()
Jan Tattermuscha7608b02015-02-03 17:54:38 -080059 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +020060 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch0abb8472015-08-07 20:28:44 -070061 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +020062 return Task.FromResult(request);
Jan Tattermusch0abb8472015-08-07 20:28:44 -070063 });
64
65 Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC"));
66
67 Assert.AreEqual("ABC", await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "ABC"));
Jan Tattermuscha5272b62015-04-30 11:56:46 -070068 }
69
70 [Test]
Jan Tattermusche5c44602015-05-01 11:12:34 -070071 public void UnaryCall_ServerHandlerThrows()
Jan Tattermuscha5272b62015-04-30 11:56:46 -070072 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -070073 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermuscha5272b62015-04-30 11:56:46 -070074 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -070075 throw new Exception("This was thrown on purpose by a test");
76 });
77
78 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
79 Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode);
80
Jan Tattermusche331da22016-04-08 11:05:39 -070081 var ex2 = Assert.ThrowsAsync<RpcException>(async () => await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "abc"));
Jan Tattermusch0abb8472015-08-07 20:28:44 -070082 Assert.AreEqual(StatusCode.Unknown, ex2.Status.StatusCode);
Jan Tattermusche5c44602015-05-01 11:12:34 -070083 }
Craig Tiller190d3602015-02-18 09:23:38 -080084
Jan Tattermusche5c44602015-05-01 11:12:34 -070085 [Test]
Jan Tattermusch1cf8d422015-07-21 10:37:55 -070086 public void UnaryCall_ServerHandlerThrowsRpcException()
87 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -070088 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch1cf8d422015-07-21 10:37:55 -070089 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -070090 throw new RpcException(new Status(StatusCode.Unauthenticated, ""));
91 });
92
93 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
94 Assert.AreEqual(StatusCode.Unauthenticated, ex.Status.StatusCode);
Jan Tattermuscha87f6d82017-08-08 16:35:51 +020095 Assert.AreEqual(0, ex.Trailers.Count);
Jan Tattermusch0abb8472015-08-07 20:28:44 -070096
Jan Tattermusche331da22016-04-08 11:05:39 -070097 var ex2 = Assert.ThrowsAsync<RpcException>(async () => await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "abc"));
Jan Tattermusch0abb8472015-08-07 20:28:44 -070098 Assert.AreEqual(StatusCode.Unauthenticated, ex2.Status.StatusCode);
Jan Tattermuscha87f6d82017-08-08 16:35:51 +020099 Assert.AreEqual(0, ex.Trailers.Count);
100 }
101
102 [Test]
103 public void UnaryCall_ServerHandlerThrowsRpcExceptionWithTrailers()
104 {
105 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
106 {
107 var trailers = new Metadata { {"xyz", "xyz-value"} };
108 throw new RpcException(new Status(StatusCode.Unauthenticated, ""), trailers);
109 });
110
111 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
112 Assert.AreEqual(StatusCode.Unauthenticated, ex.Status.StatusCode);
113 Assert.AreEqual(1, ex.Trailers.Count);
114 Assert.AreEqual("xyz", ex.Trailers[0].Key);
115 Assert.AreEqual("xyz-value", ex.Trailers[0].Value);
116
117 var ex2 = Assert.ThrowsAsync<RpcException>(async () => await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "abc"));
118 Assert.AreEqual(StatusCode.Unauthenticated, ex2.Status.StatusCode);
119 Assert.AreEqual(1, ex2.Trailers.Count);
120 Assert.AreEqual("xyz", ex2.Trailers[0].Key);
121 Assert.AreEqual("xyz-value", ex2.Trailers[0].Value);
Jan Tattermusch1cf8d422015-07-21 10:37:55 -0700122 }
123
124 [Test]
125 public void UnaryCall_ServerHandlerSetsStatus()
126 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200127 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch1cf8d422015-07-21 10:37:55 -0700128 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700129 context.Status = new Status(StatusCode.Unauthenticated, "");
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200130 return Task.FromResult("");
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700131 });
Jan Tattermusch1cf8d422015-07-21 10:37:55 -0700132
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700133 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
134 Assert.AreEqual(StatusCode.Unauthenticated, ex.Status.StatusCode);
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200135 Assert.AreEqual(0, ex.Trailers.Count);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700136
Jan Tattermusche331da22016-04-08 11:05:39 -0700137 var ex2 = Assert.ThrowsAsync<RpcException>(async () => await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "abc"));
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700138 Assert.AreEqual(StatusCode.Unauthenticated, ex2.Status.StatusCode);
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200139 Assert.AreEqual(0, ex2.Trailers.Count);
140 }
141
142 [Test]
143 public void UnaryCall_ServerHandlerSetsStatusAndTrailers()
144 {
Jan Tattermusch23126342017-08-11 18:21:05 +0200145 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200146 {
147 context.Status = new Status(StatusCode.Unauthenticated, "");
148 context.ResponseTrailers.Add("xyz", "xyz-value");
Jan Tattermusch23126342017-08-11 18:21:05 +0200149 return Task.FromResult("");
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200150 });
151
152 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
153 Assert.AreEqual(StatusCode.Unauthenticated, ex.Status.StatusCode);
154 Assert.AreEqual(1, ex.Trailers.Count);
155 Assert.AreEqual("xyz", ex.Trailers[0].Key);
156 Assert.AreEqual("xyz-value", ex.Trailers[0].Value);
157
158 var ex2 = Assert.ThrowsAsync<RpcException>(async () => await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "abc"));
159 Assert.AreEqual(StatusCode.Unauthenticated, ex2.Status.StatusCode);
160 Assert.AreEqual(1, ex2.Trailers.Count);
161 Assert.AreEqual("xyz", ex2.Trailers[0].Key);
162 Assert.AreEqual("xyz-value", ex2.Trailers[0].Value);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700163 }
164
165 [Test]
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700166 public async Task ClientStreamingCall()
Jan Tattermusche5c44602015-05-01 11:12:34 -0700167 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700168 helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) =>
169 {
170 string result = "";
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200171 await requestStream.ForEachAsync((request) =>
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700172 {
173 result += request;
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200174 return TaskUtils.CompletedTask;
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700175 });
176 await Task.Delay(100);
177 return result;
178 });
Jan Tattermusche5c44602015-05-01 11:12:34 -0700179
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700180 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700181 await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });
Jan Tattermusch9b048e52015-07-24 21:20:24 -0700182 Assert.AreEqual("ABC", await call.ResponseAsync);
Jan Tattermuscha1e60972015-12-10 19:54:10 -0800183
184 Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
185 Assert.IsNotNull(call.GetTrailers());
186 }
187
188 [Test]
189 public async Task ServerStreamingCall()
190 {
191 helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) =>
192 {
Jan Tattermusch8fe3f642015-12-10 20:30:46 -0800193 await responseStream.WriteAllAsync(request.Split(new []{' '}));
Jan Tattermuscha1e60972015-12-10 19:54:10 -0800194 context.ResponseTrailers.Add("xyz", "");
195 });
196
197 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "A B C");
198 CollectionAssert.AreEqual(new string[] { "A", "B", "C" }, await call.ResponseStream.ToListAsync());
199
200 Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200201 Assert.AreEqual("xyz", call.GetTrailers()[0].Key);
Jan Tattermuscha1e60972015-12-10 19:54:10 -0800202 }
203
204 [Test]
Jan Tattermuscha83ad2a2016-05-02 17:13:00 -0700205 public async Task ServerStreamingCall_EndOfStreamIsIdempotent()
206 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200207 helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>((request, responseStream, context) => TaskUtils.CompletedTask);
Jan Tattermuscha83ad2a2016-05-02 17:13:00 -0700208
209 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "");
210
211 Assert.IsFalse(await call.ResponseStream.MoveNext());
212 Assert.IsFalse(await call.ResponseStream.MoveNext());
213 }
214
215 [Test]
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200216 public void ServerStreamingCall_ErrorCanBeAwaitedTwice()
Jan Tattermuscha83ad2a2016-05-02 17:13:00 -0700217 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200218 helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>((request, responseStream, context) =>
Jan Tattermuscha83ad2a2016-05-02 17:13:00 -0700219 {
220 context.Status = new Status(StatusCode.InvalidArgument, "");
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200221 return TaskUtils.CompletedTask;
Jan Tattermuscha83ad2a2016-05-02 17:13:00 -0700222 });
223
224 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "");
225
226 var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext());
227 Assert.AreEqual(StatusCode.InvalidArgument, ex.Status.StatusCode);
228
229 // attempting MoveNext again should result in throwing the same exception.
230 var ex2 = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext());
231 Assert.AreEqual(StatusCode.InvalidArgument, ex2.Status.StatusCode);
232 }
233
234 [Test]
Jan Tattermusch23126342017-08-11 18:21:05 +0200235 public void ServerStreamingCall_TrailersFromMultipleSourcesGetConcatenated()
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200236 {
Jan Tattermusch23126342017-08-11 18:21:05 +0200237 helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>((request, responseStream, context) =>
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200238 {
239 context.ResponseTrailers.Add("xyz", "xyz-value");
240 throw new RpcException(new Status(StatusCode.InvalidArgument, ""), new Metadata { {"abc", "abc-value"} });
241 });
242
243 var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "");
244
245 var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext());
246 Assert.AreEqual(StatusCode.InvalidArgument, ex.Status.StatusCode);
247 Assert.AreEqual(2, call.GetTrailers().Count);
248 Assert.AreEqual(2, ex.Trailers.Count);
249 Assert.AreEqual("xyz", ex.Trailers[0].Key);
250 Assert.AreEqual("xyz-value", ex.Trailers[0].Value);
251 Assert.AreEqual("abc", ex.Trailers[1].Key);
252 Assert.AreEqual("abc-value", ex.Trailers[1].Value);
253 }
254
255 [Test]
Jan Tattermuscha1e60972015-12-10 19:54:10 -0800256 public async Task DuplexStreamingCall()
257 {
258 helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) =>
259 {
260 while (await requestStream.MoveNext())
261 {
262 await responseStream.WriteAsync(requestStream.Current);
263 }
264 context.ResponseTrailers.Add("xyz", "xyz-value");
265 });
266
267 var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreamingCall());
268 await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });
269 CollectionAssert.AreEqual(new string[] { "A", "B", "C" }, await call.ResponseStream.ToListAsync());
270
271 Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
Jan Tattermuscha87f6d82017-08-08 16:35:51 +0200272 Assert.AreEqual("xyz-value", call.GetTrailers()[0].Value);
Jan Tattermusche5c44602015-05-01 11:12:34 -0700273 }
274
275 [Test]
Jan Tattermusch2615f392015-08-07 20:41:26 -0700276 public async Task AsyncUnaryCall_EchoMetadata()
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700277 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200278 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700279 {
280 foreach (Metadata.Entry metadataEntry in context.RequestHeaders)
281 {
282 if (metadataEntry.Key != "user-agent")
283 {
284 context.ResponseTrailers.Add(metadataEntry);
285 }
286 }
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200287 return Task.FromResult("");
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700288 });
289
Jan Tattermusch7d219cf2015-07-21 12:23:31 -0700290 var headers = new Metadata
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700291 {
Jan Tattermusch410c4732015-08-08 00:02:07 -0700292 { "ascii-header", "abcdefg" },
293 { "binary-header-bin", new byte[] { 1, 2, 3, 0, 0xff } }
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700294 };
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700295 var call = Calls.AsyncUnaryCall(helper.CreateUnaryCall(new CallOptions(headers: headers)), "ABC");
Jan Tattermusch2615f392015-08-07 20:41:26 -0700296 await call;
Jan Tattermusch1cf8d422015-07-21 10:37:55 -0700297
Jan Tattermusche7e1c822015-07-21 12:38:07 -0700298 Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
Jan Tattermusch7d219cf2015-07-21 12:23:31 -0700299
Jan Tattermusche7e1c822015-07-21 12:38:07 -0700300 var trailers = call.GetTrailers();
Jan Tattermusch7d219cf2015-07-21 12:23:31 -0700301 Assert.AreEqual(2, trailers.Count);
302 Assert.AreEqual(headers[0].Key, trailers[0].Key);
303 Assert.AreEqual(headers[0].Value, trailers[0].Value);
304
305 Assert.AreEqual(headers[1].Key, trailers[1].Key);
306 CollectionAssert.AreEqual(headers[1].ValueBytes, trailers[1].ValueBytes);
Jan Tattermusch998eb9b2015-07-20 22:12:53 -0700307 }
Jan Tattermusch7ebbc472015-12-08 22:39:02 -0800308
Jan Tattermusch797b8752015-06-17 10:53:59 -0700309 [Test]
310 public void UnknownMethodHandler()
311 {
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700312 var nonexistentMethod = new Method<string, string>(
313 MethodType.Unary,
314 MockServiceHelper.ServiceName,
315 "NonExistentMethod",
316 Marshallers.StringMarshaller,
317 Marshallers.StringMarshaller);
318
319 var callDetails = new CallInvocationDetails<string, string>(channel, nonexistentMethod, new CallOptions());
320
321 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(callDetails, "abc"));
322 Assert.AreEqual(StatusCode.Unimplemented, ex.Status.StatusCode);
Jan Tattermusch797b8752015-06-17 10:53:59 -0700323 }
Jan Tattermusch337a2dd2015-02-13 15:41:41 -0800324
Jan Tattermusch766d72b2015-07-21 20:09:25 -0700325 [Test]
Jan Tattermusch2e5e5f62017-01-18 21:31:06 +0100326 public void StatusDetailIsUtf8()
327 {
328 // some japanese and chinese characters
329 var nonAsciiString = "\u30a1\u30a2\u30a3 \u62b5\u6297\u662f\u5f92\u52b3\u7684";
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200330 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch2e5e5f62017-01-18 21:31:06 +0100331 {
332 context.Status = new Status(StatusCode.Unknown, nonAsciiString);
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200333 return Task.FromResult("");
Jan Tattermusch2e5e5f62017-01-18 21:31:06 +0100334 });
335
336 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
337 Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode);
338 Assert.AreEqual(nonAsciiString, ex.Status.Detail);
339 }
340
341 [Test]
Jan Tattermusch00c144a2015-12-10 20:04:01 -0800342 public void ServerCallContext_PeerInfoPresent()
Jan Tattermusch062c3292015-07-23 20:28:42 -0700343 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200344 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700345 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200346 return Task.FromResult(context.Peer);
Jan Tattermusch0abb8472015-08-07 20:28:44 -0700347 });
348
349 string peer = Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc");
Jan Tattermusch062c3292015-07-23 20:28:42 -0700350 Assert.IsTrue(peer.Contains(Host));
351 }
352
Jan Tattermuschd8bbdea2015-07-22 12:51:06 -0700353 [Test]
Jan Tattermusch00c144a2015-12-10 20:04:01 -0800354 public void ServerCallContext_HostAndMethodPresent()
355 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200356 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch00c144a2015-12-10 20:04:01 -0800357 {
358 Assert.IsTrue(context.Host.Contains(Host));
359 Assert.AreEqual("/tests.Test/Unary", context.Method);
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200360 return Task.FromResult("PASS");
Jan Tattermusch00c144a2015-12-10 20:04:01 -0800361 });
362 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
363 }
364
365 [Test]
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -0800366 public void ServerCallContext_AuthContextNotPopulated()
367 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200368 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -0800369 {
370 Assert.IsFalse(context.AuthContext.IsPeerAuthenticated);
371 Assert.AreEqual(0, context.AuthContext.Properties.Count());
Jan Tattermusch2fb313c2017-08-09 09:41:25 +0200372 return Task.FromResult("PASS");
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -0800373 });
374 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
375 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800376 }
377}