Skip to content

Commit

Permalink
Merge branch 'main' into dispose-of-client-should-only-flush
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 5, 2022
2 parents a27fcec + 0c62e9a commit 3c3968b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 78 deletions.
5 changes: 5 additions & 0 deletions src/Sentry.AspNet/Internal/SystemWebVersionLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ internal static class SystemWebVersionLocator
return release;
}

return Resolve(context);
}

internal static string? Resolve(HttpContext context)
{
if (context.ApplicationInstance?.GetType() is { } type)
{
// Usually the type is ASP.global_asax and the BaseType is the Web Application.
Expand Down
3 changes: 1 addition & 2 deletions test/Sentry.AspNet.Tests/ApiApprovalTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Sentry.AspNet;
using Sentry.Tests;

namespace Sentry.AspNet.Tests;

[UsesVerify]
public class ApiApprovalTests
{
Expand Down
17 changes: 17 additions & 0 deletions test/Sentry.AspNet.Tests/HttpContextBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Web;

public static class HttpContextBuilder
{
public static HttpContext Build(int responseStatusCode = 200)
{
return new HttpContext(
new HttpRequest("test", "http://test/the/path", null),
new HttpResponse(TextWriter.Null)
{
StatusCode = responseStatusCode
})
{
ApplicationInstance = new HttpApplication()
};
}
}
39 changes: 11 additions & 28 deletions test/Sentry.AspNet.Tests/HttpContextExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
using System.Web;

namespace Sentry.AspNet.Tests;
using Sentry.AspNet;

public class HttpContextExtensionsTests
{
[Fact]
public void StartSentryTransaction_CreatesValidTransaction()
{
// Arrange
var context = new HttpContext(
new HttpRequest("foo", "https://localhost/person/13", "details=true")
{
RequestType = "GET"
},
new HttpResponse(TextWriter.Null));
var context = HttpContextBuilder.Build();

// Act
var transaction = context.StartSentryTransaction();

// Assert
transaction.Name.Should().Be("GET /person/13");
transaction.Name.Should().Be("GET /the/path");
transaction.Operation.Should().Be("http.server");
}

[Fact]
public void StartSentryTransaction_BindsToScope()
{
// Arrange
using var _ = SentrySdk.UseHub(new Sentry.Internal.Hub(
using var _ = SentrySdk.UseHub(new Hub(
new SentryOptions { Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647" },
Substitute.For<ISentryClient>()
));

var context = new HttpContext(
new HttpRequest("foo", "https://localhost/person/13", "details=true")
{
RequestType = "GET"
},
new HttpResponse(TextWriter.Null));
var context = HttpContextBuilder.Build();

// Act
var transaction = context.StartSentryTransaction();
Expand All @@ -51,20 +39,15 @@ public void StartSentryTransaction_BindsToScope()
public void FinishSentryTransaction_FinishesTransaction()
{
// Arrange
using var _ = SentrySdk.UseHub(new Sentry.Internal.Hub(
new SentryOptions { Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647" },
using var _ = SentrySdk.UseHub(new Hub(
new SentryOptions
{
Dsn = "https://d4d82fc1c2c4032a83f3a29aa3a3aff@fake-sentry.io:65535/2147483647"
},
Substitute.For<ISentryClient>()
));

var context = new HttpContext(
new HttpRequest("foo", "https://localhost/person/13", "details=true")
{
RequestType = "GET"
},
new HttpResponse(TextWriter.Null)
{
StatusCode = 404
});
var context = HttpContextBuilder.Build(404);

// Act
var transaction = context.StartSentryTransaction();
Expand Down
23 changes: 23 additions & 0 deletions test/Sentry.AspNet.Tests/HttpContextTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Web;

public abstract class HttpContextTest :
IDisposable
{
private HttpContext _context;

protected HttpContextTest()
{
HttpContext.Current = Context = HttpContextBuilder.Build();
}

public HttpContext Context
{
get => _context;
set => HttpContext.Current = _context = value;
}

public void Dispose()
{
HttpContext.Current = null;
}
}
2 changes: 0 additions & 2 deletions test/Sentry.AspNet.Tests/Internal/SentryScopeManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Sentry.AspNet.Internal;

namespace Sentry.AspNet.Tests.Internal;

public class SentryScopeManagerTests
{
private class Fixture
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
using System.Web;
using Sentry.AspNet.Internal;

namespace Sentry.AspNet.Tests.Internal;

public class SystemWebRequestEventProcessorTests
public class SystemWebRequestEventProcessorTests :
HttpContextTest
{
private class Fixture
{
public IRequestPayloadExtractor RequestPayloadExtractor { get; set; } = Substitute.For<IRequestPayloadExtractor>();
public SentryOptions SentryOptions { get; set; } = new();
public object MockBody { get; set; } = new();
public HttpContext HttpContext { get; set; }

public Fixture()
{
_ = RequestPayloadExtractor.ExtractPayload(Arg.Any<IHttpRequest>()).Returns(MockBody);
HttpContext = new HttpContext(new HttpRequest("test", "http://test", null), new HttpResponse(new StringWriter()));
}

public SystemWebRequestEventProcessor GetSut()
{
HttpContext.Current = HttpContext;
return new SystemWebRequestEventProcessor(RequestPayloadExtractor, SentryOptions);
}
}
Expand Down Expand Up @@ -52,7 +47,7 @@ public void Process_DefaultFixture_ReadsMockBody()
[Fact]
public void Process_NoHttpContext_NoRequestData()
{
_fixture.HttpContext = null;
Context = null;
var expected = new SentryEvent();

var sut = _fixture.GetSut();
Expand Down
37 changes: 7 additions & 30 deletions test/Sentry.AspNet.Tests/Internal/SystemWebVersionLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
using System.Reflection;
using System.Web;
using Sentry.AspNet.Internal;

namespace Sentry.AspNet.Tests.Internal;

public class SystemWebVersionLocatorTests
public class SystemWebVersionLocatorTests :
HttpContextTest
{
private class Fixture
{
public HttpContext HttpContext { get; set; }

public Fixture()
{
HttpContext = new HttpContext(new HttpRequest("test", "http://test", null), new HttpResponse(new StringWriter()));
}

public Assembly GetSut()
{
HttpContext.Current = HttpContext;
HttpContext.Current.ApplicationInstance = new HttpApplication();
return HttpContext.Current.ApplicationInstance.GetType().Assembly;
}
}

private readonly Fixture _fixture = new();

[Fact]
public void GetCurrent_GetEntryAssemblyNull_HttpApplicationAssembly()
{
_fixture.HttpContext.ApplicationInstance = new HttpApplication();
var sut = _fixture.GetSut();
var expected = ApplicationVersionLocator.GetCurrent(sut);
var expected = ApplicationVersionLocator.GetCurrent(typeof(HttpApplication).Assembly);

var actual = SystemWebVersionLocator.Resolve((string)null, _fixture.HttpContext);
var actual = SystemWebVersionLocator.Resolve((string)null, Context);

Assert.Equal(expected, actual);
}

[Fact]
public void GetCurrent_GetEntryAssemblySet_HttpApplicationAssembly()
public void HttpApplicationAssembly_VersionParsing()
{
var expected = ApplicationVersionLocator.GetCurrent();
var expected = ApplicationVersionLocator.GetCurrent(typeof(HttpApplication).Assembly);

var actual = SystemWebVersionLocator.Resolve(new SentryOptions(), _fixture.HttpContext);
var actual = SystemWebVersionLocator.Resolve(Context);

Assert.Equal(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Sentry.AspNet;
using Sentry.AspNet.Internal;

namespace Sentry.AspNet.Tests;

public class SentryAspNetOptionsExtensionsTests
public class SentryAspNetOptionsExtensionsTests :
HttpContextTest
{
[Fact]
public void AddAspNet_EventProcessorsContainBodyExtractor()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Web;

namespace Sentry.AspNet.Tests;
using Sentry.AspNet;

public class SentryHttpServerUtilityExtensionsTests
{
Expand All @@ -27,7 +25,7 @@ public void CaptureLastError_WithError_UnhandledErrorCaptured()
var hub = _fixture.GetSut();
var exception = new Exception();

var context = new HttpContext(new HttpRequest("", "http://test", null), new HttpResponse(new StringWriter()));
var context = HttpContextBuilder.Build();
context.AddError(exception);

// Act
Expand All @@ -45,7 +43,7 @@ public void CaptureLastError_WithoutError_DoNothing()
{
// Arrange
var hub = _fixture.GetSut();
var context = new HttpContext(new HttpRequest("", "http://test", null), new HttpResponse(new StringWriter()));
var context = HttpContextBuilder.Build();

// Act
var receivedId = context.Server.CaptureLastError(hub);
Expand Down

0 comments on commit 3c3968b

Please sign in to comment.