コード例 #1
0
        public void NotifyAsync_ShouldSetCanceledIfRequestStreamOrResponseIsCanceled(string canceledTask)
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                requestHandler.HttpResponse.StatusCode   = HttpStatusCode.Created;
                requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://api.airbrake.io/\"}";

                requestHandler.HttpRequest.IsCanceledGetRequestStream = canceledTask == "GetRequestStream";
                requestHandler.HttpRequest.IsCanceledGetResponse      = canceledTask == "GetResponse";

                var notifier      = new AirbrakeNotifier(config, requestHandler);
                var notifyTask    = notifier.NotifyAsync(NoticeBuilder.BuildNotice());
                var exceptionTask = Record.ExceptionAsync(() => notifyTask);

                Assert.NotNull(exceptionTask);

                var exception = exceptionTask.Result;

                Assert.True(notifyTask.IsCanceled);
                Assert.IsType <TaskCanceledException>(exception);
            }
        }
コード例 #2
0
        public void ToJsonString()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(new Exception(), string.Empty);

            var actualJson = notice.ToJsonString();

            var serializerSettings = new DataContractJsonSerializerSettings
            {
                UseSimpleDictionaryFormat = true
            };

            var serializer = new DataContractJsonSerializer(typeof(Notice), serializerSettings);

            string expectedJson;

            using (var memoryStream = new MemoryStream())
            {
                serializer.WriteObject(memoryStream, notice);
                memoryStream.Position = 0;

                using (var reader = new StreamReader(memoryStream))
                    expectedJson = reader.ReadToEnd();
            }

            Assert.Equal(expectedJson, actualJson);
        }
コード例 #3
0
        public void NotifyAsync_ShouldSetRequestStatusToSuccessOnlyIfStatusCodeCreated(bool isStatusCodeCreated)
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                requestHandler.HttpResponse.StatusCode = isStatusCodeCreated
                    ? HttpStatusCode.Created
                    : HttpStatusCode.BadRequest;
                requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://api.airbrake.io/\"}";

                var notifier         = new AirbrakeNotifier(config, requestHandler);
                var airbrakeResponse = notifier.NotifyAsync(NoticeBuilder.BuildNotice()).Result;

                if (isStatusCodeCreated)
                {
                    Assert.True(airbrakeResponse.Status == RequestStatus.Success);
                }
                else
                {
                    Assert.True(airbrakeResponse.Status == RequestStatus.RequestError);
                }
            }
        }
コード例 #4
0
        public void NotifyAsync_ShouldUpdateNoticeAfterApplyingFilters()
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                requestHandler.HttpResponse.StatusCode   = HttpStatusCode.Created;
                requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://api.airbrake.io/\"}";

                var notifier = new AirbrakeNotifier(config, requestHandler);

                notifier.AddFilter(n =>
                {
                    n.Context.Action = "modified action";
                    return(n);
                });

                var notice       = notifier.BuildNotice(new Exception());
                var response     = notifier.NotifyAsync(notice).Result;
                var actualNotice = NoticeBuilder.FromJsonString(requestHandler.HttpRequest.GetRequestStreamContent());

                Assert.True(response.Status == RequestStatus.Success);
                Assert.NotNull(actualNotice.Context);
                Assert.True(actualNotice.Context.Action == "modified action");
            }
        }
コード例 #5
0
        public void BuildNotice_ShouldInitializeContextAndNotifierInfo()
        {
            var notice = NoticeBuilder.BuildNotice();

            Assert.NotNull(notice.Context);
            Assert.NotNull(notice.Context.Notifier);
        }
コード例 #6
0
        public void SetHttpContext_ShouldNotSetHttpContextIfContextParamIsEmpty()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(null, null);

            Assert.Null(notice.HttpContext);
        }
コード例 #7
0
        public void SetHttpContext_ShouldSetHttpContextProperty()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(new FakeHttpContext(), null);

            Assert.NotNull(notice.HttpContext);
        }
コード例 #8
0
        public void SetSeverity_ShouldSetSeverityLowercase()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetSeverity(Severity.Critical);

            Assert.Equal("critical", notice.Context.Severity);
        }
コード例 #9
0
        public void SetErrorEntries_ShouldSetExceptionProperty()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(new Exception(), string.Empty);

            Assert.NotNull(notice.Exception);
        }
コード例 #10
0
        public void Ctor_ShouldInitializeContextAndNotifierInfo()
        {
            var builder = new NoticeBuilder();

            var notice = builder.ToNotice();

            Assert.NotNull(notice.Context);
            Assert.NotNull(notice.Context.Notifier);
        }
コード例 #11
0
        public void SetErrorEntries_ShouldSetExceptionProperty()
        {
            var builder = new NoticeBuilder();

            builder.SetErrorEntries(new Exception());

            var notice = builder.ToNotice();

            Assert.NotNull(notice.Exception);
        }
コード例 #12
0
        public void SetHttpContext_ShouldSetHttpContextProperty()
        {
            var builder = new NoticeBuilder();

            builder.SetHttpContext(new FakeHttpContext(), null);

            var notice = builder.ToNotice();

            Assert.NotNull(notice.HttpContext);
        }
コード例 #13
0
        public void SetHttpContext_ShouldNotSetHttpContextIfContextParamIsEmpty()
        {
            var builder = new NoticeBuilder();

            builder.SetHttpContext(null, null);

            var notice = builder.ToNotice();

            Assert.True(notice.Context == null);
        }
コード例 #14
0
        public void SetHttpContext_ShouldSetHttpContext()
        {
            var notifier = new AirbrakeNotifier(new AirbrakeConfig());
            var notice   = NoticeBuilder.BuildNotice();
            var context  = new FakeHttpContext();

            notifier.SetHttpContext(notice, context);

            Assert.NotNull(notice.HttpContext);
        }
コード例 #15
0
        public void SetSeverity_ShouldSetSeverityLowercase()
        {
            var builder = new NoticeBuilder();

            builder.SetSeverity(Severity.Critical);

            var notice = builder.ToNotice();

            Assert.True(notice.Context.Severity.Equals("critical"));
        }
コード例 #16
0
        public void SetConfigurationContext_ShouldNotSetEnvironmentNameAndAppVersionIfConfigIsNull()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetConfigurationContext(null);

            Assert.NotNull(notice.Context);
            Assert.True(string.IsNullOrEmpty(notice.Context.EnvironmentName));
            Assert.True(string.IsNullOrEmpty(notice.Context.AppVersion));
        }
コード例 #17
0
        public void FromJsonString()
        {
            const string json = "{\"errors\":[{\"type\":\"System.Exception\",\"message\":\"Exception: Exception of type 'System.Exception' was thrown.\"}]}";

            var notice = NoticeBuilder.FromJsonString(json);

            Assert.NotNull(notice);
            Assert.NotNull(notice.Errors);
            Assert.True(notice.Errors.Count > 0);
        }
コード例 #18
0
        public void SetErrorEntries_ShouldNotSetActionAndComponentIfNoError()
        {
            var builder = new NoticeBuilder();

            builder.SetErrorEntries(null);

            var notice = builder.ToNotice();

            Assert.True(string.IsNullOrEmpty(notice.Context.Action));
            Assert.True(string.IsNullOrEmpty(notice.Context.Component));
        }
コード例 #19
0
        public void NotifyAsync_ShouldInitializeHttpContextOnlyIfProvided(bool isHttpContextProvided)
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                requestHandler.HttpResponse.StatusCode   = HttpStatusCode.Created;
                requestHandler.HttpResponse.ResponseJson = "{\"Id\":\"12345\",\"Url\":\"https://airbrake.io/\"}";

                var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler);

                FakeHttpContext context = null;
                if (isHttpContextProvided)
                {
                    context = new FakeHttpContext {
                        UserAgent = "test"
                    }
                }
                ;

#if NET35
                var resetEvent = new AutoResetEvent(false);
                AirbrakeResponse airbrakeResponse = null;
                notifier.NotifyCompleted += (sender, eventArgs) =>
                {
                    airbrakeResponse = eventArgs.Result;
                    resetEvent.Set();
                };

                notifier.NotifyAsync(new Exception(), context);

                Assert.Equal(resetEvent.WaitOne(2000), true);
                resetEvent.Close();
#else
                var airbrakeResponse = notifier.NotifyAsync(new Exception(), context).Result;
#endif
                var notice = NoticeBuilder.FromJsonString(requestHandler.HttpRequest.GetRequestStreamContent());

                Assert.True(airbrakeResponse.Status == RequestStatus.Success);

                if (isHttpContextProvided)
                {
                    Assert.True(notice.Context != null);
                }
                else
                {
                    Assert.True(notice.Context == null || string.IsNullOrEmpty(notice.Context.UserAgent));
                }
            }
        }
コード例 #20
0
        public void ToJsonString_ShouldNotSerializeExceptionAndHttpContext()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(new Exception(), string.Empty);
            notice.SetHttpContext(new FakeHttpContext(), null);

            var json = notice.ToJsonString();

            Assert.True(json.IndexOf("\"Exception\":", StringComparison.OrdinalIgnoreCase) == -1);
            Assert.True(json.IndexOf("\"HttpContext\":", StringComparison.OrdinalIgnoreCase) == -1);
        }
コード例 #21
0
        public void SetErrorEntries_ShouldSetErrorEntryUsingMessageIfExceptionEmpty()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(null, "message");

            var errorEntries = notice.Errors;

            Assert.NotNull(errorEntries);
            Assert.True(errorEntries.Count == 1);
            Assert.Equal("message", errorEntries.First().Message);
        }
コード例 #22
0
        public void SetErrorEntries_ShouldSetErrorMessageFromExceptionIfNoMessage()
        {
            var ex = new FakeException("error message from exception");

            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(ex, string.Empty);

            var errorEntries = notice.Errors;

            Assert.NotNull(errorEntries);
            Assert.True(errorEntries.Count == 1);
            Assert.Equal("error message from exception", errorEntries.First().Message);
        }
コード例 #23
0
        public void SetErrorEntries_ShouldSetMessageIfPresent()
        {
            var ex = new FakeException("error message from exception");

            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(ex, "message");

            var errorEntries = notice.Errors;

            Assert.NotNull(errorEntries);
            Assert.True(errorEntries.Count == 1);
            Assert.Equal("message", errorEntries.First().Message);
        }
コード例 #24
0
        public void SetErrorEntries_ShouldSetErrorMessageFromExceptionTypeIfNoExceptionMessage()
        {
            var ex = new FakeException();

            var builder = new NoticeBuilder();

            builder.SetErrorEntries(ex);

            var errorEntries = builder.ToNotice().Errors;

            Assert.NotNull(errorEntries);
            Assert.True(errorEntries.Count == 1);
            Assert.True(errorEntries.First().Message.Equals("FakeException"));
        }
コード例 #25
0
        public void SetConfigurationContext_ShouldSetEnvironmentNameAndAppVersion()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetConfigurationContext(new AirbrakeConfig
            {
                Environment = "local",
                AppVersion  = "1.2.3"
            });

            Assert.NotNull(notice.Context);
            Assert.Equal("local", notice.Context.EnvironmentName);
            Assert.Equal("1.2.3", notice.Context.AppVersion);
        }
コード例 #26
0
        public void SetHttpContext_ShouldSetActionAndContextIfProvided()
        {
            var httpContext = new FakeHttpContext
            {
                Action    = "Action",
                Component = "Component"
            };

            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(httpContext, null);

            Assert.True(!string.IsNullOrEmpty(notice.Context.Action));
            Assert.True(!string.IsNullOrEmpty(notice.Context.Component));
        }
コード例 #27
0
        public void SetConfigurationContext_ShouldSetEnvironmentNameAndAppVersion()
        {
            var builder = new NoticeBuilder();

            builder.SetConfigurationContext(new AirbrakeConfig
            {
                Environment = "local",
                AppVersion  = "1.2.3"
            });

            var notice = builder.ToNotice();

            Assert.NotNull(notice.Context);
            Assert.True(notice.Context.EnvironmentName.Equals("local"));
            Assert.True(notice.Context.AppVersion.Equals("1.2.3"));
        }
コード例 #28
0
        public void SetErrorEntries_ShouldLimitInnerExceptionsToThree()
        {
            var ex = new Exception("Main exception",
                                   new Exception("Inner exception 1",
                                                 new Exception("Inner exception 2",
                                                               new Exception("Inner exception 3",
                                                                             new Exception("Inner exception 4")))));

            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(ex, string.Empty);

            var errorEntries = notice.Errors;

            // main exception + no more than 3 inner exceptions
            Assert.True(errorEntries.Count.Equals(4));
        }
コード例 #29
0
        public void SetHttpContext_ShouldSetParametersIfConfigIsNotDefined()
        {
            var httpContext = new FakeHttpContext
            {
                Parameters      = new Dictionary <string, string>(),
                EnvironmentVars = new Dictionary <string, string>(),
                Session         = new Dictionary <string, string>()
            };

            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(httpContext, null);

            Assert.NotNull(notice.Params);
            Assert.NotNull(notice.EnvironmentVars);
            Assert.NotNull(notice.Session);
        }
コード例 #30
0
        public void SetHttpContext_ContextHttpParameters(string url, string userAgent, string userAddr)
        {
            var httpContext = new FakeHttpContext
            {
                Url       = url,
                UserAgent = userAgent,
                UserAddr  = userAddr
            };

            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(httpContext, null);

            Assert.NotNull(notice.Context);
            Assert.True(string.IsNullOrEmpty(url) ? string.IsNullOrEmpty(notice.Context.Url) : notice.Context.Url.Equals(url));
            Assert.True(string.IsNullOrEmpty(userAgent) ? string.IsNullOrEmpty(notice.Context.UserAgent) : notice.Context.UserAgent.Equals(userAgent));
        }