fix AspNetCore tests (#2414)
This commit is contained in:
Родитель
9137120a09
Коммит
fb0e8a4d94
|
@ -44,11 +44,11 @@
|
|||
return contextAccessor;
|
||||
}
|
||||
|
||||
public static HttpContextAccessor CreateHttpContextAccessorWithoutRequest(HttpContextStub httpContextStub, RequestTelemetry requestTelemetry = null)
|
||||
public static HttpContextAccessor CreateHttpContextAccessorWithoutRequest(HttpContext httpContext, RequestTelemetry requestTelemetry = null)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
var contextAccessor = new HttpContextAccessor { HttpContext = httpContextStub };
|
||||
var contextAccessor = new HttpContextAccessor { HttpContext = httpContext };
|
||||
|
||||
services.AddSingleton<IHttpContextAccessor>(contextAccessor);
|
||||
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNetCore.Tests.Helpers
|
||||
{
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
/// <summary>
|
||||
/// Class that is used in unit tests and allows to override main HttpContext properties.
|
||||
/// </summary>
|
||||
public class HttpContextStub : DefaultHttpContext
|
||||
{
|
||||
public Func<HttpRequest> OnRequestGetter = () => null;
|
||||
|
||||
public override HttpRequest Request => this.OnRequestGetter();
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
namespace Microsoft.ApplicationInsights.AspNetCore.Tests.Helpers
|
||||
{
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Internal;
|
||||
|
||||
public class HttpRequestStub : DefaultHttpRequest
|
||||
{
|
||||
public Func<IHeaderDictionary> OnGetHeaders = () => null;
|
||||
|
||||
public HttpRequestStub(HttpContext context) : base(context)
|
||||
{
|
||||
}
|
||||
|
||||
public override IHeaderDictionary Headers => this.OnGetHeaders();
|
||||
}
|
||||
}
|
|
@ -110,7 +110,7 @@
|
|||
{
|
||||
return new HttpContextAccessor
|
||||
{
|
||||
HttpContext = new HttpContextStub
|
||||
HttpContext = new DefaultHttpContext
|
||||
{
|
||||
User = new GenericPrincipal(new IdentityStub() { Name = name, IsAuthenticated = isAuthenticated }, new string[0])
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<!-- TargetFrameworks are defined in Test.props, but can be overridden here if needed. -->
|
||||
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
[Fact]
|
||||
public void InitializeDoesNotThrowIfRequestIsUnavailable()
|
||||
{
|
||||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(new HttpContextStub(), new RequestTelemetry());
|
||||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(new DefaultHttpContext(), new RequestTelemetry());
|
||||
|
||||
var initializer = new ClientIpHeaderTelemetryInitializer(contextAccessor);
|
||||
|
||||
|
@ -51,10 +51,7 @@
|
|||
[Fact]
|
||||
public void InitializeDoesNotThrowIfHeaderCollectionIsUnavailable()
|
||||
{
|
||||
var httpContext = new HttpContextStub();
|
||||
httpContext.OnRequestGetter = () => new HttpRequestStub(httpContext);
|
||||
|
||||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(httpContext, new RequestTelemetry());
|
||||
var contextAccessor = HttpContextAccessorHelper.CreateHttpContextAccessorWithoutRequest(new DefaultHttpContext(), new RequestTelemetry());
|
||||
|
||||
var initializer = new ClientIpHeaderTelemetryInitializer(contextAccessor);
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче