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);
            }
        }
Exemple #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);
        }
        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);
                }
            }
        }
        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");
            }
        }
Exemple #5
0
        public void BuildNotice_ShouldInitializeContextAndNotifierInfo()
        {
            var notice = NoticeBuilder.BuildNotice();

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

            notice.SetHttpContext(null, null);

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

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

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

            notice.SetSeverity(Severity.Critical);

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

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

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

            var notice = builder.ToNotice();

            Assert.NotNull(notice.Context);
            Assert.NotNull(notice.Context.Notifier);
        }
        public void SetErrorEntries_ShouldSetExceptionProperty()
        {
            var builder = new NoticeBuilder();

            builder.SetErrorEntries(new Exception());

            var notice = builder.ToNotice();

            Assert.NotNull(notice.Exception);
        }
        public void SetHttpContext_ShouldSetHttpContextProperty()
        {
            var builder = new NoticeBuilder();

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

            var notice = builder.ToNotice();

            Assert.NotNull(notice.HttpContext);
        }
        public void SetHttpContext_ShouldNotSetHttpContextIfContextParamIsEmpty()
        {
            var builder = new NoticeBuilder();

            builder.SetHttpContext(null, null);

            var notice = builder.ToNotice();

            Assert.True(notice.Context == null);
        }
        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);
        }
Exemple #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"));
        }
Exemple #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));
        }
Exemple #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);
        }
Exemple #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));
        }
        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));
                }
            }
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
Exemple #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);
        }
        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"));
        }
Exemple #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);
        }
Exemple #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));
        }
Exemple #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"));
        }
Exemple #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));
        }
Exemple #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);
        }
Exemple #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));
        }