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 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); }
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_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"); } }