This commit is contained in:
Timothy Mothra 2021-09-16 09:50:19 -07:00 коммит произвёл GitHub
Родитель 9137120a09
Коммит fb0e8a4d94
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 6 добавлений и 40 удалений

Просмотреть файл

@ -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);