Ejemplo n.º 1
0
        public void BuildNotice_ShouldInitializeContextAndNotifierInfo()
        {
            var notice = NoticeBuilder.BuildNotice();

            Assert.NotNull(notice.Context);
            Assert.NotNull(notice.Context.Notifier);
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        public void SetHttpContext_ShouldNotSetHttpContextIfContextParamIsEmpty()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(null, null);

            Assert.Null(notice.HttpContext);
        }
Ejemplo n.º 6
0
        public void SetSeverity_ShouldSetSeverityLowercase()
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetSeverity(Severity.Critical);

            Assert.Equal("critical", notice.Context.Severity);
        }
Ejemplo n.º 7
0
        public void SetHttpContext_ShouldSetHttpContextProperty()
        {
            var notice = NoticeBuilder.BuildNotice();

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

            Assert.NotNull(notice.HttpContext);
        }
Ejemplo n.º 8
0
        public void SetErrorEntries_ShouldSetExceptionProperty()
        {
            var notice = NoticeBuilder.BuildNotice();

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

            Assert.NotNull(notice.Exception);
        }
Ejemplo n.º 9
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));
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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));
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
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));
        }
Ejemplo n.º 19
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));
        }
Ejemplo n.º 20
0
        public void SetErrorEntries_ShouldSetExceptionsInCorrectOrder()
        {
            var ex = new Exception("Main exception",
                                   new Exception("Inner exception 1",
                                                 new Exception("Inner exception 2")));

            var notice = NoticeBuilder.BuildNotice();

            notice.SetErrorEntries(ex, string.Empty);

            var errorEntries = notice.Errors;

            Assert.True(errorEntries.Count.Equals(3));

            Assert.Equal("Main exception", errorEntries[0].Message);
            Assert.Equal("Inner exception 1", errorEntries[1].Message);
            Assert.Equal("Inner exception 2", errorEntries[2].Message);
        }
Ejemplo n.º 21
0
        public void ToJsonString_ShouldTruncateNoticeBigger64KB()
        {
            var httpContext = new FakeHttpContext
            {
                Parameters = new Dictionary <string, string>
                {
                    { "long_param", new string('x', 64001) }
                }
            };

            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(httpContext, null);

            var json = notice.ToJsonString();

            Assert.NotNull(json);
            Assert.True(json.Length <= 64000);
        }
Ejemplo n.º 22
0
        public void SetEnvironmentContext_ShouldSetEnvironmentContextAccordingToPassedParameters(string host, string os, string lang)
        {
            var notice = NoticeBuilder.BuildNotice();

            notice.SetEnvironmentContext(host, os, lang);

            if (string.IsNullOrEmpty(host) && string.IsNullOrEmpty(os) && string.IsNullOrEmpty(lang))
            {
                Assert.True(string.IsNullOrEmpty(notice.Context.Hostname));
                Assert.True(string.IsNullOrEmpty(notice.Context.Os));
                Assert.True(string.IsNullOrEmpty(notice.Context.Language));
            }
            else
            {
                Assert.True(string.IsNullOrEmpty(host) ? string.IsNullOrEmpty(notice.Context.Hostname) : notice.Context.Hostname.Equals(host));
                Assert.True(string.IsNullOrEmpty(os) ? string.IsNullOrEmpty(notice.Context.Os) : notice.Context.Os.Equals(os));
                Assert.True(string.IsNullOrEmpty(lang) ? string.IsNullOrEmpty(notice.Context.Language) : notice.Context.Language.Equals(lang));
            }
        }
Ejemplo n.º 23
0
        public void SetHttpContext_ContextUserInfo(string id, string name, string email)
        {
            var httpContext = new FakeHttpContext
            {
                UserName  = name,
                UserId    = id,
                UserEmail = email
            };

            var notice = NoticeBuilder.BuildNotice();

            notice.SetHttpContext(httpContext, null);

            Assert.NotNull(notice.Context);
            Assert.NotNull(notice.Context.User);
            Assert.True(string.IsNullOrEmpty(id) ? string.IsNullOrEmpty(notice.Context.User.Id) : notice.Context.User.Id.Equals(id));
            Assert.True(string.IsNullOrEmpty(name) ? string.IsNullOrEmpty(notice.Context.User.Name) : notice.Context.User.Name.Equals(name));
            Assert.True(string.IsNullOrEmpty(email) ? string.IsNullOrEmpty(notice.Context.User.Email) : notice.Context.User.Email.Equals(email));
        }
Ejemplo n.º 24
0
        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);
            }
        }
Ejemplo n.º 25
0
        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);
            }
        }