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 Notify_ShouldLogExceptionIfResponseIsFaulted()
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            var logger = new FakeLogger();

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                requestHandler.HttpRequest.IsFaultedGetResponse = true;

                var notifier = new AirbrakeNotifier(config, logger, requestHandler);
                notifier.Notify(new Exception());

                var resetEvent = new AutoResetEvent(false);
                while (!resetEvent.WaitOne(2000))
                {
                    if (logger.LoggedExceptions.Count > 0)
                    {
                        resetEvent.Set();
                    }
                }
#if NET35
                resetEvent.Close();
#else
                resetEvent.Dispose();
#endif
            }

            Assert.True(logger.LoggedExceptions.Count > 0);
        }
        public void NotifyAsync_ShouldSetExceptionIfRequestStreamOrResponseIsFaulted(string faultedTask)
        {
            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/\"}";

                requestHandler.HttpRequest.IsFaultedGetRequestStream = faultedTask == "GetRequestStream";
                requestHandler.HttpRequest.IsFaultedGetResponse      = faultedTask == "GetResponse";

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

                Exception exception  = null;
                var       resetEvent = new AutoResetEvent(false);
                notifier.NotifyCompleted += (sender, eventArgs) =>
                {
                    exception = eventArgs.Error;
                    resetEvent.Set();
                };

                notifier.NotifyAsync(new Exception());

                Assert.Equal(resetEvent.WaitOne(), true);
                resetEvent.Close();

                Assert.IsType <Exception>(exception);
            }
        }
        public void NotifyAsync_ShouldSetStatusToIgnoredIfNoticeIsNullAfterApplyingFilters()
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler);
                notifier.AddFilter(notice => null);
#if NET35
                var resetEvent = new AutoResetEvent(false);
                AirbrakeResponse airbrakeResponse = null;
                notifier.NotifyCompleted += (sender, eventArgs) =>
                {
                    airbrakeResponse = eventArgs.Result;
                    resetEvent.Set();
                };

                notifier.NotifyAsync(new Exception());

                Assert.Equal(resetEvent.WaitOne(2000), true);
                resetEvent.Close();
#else
                var airbrakeResponse = notifier.NotifyAsync(new Exception()).Result;
#endif
                Assert.True(airbrakeResponse.Status == RequestStatus.Ignored);
            }
        }
        public void Notify_ShouldLogResponseIfResponseIsOk()
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            var logger = new FakeLogger();

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

                var notifier = new AirbrakeNotifier(config, logger, requestHandler);
                notifier.Notify(new Exception());

                var resetEvent = new AutoResetEvent(false);
                while (!resetEvent.WaitOne(2000))
                {
                    if (logger.LoggedResponses.Count > 0)
                    {
                        resetEvent.Set();
                    }
                }
#if NET35
                resetEvent.Close();
#else
                resetEvent.Dispose();
#endif
            }

            Assert.True(logger.LoggedResponses.Count > 0);
        }
        public void Ctor_ShouldSetFileLoggerIfCustomIsEmptyAndLogFileIsSet()
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6",
                LogFile    = Guid.NewGuid() + ".log"
            };

            var logFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location ?? string.Empty), config.LogFile);

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

                var notifier = new AirbrakeNotifier(config, null, requestHandler);
                notifier.Notify(new Exception());

                var resetEvent = new AutoResetEvent(false);
                while (!resetEvent.WaitOne(2000))
                {
                    if (File.Exists(logFile))
                    {
                        resetEvent.Set();
                    }
                }
                resetEvent.Close();
            }

            Assert.True(File.Exists(logFile));
            File.Delete(logFile);
        }
        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://airbrake.io/\"}";

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

                var notifier = new AirbrakeNotifier(config, new FakeLogger(), requestHandler);
#if NET35
#else
                var notifyTask    = notifier.NotifyAsync(new Exception());
                var exceptionTask = Record.ExceptionAsync(() => notifyTask);

                Assert.NotNull(exceptionTask);
                var exception = exceptionTask.Result;
                Assert.True(notifyTask.IsCanceled);
                Assert.IsType <TaskCanceledException>(exception);
#endif
            }
        }
        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");
            }
        }
        public void NotifyAsync_ShouldSetExceptionIfRequestStreamOrResponseIsFaulted(string faultedTask)
        {
            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.IsFaultedGetRequestStream = faultedTask == "GetRequestStream";
                requestHandler.HttpRequest.IsFaultedGetResponse      = faultedTask == "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.IsFaulted);
                Assert.IsType <Exception>(exception);
            }
        }
        public void NotifyAsync_ShouldSetStatusToIgnoredIfEnvironmentIsIgnored()
        {
            var config = new AirbrakeConfig
            {
                ProjectId          = "127348",
                ProjectKey         = "e2046ca6e4e9214b24ad252e3c99a0f6",
                Environment        = "test",
                IgnoreEnvironments = new List <string> {
                    "test"
                }
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                var notifier   = new AirbrakeNotifier(config, new FakeLogger(), requestHandler);
                var resetEvent = new AutoResetEvent(false);
                AirbrakeResponse airbrakeResponse = null;
                notifier.NotifyCompleted += (sender, eventArgs) =>
                {
                    airbrakeResponse = eventArgs.Result;
                    resetEvent.Set();
                };

                notifier.NotifyAsync(new Exception());

                Assert.Equal(resetEvent.WaitOne(2000), true);
                resetEvent.Close();

                Assert.True(airbrakeResponse.Status == RequestStatus.Ignored);
            }
        }
        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));
                }
            }
        }
        public void NotifyAsync_ShouldSetStatusToIgnoredIfNoticeIsNull()
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = "127348",
                ProjectKey = "e2046ca6e4e9214b24ad252e3c99a0f6"
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                var notifier = new AirbrakeNotifier(config, requestHandler);
                var response = notifier.NotifyAsync(null).Result;

                Assert.True(response.Status == RequestStatus.Ignored);
            }
        }
        public void NotifyAsync_ShouldThrowExceptionIfProjectIdOrKeyIsNotSet(string projectId, string projectKey)
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = projectId,
                ProjectKey = projectKey
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                var notifier  = new AirbrakeNotifier(config, new FakeLogger(), requestHandler);
                var exception = Record.Exception(() => notifier.NotifyAsync(new Exception()));

                Assert.IsType <Exception>(exception);
                Assert.True(exception.Message.Equals("Project " + (string.IsNullOrEmpty(projectId) ? "Id" : "Key") + " is required"));
            }
        }
        private void NotifyAsync()
        {
            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);
                var notice           = notifier.BuildNotice(new Exception());
                var airbrakeResponse = notifier.NotifyAsync(notice).Result;
            }
        }
        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://airbrake.io/\"}";

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

                notifier.AddFilter(notice =>
                {
                    notice.Context.Action = "modified action";
                    return(notice);
                });
#if NET35
                var resetEvent = new AutoResetEvent(false);
                AirbrakeResponse airbrakeResponse = null;
                notifier.NotifyCompleted += (sender, eventArgs) =>
                {
                    airbrakeResponse = eventArgs.Result;
                    resetEvent.Set();
                };

                notifier.NotifyAsync(new Exception());

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

                Assert.True(airbrakeResponse.Status == RequestStatus.Success);
                Assert.NotNull(actualNotice.Context);
                Assert.True(actualNotice.Context.Action == "modified action");
            }
        }
        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://airbrake.io/\"}";

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

                notifier.NotifyAsync(new Exception());

                Assert.Equal(resetEvent.WaitOne(2000), true);
                resetEvent.Close();
#else
                var airbrakeResponse = notifier.NotifyAsync(new Exception()).Result;
#endif
                if (isStatusCodeCreated)
                {
                    Assert.True(airbrakeResponse.Status == RequestStatus.Success);
                }
                else
                {
                    Assert.True(airbrakeResponse.Status == RequestStatus.RequestError);
                }
            }
        }
        public void NotifyAsync_ShouldSetStatusToIgnoredIfEnvironmentIsIgnored()
        {
            var config = new AirbrakeConfig
            {
                ProjectId          = "127348",
                ProjectKey         = "e2046ca6e4e9214b24ad252e3c99a0f6",
                Environment        = "test",
                IgnoreEnvironments = new List <string> {
                    "test"
                }
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                var notifier = new AirbrakeNotifier(config, requestHandler);
                var response = notifier.NotifyAsync(NoticeBuilder.BuildNotice()).Result;

                Assert.True(response.Status == RequestStatus.Ignored);
            }
        }
        public void NotifyAsync_ShouldThrowExceptionIfProjectIdOrKeyIsNotSet(string projectId, string projectKey)
        {
            var config = new AirbrakeConfig
            {
                ProjectId  = projectId,
                ProjectKey = projectKey
            };

            using (var requestHandler = new FakeHttpRequestHandler())
            {
                var notifier      = new AirbrakeNotifier(config, requestHandler);
                var exceptionTask = Record.ExceptionAsync(() => Task.Run(() => notifier.NotifyAsync(NoticeBuilder.BuildNotice())));

                Assert.NotNull(exceptionTask);

                var exception = exceptionTask.Result;

                Assert.IsType <Exception>(exception);
                Assert.Equal("Project " + (string.IsNullOrEmpty(projectId) ? "Id" : "Key") + " is required", exception.Message);
            }
        }