public void GlobalSetup()
        {
            var marshaller = Marshallers.Create((arg) => MessageExtensions.ToByteArray(arg), bytes => new ChatMessage());
            var method     = new Method <ChatMessage, ChatMessage>(MethodType.Unary, typeof(TestService).FullName, nameof(TestService.SayHello), marshaller, marshaller);

            _callHandler = new UnaryServerCallHandler <ChatMessage, ChatMessage, TestService>(method);

            _trailers = new HeaderDictionary();

            var messageData = new ChatMessage
            {
                Name = "Joe"
            }.ToByteArray();

            _requestStream = new MemoryStream();
            MessageHelpers.WriteMessage(_requestStream, messageData);

            var services = new ServiceCollection();

            services.TryAddSingleton <IGrpcServiceActivator <TestService> >(new TestGrpcServiceActivator <TestService>(new TestService()));
            _requestServices = services.BuildServiceProvider();

            _httpContext = new DefaultHttpContext();
            _httpContext.RequestServices = _requestServices;
            _httpContext.Request.Body    = _requestStream;

            _httpContext.Features.Set <IHttpResponseTrailersFeature>(new TestHttpResponseTrailersFeature
            {
                Trailers = _trailers
            });
        }
예제 #2
0
        public void GlobalSetup()
        {
            var message = new ChatMessage
            {
                Name =
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at ligula nec orci placerat mollis. " +
                    "Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut aliquet non nunc id lobortis. " +
                    "In tincidunt ac sapien sit amet consequat. Interdum et malesuada fames ac ante ipsum primis in faucibus. " +
                    "Duis vel tristique ipsum, eget hendrerit justo. Donec accumsan, purus quis cursus auctor, sapien nisi " +
                    "lacinia ligula, ut vehicula lorem augue vel est. Vestibulum finibus ornare vulputate."
            };

            var services = new ServiceCollection();

            services.TryAddSingleton <IGrpcInterceptorActivator <UnaryAwaitInterceptor> >(new TestGrpcInterceptorActivator <UnaryAwaitInterceptor>(new UnaryAwaitInterceptor()));
            var serviceProvider = services.BuildServiceProvider();

            var marshaller = CreateMarshaller();

            var method = new Method <ChatMessage, ChatMessage>(MethodType.Unary, typeof(TestService).FullName, nameof(TestService.SayHello), marshaller, marshaller);
            var result = Task.FromResult(message);

            _callHandler = new UnaryServerCallHandler <TestService, ChatMessage, ChatMessage>(
                new UnaryServerMethodInvoker <TestService, ChatMessage, ChatMessage>(
                    (service, request, context) => result,
                    method,
                    HttpContextServerCallContextHelper.CreateMethodOptions(
                        compressionProviders: CompressionProviders,
                        responseCompressionAlgorithm: ResponseCompressionAlgorithm,
                        interceptors: Interceptors),
                    new TestGrpcServiceActivator <TestService>(new TestService())),
                NullLoggerFactory.Instance);

            _trailers = new HeaderDictionary();

            _requestMessage = GetMessageData(message);

            _requestPipe = new TestPipeReader();

            _requestServices = serviceProvider;

            _httpContext = new DefaultHttpContext();
            _httpContext.RequestServices     = _requestServices;
            _httpContext.Request.ContentType = GrpcProtocolConstants.GrpcContentType;
            _httpContext.Request.Protocol    = GrpcProtocolConstants.Http2Protocol;

            _httpContext.Features.Set <IRequestBodyPipeFeature>(new TestRequestBodyPipeFeature(_requestPipe));
            _httpContext.Features.Set <IHttpResponseBodyFeature>(new TestResponseBodyFeature(new TestPipeWriter()));
            _httpContext.Features.Set <IHttpResponseTrailersFeature>(new TestHttpResponseTrailersFeature
            {
                Trailers = _trailers
            });
            _headers = _httpContext.Response.Headers;
            SetupHttpContext(_httpContext);
        }
예제 #3
0
        public void GlobalSetup()
        {
            var marshaller = Marshallers.Create((arg) => MessageExtensions.ToByteArray(arg), bytes => new ChatMessage());
            var method     = new Method <ChatMessage, ChatMessage>(MethodType.Unary, typeof(TestService).FullName, nameof(TestService.SayHello), marshaller, marshaller);

            _callHandler = new UnaryServerCallHandler <ChatMessage, ChatMessage, TestService>(method);

            var services = new ServiceCollection();

            services.TryAddSingleton <IGrpcServiceActivator <TestService> >(new TestGrpcServiceActivator <TestService>(new TestService()));
            _requestServices = services.BuildServiceProvider();
        }
예제 #4
0
        public void GlobalSetup()
        {
            var services = new ServiceCollection();

            services.TryAddSingleton <IGrpcInterceptorActivator <UnaryAwaitInterceptor> >(new TestGrpcInterceptorActivator <UnaryAwaitInterceptor>(new UnaryAwaitInterceptor()));
            var serviceProvider = services.BuildServiceProvider();

            var marshaller = Marshallers.Create((arg) => MessageExtensions.ToByteArray(arg), bytes => new ChatMessage());
            var method     = new Method <ChatMessage, ChatMessage>(MethodType.Unary, typeof(TestService).FullName, nameof(TestService.SayHello), marshaller, marshaller);
            var result     = Task.FromResult(new ChatMessage());

            _callHandler = new UnaryServerCallHandler <TestService, ChatMessage, ChatMessage>(
                method,
                (service, request, context) => result,
                new GrpcServiceMethodOptions(method.Name),
                ServiceOptions,
                NullLoggerFactory.Instance,
                new TestGrpcServiceActivator <TestService>(new TestService()),
                serviceProvider);

            _trailers = new HeaderDictionary();

            var message = new ChatMessage
            {
                Name = "Joe"
            };

            var ms = new MemoryStream();

            MessageHelpers.WriteMessage(ms, message);
            _requestMessage = ms.ToArray();

            _requestPipe = new TestPipeReader();

            _requestServices = serviceProvider;

            _httpContext = new DefaultHttpContext();
            _httpContext.RequestServices     = _requestServices;
            _httpContext.Request.ContentType = GrpcProtocolConstants.GrpcContentType;

            _httpContext.Features.Set <IRequestBodyPipeFeature>(new TestRequestBodyPipeFeature(_requestPipe));
            _httpContext.Features.Set <IHttpResponseBodyFeature>(new TestResponseBodyFeature(new TestPipeWriter()));
            _httpContext.Features.Set <IHttpResponseTrailersFeature>(new TestHttpResponseTrailersFeature
            {
                Trailers = _trailers
            });
            SetupHttpContext(_httpContext);
        }
예제 #5
0
        public async Task MinRequestBodyDataRateFeature_Unary_DoesNotDisableRequestBodyDataRate()
        {
            // Arrange
            var httpContext = CreateContext();
            var method      = new Method <TestMessage, TestMessage>(MethodType.Unary, "test", "test", _marshaller, _marshaller);
            var call        = new UnaryServerCallHandler <TestService, TestMessage, TestMessage>(
                method,
                (service, request, context) => Task.FromResult(new TestMessage()),
                new GrpcServiceOptions(),
                NullLoggerFactory.Instance);

            // Act
            await call.HandleCallAsync(httpContext);

            // Assert
            Assert.NotNull(httpContext.Features.Get <IHttpMinRequestBodyDataRateFeature>().MinDataRate);
        }
예제 #6
0
        public void AddUnaryMethod <TRequest, TResponse>(
            Method <TRequest, TResponse> method,
            Func <MethodInfo> resolveContractMethodDefinition,
            IList <object> metadata,
            Func <TService, TRequest, ServerCallContext, Task <TResponse> > handler)
            where TRequest : class
            where TResponse : class
        {
            var filterHandlerFactory = _filterRegistration.CreateHandlerFactory(metadata, resolveContractMethodDefinition);
            var invoker = new UnaryServerCallHandler <TService, TRequest, TResponse>(handler, filterHandlerFactory);

            if (_requiresGrpcMarker)
            {
                metadata = AddServiceModelGrpcMarker(metadata, filterHandlerFactory?.ContractMethodDefinition ?? resolveContractMethodDefinition());
            }

            _context.AddUnaryMethod(method, metadata, invoker.Handle);
        }