Exemple #1
0
        public async Task SendException_ReturnsEventId()
        {
            var dsn    = "<YOUR DSN HERE>";
            var client = Sentry.CreateClient(Dsn.Create(dsn));

            var value = 0;

            try
            {
                var x = 10 / value;
            }
            catch (Exception ex)
            {
                var response = await client.CaptureAsync(e => e
                                                         .SetException(ex)
                                                         .SetErrorLevel(ErrorLevel.Warning)
                                                         .AddExtraData("test", new
                {
                    IsTest = true
                })
                                                         .AddTag("test_tag", "yes"));

                Assert.NotNull(response.EventId);
            }
        }
        public async Task CapturesExceptionOnInvokation(string eventId)
        {
            var reqMock = new Mock <HttpRequest>();

            var httpMock = new Mock <HttpContext>();

            httpMock.Setup(m => m.Items)
            .Returns(new Dictionary <object, object>());

            httpMock.SetupGet(c => c.Request)
            .Returns(reqMock.Object);

            var clientMock = new Mock <SentryClient>(
                Dsn.Create(ExceptionReportingOptions.Dsn));

            clientMock.Setup(c => c.SendEventAsync(It.IsAny <SentryEventData>()))
            .ReturnsAsync(new SentryResponse {
                EventId = eventId
            })
            .Verifiable();

            var middleware = CreateMiddleware(Next_Throw, clientMock.Object);

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => middleware.Invoke(httpMock.Object));

            clientMock.Verify();
        }
        public async Task Honors429RetryAfter()
        {
            var reqMock = new Mock <HttpRequest>();

            var httpMock = new Mock <HttpContext>();

            httpMock.Setup(m => m.Items)
            .Returns(new Dictionary <object, object>());

            httpMock.SetupGet(c => c.Request)
            .Returns(reqMock.Object);

            var clientMock = new Mock <SentryClient>(
                Dsn.Create(ExceptionReportingOptions.Dsn));

            const int retryAfterMs = 500;

            var retryAfter = DateTimeOffset.UtcNow
                             + TimeSpan.FromMilliseconds(retryAfterMs);

            clientMock.Setup(m => m
                             .SendEventAsync(It.IsAny <SentryEventData>()))
            .ReturnsAsync(new SentryResponse
            {
                StatusCode = 429,
                RetryAfter = retryAfter,
            })
            .Verifiable();

            var middleware = CreateMiddleware(Next_Throw, clientMock.Object);

            // Invoke once, get "RetryAfter"

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => middleware.Invoke(httpMock.Object));

            // Invoke twice: reject

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => middleware.Invoke(httpMock.Object));

            clientMock.Verify(m => m
                              .SendEventAsync(It.IsAny <SentryEventData>()),
                              Times.Once());

            // Delay for requested period

            await Task.Delay(retryAfterMs);

            // Invoke again, responds with 429, but sends request.

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => middleware.Invoke(httpMock.Object));

            clientMock.Verify(m => m
                              .SendEventAsync(It.IsAny <SentryEventData>()),
                              Times.Exactly(2));
        }
        public async Task CapturesUserData(string eventId,
                                           string userId,
                                           string userName,
                                           string email,
                                           string ipAddress)
        {
            var user = UserDataProviderTests.MakeUser(
                Tuple.Create(ClaimTypes.NameIdentifier, userId),
                Tuple.Create(ClaimTypes.Name, userName),
                Tuple.Create(ClaimTypes.Email, email));

            var reqMock = new Mock <HttpRequest>();

            reqMock.SetupGet(r => r.Headers)
            .Returns(new HeaderDictionary());

            var connectionMock = new Mock <ConnectionInfo>();

            connectionMock.SetupGet(c => c.RemoteIpAddress)
            .Returns(IPAddress.Parse(ipAddress))
            .Verifiable();

            var httpMock = new Mock <HttpContext>();

            httpMock.SetupGet(c => c.Connection)
            .Returns(connectionMock.Object)
            .Verifiable();

            httpMock.SetupGet(c => c.User)
            .Returns(user)
            .Verifiable();

            httpMock.Setup(m => m.Items)
            .Returns(new Dictionary <object, object>());

            httpMock.SetupGet(c => c.Request)
            .Returns(reqMock.Object);

            var clientMock = new Mock <SentryClient>(
                Dsn.Create(ExceptionReportingOptions.Dsn));

            clientMock.Setup(c => c.SendEventAsync(It.Is <SentryEventData>(r
                                                                           => r.User.Id == userId &&
                                                                           r.User.UserName == userName &&
                                                                           r.User.Email == email &&
                                                                           r.User.IpAddress == ipAddress)))
            .ReturnsAsync(new SentryResponse {
                EventId = eventId
            })
            .Verifiable();

            var middleware = CreateMiddleware(Next_Throw, clientMock.Object);

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => middleware.Invoke(httpMock.Object));

            clientMock.Verify();
        }
Exemple #5
0
        public void GetCaptureUrl_NonStandardPort(int port)
        {
            var dsn = Dsn.Create(
                $"https://{PublicKey}:{SecretKey}@{Host}:{port}{Path}{ProjectId}");

            Assert.Equal(
                expected: $"https://{Host}:{port}/api/{ProjectId}/store/",
                actual: dsn.GetCaptureUrl());
        }
        public async Task SuccessfulInvoke_DoesNotSend()
        {
            var clientMock = new Mock <SentryClient>(
                Dsn.Create(ExceptionReportingOptions.Dsn));

            var middleware = CreateMiddleware(Next_Noop, clientMock.Object);

            await middleware.Invoke(null);

            clientMock.Verify(c => c.SendEventAsync(It.IsAny <SentryEventData>()), Times.Never);
        }
        public async Task CapturesRequestData(string eventId,
                                              string method,
                                              string url)
        {
            var uri = new Uri(url, UriKind.Absolute);

            var reqMock = new Mock <HttpRequest>();

            reqMock.SetupGet(r => r.Method).Returns(method);
            reqMock.SetupGet(r => r.Scheme).Returns(uri.Scheme);
            reqMock.SetupGet(r => r.Host).Returns(new HostString(uri.Host));
            reqMock.SetupGet(r => r.Path).Returns(uri.AbsolutePath);

            reqMock.SetupGet(r => r.QueryString)
            .Returns(QueryString.FromUriComponent(uri.Query));
            reqMock.SetupGet(r => r.Query)
            .Returns(new QueryCollection(QueryHelpers.ParseQuery(uri.Query)));

            var httpMock = new Mock <HttpContext>();

            httpMock.Setup(m => m.Items)
            .Returns(new Dictionary <object, object>());

            httpMock.SetupGet(c => c.Request)
            .Returns(reqMock.Object);

            var clientMock = new Mock <SentryClient>(
                Dsn.Create(ExceptionReportingOptions.Dsn));

            clientMock.Setup(c => c.SendEventAsync(It.Is <SentryEventData>(r
                                                                           => r.Request.Url == url.Split('?', StringSplitOptions.None)[0] &&
                                                                           r.Request.Method == method &&
                                                                           r.Request.QueryString == uri.Query)))
            .ReturnsAsync(new SentryResponse {
                EventId = eventId
            })
            .Verifiable();

            var middleware = CreateMiddleware(Next_Throw, clientMock.Object);

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => middleware.Invoke(httpMock.Object));

            clientMock.Verify();
        }
Exemple #8
0
        public void SentryDsn_ContainsExpectedValues(string dsnValue)
        {
            var dsn    = Dsn.Create(dsnValue);
            var auth   = SentryAuth.Issue(dsn, IssuedAt);
            var values = SentryAuthHeader.GetValues(auth);

            Assert.Contains($"Sentry sentry_version={auth.SentryVersion}", values);
            Assert.Contains($"sentry_timestamp={auth.Timestamp}", values);
            Assert.Contains($"sentry_key={auth.PublicKey}", values);

            if (dsn.GetSecretKey() != null)
            {
                Assert.Contains($"sentry_secret={auth.SecretKey}", values);
            }
            else
            {
                Assert.DoesNotContain("sentry_secret", values);
            }

            Assert.Contains($"sentry_client={auth.ClientVersion}", values);
        }
Exemple #9
0
 public void GetCaptureUrl(string dsn)
 => Assert.Equal(
     expected: $"https://{Host}/api/{ProjectId}/store/",
     actual: Dsn.Create(dsn).GetCaptureUrl());
Exemple #10
0
 public void CreateClient_IsSentryHttpClient()
 => Assert.True(Sentry.CreateClient(Dsn.Create(Dsns.SentryDsn)) is SentryHttpClient);
Exemple #11
0
 public void GetPath(string dsn)
 => Assert.Equal(Path, Dsn.Create(dsn).GetPath());
Exemple #12
0
 public void SentryDsn_HasNullSecretKey()
 => Assert.Null(Dsn.Create(SentryDsn).GetSecretKey());
Exemple #13
0
 public void LegacyDsn_GetSecretKey()
 => Assert.Equal(SecretKey,
                 Dsn.Create(LegacyDsn).GetSecretKey());
Exemple #14
0
 public void GetPublicKey(string dsn)
 => Assert.Equal(PublicKey, Dsn.Create(dsn).GetPublicKey());
Exemple #15
0
 public void CreateThrows_ForNullDsn()
 => Assert.Throws <ArgumentNullException>(()
                                          => Dsn.Create(null));
Exemple #16
0
 public void GetProjectId(string dsn)
 => Assert.Equal(ProjectId, Dsn.Create(dsn).GetProjectId());
Exemple #17
0
 public void ToString_IsOriginalString(string dsn)
 => Assert.Equal(dsn, Dsn.Create(dsn).ToString());
Exemple #18
0
 private static Dsn GetDsn(SentryOptions options)
 => !string.IsNullOrEmpty(options.Dsn)
         ? Dsn.Create(options.Dsn)
         : null;
Exemple #19
0
 public Dsn GetDsn()
 => Dsn.Create(SentryOptions.Dsn);