コード例 #1
0
        public void CheckNullInterceptorRegistrationFails()
        {
            var helper = new MockServiceHelper(Host);

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) =>
            {
                return(Task.FromResult("PASS"));
            });
            Assert.Throws <ArgumentNullException>(() => helper.GetChannel().Intercept(default(Interceptor)));
            Assert.Throws <ArgumentNullException>(() => helper.GetChannel().Intercept(new[] { default(Interceptor) }));
            Assert.Throws <ArgumentNullException>(() => helper.GetChannel().Intercept(new[] { new CallbackInterceptor(() => {}), null }));
            Assert.Throws <ArgumentNullException>(() => helper.GetChannel().Intercept(default(Interceptor[])));
        }
コード例 #2
0
        public void CheckInterceptorOrderInClientInterceptors()
        {
            var helper = new MockServiceHelper(Host);

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) =>
            {
                return(Task.FromResult("PASS"));
            });
            var server = helper.GetServer();

            server.Start();
            var stringBuilder = new StringBuilder();
            var callInvoker   = helper.GetChannel().Intercept(metadata => {
                stringBuilder.Append("interceptor1");
                return(metadata);
            }).Intercept(new CallbackInterceptor(() => stringBuilder.Append("array1")),
                         new CallbackInterceptor(() => stringBuilder.Append("array2")),
                         new CallbackInterceptor(() => stringBuilder.Append("array3")))
                                .Intercept(metadata =>
            {
                stringBuilder.Append("interceptor2");
                return(metadata);
            }).Intercept(metadata =>
            {
                stringBuilder.Append("interceptor3");
                return(metadata);
            });

            Assert.AreEqual("PASS", callInvoker.BlockingUnaryCall(new Method <string, string>(MethodType.Unary, MockServiceHelper.ServiceName, "Unary", Marshallers.StringMarshaller, Marshallers.StringMarshaller), Host, new CallOptions(), ""));
            Assert.AreEqual("interceptor3interceptor2array1array2array3interceptor1", stringBuilder.ToString());
        }
コード例 #3
0
        public async Task CountNumberOfRequestsInClientInterceptors()
        {
            var helper = new MockServiceHelper(Host);

            helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) =>
            {
                var stringBuilder = new StringBuilder();
                await requestStream.ForEachAsync(request =>
                {
                    stringBuilder.Append(request);
                    return(TaskUtils.CompletedTask);
                });
                await Task.Delay(100);
                return(stringBuilder.ToString());
            });

            var callInvoker = helper.GetChannel().Intercept(new ClientStreamingCountingInterceptor());

            var server = helper.GetServer();

            server.Start();
            var call = callInvoker.AsyncClientStreamingCall(new Method <string, string>(MethodType.ClientStreaming, MockServiceHelper.ServiceName, "ClientStreaming", Marshallers.StringMarshaller, Marshallers.StringMarshaller), Host, new CallOptions());
            await call.RequestStream.WriteAllAsync(new string[] { "A", "B", "C" });

            Assert.AreEqual("3", await call.ResponseAsync);

            Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode);
            Assert.IsNotNull(call.GetTrailers());
        }
コード例 #4
0
        public void UserStateVisibleToAllInterceptors()
        {
            object       key1   = new object();
            object       value1 = new object();
            const string key2   = "Interceptor #2";
            const string value2 = "Important state";

            var interceptor1 = new ServerCallContextInterceptor(ctx => {
                // state starts off empty
                Assert.AreEqual(0, ctx.UserState.Count);

                ctx.UserState.Add(key1, value1);
            });

            var interceptor2 = new ServerCallContextInterceptor(ctx => {
                // second interceptor can see state set by the first
                bool found = ctx.UserState.TryGetValue(key1, out object storedValue1);
                Assert.IsTrue(found);
                Assert.AreEqual(value1, storedValue1);

                ctx.UserState.Add(key2, value2);
            });

            var helper = new MockServiceHelper(Host);

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => {
                // call handler can see all the state
                bool found = context.UserState.TryGetValue(key1, out object storedValue1);
                Assert.IsTrue(found);
                Assert.AreEqual(value1, storedValue1);

                found = context.UserState.TryGetValue(key2, out object storedValue2);
                Assert.IsTrue(found);
                Assert.AreEqual(value2, storedValue2);

                return(Task.FromResult("PASS"));
            });
            helper.ServiceDefinition = helper.ServiceDefinition
                                       .Intercept(interceptor2)
                                       .Intercept(interceptor1);

            var server = helper.GetServer();

            server.Start();
            var channel = helper.GetChannel();

            Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), ""));
        }
コード例 #5
0
        public void AddRequestHeaderInServerInterceptor()
        {
            var          helper        = new MockServiceHelper(Host);
            const string MetadataKey   = "x-interceptor";
            const string MetadataValue = "hello world";
            var          interceptor   = new ServerCallContextInterceptor(ctx => ctx.RequestHeaders.Add(new Metadata.Entry(MetadataKey, MetadataValue)));

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) =>
            {
                var interceptorHeader = context.RequestHeaders.Last(m => (m.Key == MetadataKey)).Value;
                Assert.AreEqual(interceptorHeader, MetadataValue);
                return(Task.FromResult("PASS"));
            });
            helper.ServiceDefinition = helper.ServiceDefinition.Intercept(interceptor);
            var server = helper.GetServer();

            server.Start();
            var channel = helper.GetChannel();

            Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), ""));
        }
コード例 #6
0
        public void AddRequestHeaderInClientInterceptor()
        {
            const string HeaderKey   = "x-client-interceptor";
            const string HeaderValue = "hello-world";
            var          helper      = new MockServiceHelper(Host);

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) =>
            {
                var interceptorHeader = context.RequestHeaders.Last(m => (m.Key == HeaderKey)).Value;
                Assert.AreEqual(interceptorHeader, HeaderValue);
                return(Task.FromResult("PASS"));
            });
            var server = helper.GetServer();

            server.Start();
            var callInvoker = helper.GetChannel().Intercept(metadata =>
            {
                metadata = metadata ?? new Metadata();
                metadata.Add(new Metadata.Entry(HeaderKey, HeaderValue));
                return(metadata);
            });

            Assert.AreEqual("PASS", callInvoker.BlockingUnaryCall(new Method <string, string>(MethodType.Unary, MockServiceHelper.ServiceName, "Unary", Marshallers.StringMarshaller, Marshallers.StringMarshaller), Host, new CallOptions(), ""));
        }
コード例 #7
0
        public void VerifyInterceptorOrdering()
        {
            var helper = new MockServiceHelper(Host);

            helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) =>
            {
                return(Task.FromResult("PASS"));
            });
            var stringBuilder = new StringBuilder();

            helper.ServiceDefinition = helper.ServiceDefinition
                                       .Intercept(new ServerCallContextInterceptor(ctx => stringBuilder.Append("A")))
                                       .Intercept(new ServerCallContextInterceptor(ctx => stringBuilder.Append("B1")),
                                                  new ServerCallContextInterceptor(ctx => stringBuilder.Append("B2")),
                                                  new ServerCallContextInterceptor(ctx => stringBuilder.Append("B3")))
                                       .Intercept(new ServerCallContextInterceptor(ctx => stringBuilder.Append("C")));
            var server = helper.GetServer();

            server.Start();
            var channel = helper.GetChannel();

            Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), ""));
            Assert.AreEqual("CB1B2B3A", stringBuilder.ToString());
        }