Beispiel #1
0
        public async Task HappyPath()
        {
            var mockLogger = new Mock <ILogService>();

            mockLogger.Setup(l => l.Error(It.IsAny <string>(), It.IsAny <Exception>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()));
            mockLogger.Setup(l => l.Trace(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()));

            TestHandler testHandler = new TestHandler(() => {
                return(new HttpResponseMessage(HttpStatusCode.OK));
            });

            LoggingDelegatingHandler handler = new LoggingDelegatingHandler(mockLogger.Object, new XpikeHttpClientFactoryOptions()
            {
                CommandGroup = "TestGroup",
                CommandName  = "testCommand"
            }, "test")
            {
                InnerHandler = testHandler
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://test.com");

            request.Content = new StringContent("test");

            HttpClient client = new HttpClient(handler);

            HttpResponseMessage response = await client.SendAsync(request);

            mockLogger.Verify(l => l.Error(It.IsAny <string>(), It.IsAny <Exception>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
            mockLogger.Verify(l => l.Trace(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(2));
        }
            public void ShouldSetInnerHandlerToLineBotLogger()
            {
                LineConfiguration configuration = new LineConfiguration()
                {
                    ChannelAccessToken = nameof(ShouldReturnInitializedHttpClient)
                };

                HttpClient client = HttpClientFactory.Create(configuration, null);

                FieldInfo field = client.GetType().BaseType.GetRuntimeFields().Where(f => f.Name == "_handler").First();
                LoggingDelegatingHandler logger = field.GetValue(client) as LoggingDelegatingHandler;

                Assert.IsNotNull(logger);
                Assert.IsInstanceOfType(logger.InnerHandler, typeof(HttpClientHandler));
            }
            public async Task ShouldLogApiCall()
            {
                var logger       = new TestLogger();
                var innerHandler = new TestHttpMessageHandler(new byte[] { });

                var loggingDelegatingHandler = new LoggingDelegatingHandler(logger, innerHandler);

                var httpClient = new HttpClient(loggingDelegatingHandler);

                await httpClient.PostAsync("https://foo.bar", new StringContent("Test"));

                Assert.AreEqual(new Uri("https://foo.bar/"), logger.LogApiCallUri);

                var content = await logger.LogApiCallHttpContent.ReadAsStringAsync();

                Assert.AreEqual("Test", content);
            }
Beispiel #4
0
        public async Task TreatNonSuccessErrorsAsWarnings()
        {
            var mockLogger = new Mock <ILogService>();

            mockLogger.Setup(l => l.Error(It.IsAny <string>(), It.IsAny <Exception>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()));
            mockLogger.Setup(l => l.Warn(It.IsAny <string>(), It.IsAny <Exception>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()));
            mockLogger.Setup(l => l.Trace(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()));

            TestHandler testHandler = new TestHandler(() => {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            });

            LoggingDelegatingHandler handler = new LoggingDelegatingHandler(mockLogger.Object, new XpikeHttpClientFactoryOptions()
            {
                CommandGroup = "TestGroup",
                CommandName  = "testCommand",
                TreatErrorsAsWarningsWhenLogging   = true,
                TreatNonSuccessAsErrorsWhenLogging = true
            }, "test")
            {
                InnerHandler = testHandler
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://test.com");

            request.Content = new StringContent("test");

            HttpClient client = new HttpClient(handler);

            try
            {
                HttpResponseMessage response = await client.SendAsync(request);
            }
            catch { }

            mockLogger.Verify(l => l.Error(It.IsAny <string>(), It.IsAny <Exception>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()), Times.Never);
            mockLogger.Verify(l => l.Warn(It.IsAny <string>(), It.IsAny <Exception>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(1));
            mockLogger.Verify(l => l.Trace(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(1));
        }