Пример #1
0
        public async Task SendUnauthenticatedRequest_UnauthenticatedErrorResponse()
        {
            SetExpectedErrorsFilter(writeContext =>
            {
                // This error can happen if the server returns an unauthorized response
                // before the client has finished sending the request content.
                if (writeContext.LoggerName == "Grpc.Net.Client.Internal.GrpcCall" &&
                    writeContext.EventId.Name == "ErrorSendingMessage" &&
                    writeContext.Exception is OperationCanceledException)
                {
                    return(true);
                }

                return(false);
            });

            // Arrage
            var channel = CreateGrpcWebChannel();

            var client = new AuthorizedGreeter.AuthorizedGreeterClient(channel);

            // Act
            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => client.SayHelloAsync(new HelloRequest {
                Name = "test"
            }).ResponseAsync).DefaultTimeout();

            // Assert
            Assert.AreEqual(StatusCode.Unauthenticated, ex.StatusCode);

            AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Unauthenticated', Message: 'Bad gRPC response. HTTP status code: 401'.");
        }
Пример #2
0
        public async Task SendValidRequest_SuccessResponse()
        {
            // Arrage
            var httpClient = CreateGrpcWebClient();
            var channel    = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
            {
                HttpClient    = httpClient,
                LoggerFactory = LoggerFactory
            });

            var client = new AuthorizedGreeter.AuthorizedGreeterClient(channel);

            // Act
            var ex = await ExceptionAssert.ThrowsAsync <RpcException>(() => client.SayHelloAsync(new HelloRequest {
                Name = "test"
            }).ResponseAsync).DefaultTimeout();

            // Assert
            Assert.AreEqual(StatusCode.Unauthenticated, ex.StatusCode);

            AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Unauthenticated', Message: 'Bad gRPC response. HTTP status code: 401'.");
        }
Пример #3
0
        public async Task SendUnauthenticatedRequest_Success()
        {
            // Arrange
            var tokenResponse = await Fixture.Client.GetAsync("generateJwtToken").DefaultTimeout();

            var token = await tokenResponse.Content.ReadAsStringAsync().DefaultTimeout();

            var channel = CreateGrpcWebChannel();

            var client = new AuthorizedGreeter.AuthorizedGreeterClient(channel);

            // Act
            var metadata = new Metadata();

            metadata.Add("Authorization", $"Bearer {token}");

            var response = await client.SayHelloAsync(new HelloRequest { Name = "test" }, headers : metadata).ResponseAsync.DefaultTimeout();

            // Assert
            Assert.AreEqual("Hello test", response.Message);

            Assert.AreEqual("testuser", response.Claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"]);
        }