Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions samples/Monq.Core.HttpClientExtensions.TestApp/TestService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
Expand All @@ -12,7 +12,7 @@ public interface ITestService
Task<TestModel> TestApi();
}

public class TestService : RestHttpClientFromOptions<ServiceUriOptions>, ITestService
public class TestService : RestHttpClient, ITestService
{
/// <summary>
/// Инициализирует новый экземпляр класса <see cref="TestService" />.
Expand All @@ -29,12 +29,10 @@ public TestService(
ILoggerFactory loggerFactory,
RestHttpClientOptions configuration,
IHttpContextAccessor httpContextAccessor) :
base(optionsAccessor,
httpClient,
base(httpClient,
loggerFactory,
configuration,
httpContextAccessor,
optionsAccessor.Value.TestServiceUri)
httpContextAccessor)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Monq.Core.HttpClientExtensions.TestApp;
Expand All @@ -14,7 +14,7 @@ public interface ITestService
Task<TestModel> TestApi(string auth);
}

public class TestService : RestHttpClientFromOptions<ServiceUriOptions>, ITestService
public class TestService : RestHttpClient, ITestService
{
ILogger<TestService> _log;

Expand All @@ -33,12 +33,10 @@ public TestService(
ILoggerFactory loggerFactory,
RestHttpClientOptions configuration,
IHttpContextAccessor httpContextAccessor) :
base(optionsAccessor,
httpClient,
base(httpClient,
loggerFactory,
configuration,
httpContextAccessor,
optionsAccessor.Value.TestServiceUri)
httpContextAccessor)
{
_log = loggerFactory.CreateLogger<TestService>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Net.Http;
Expand All @@ -23,27 +23,4 @@ public RestHttpClientMock(HttpClient httpClient,

}
}

public class RestHttpClientFromOptionsMock : RestHttpClientFromOptions<ServiceOptions>
{
/// <summary>
/// Initializes a new instance of the <see cref="RestHttpClientFromOptionsMock`1"/> class.
/// </summary>
/// <param name="optionsAccessor">The options.</param>
/// <param name="httpClient">The HttpClient from http client factory.</param>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="configuration">The configuration.</param>
/// <param name="httpContextAccessor">The HTTP context accessor.</param>
/// <param name="baseUri">The base Uri of all requests. For example: http://rsm.api.monq.cloud</param>
public RestHttpClientFromOptionsMock(IOptions<ServiceOptions> optionsAccessor,
HttpClient httpClient,
ILoggerFactory loggerFactory,
RestHttpClientOptions configuration,
IHttpContextAccessor httpContextAccessor,
string baseUri)
: base(optionsAccessor, httpClient, loggerFactory, configuration, httpContextAccessor, baseUri)
{

}
}
}

This file was deleted.

27 changes: 26 additions & 1 deletion src/Monq.Core.HttpClientExtensions.Tests/RestHttpClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using IdentityModel.Client;
using IdentityModel.Client;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Monq.Core.HttpClientExtensions.Exceptions;
Expand Down Expand Up @@ -386,6 +386,31 @@ public async Task ShouldProperlyReuseHttpClientObjectInParallelWithHeaders()
Assert.Equal(2 * totalRequests, results.Where(x => x.ResultObject is not null).SelectMany(x => x.ResultObject).Count());
}

[Fact(DisplayName = "Проверка установки Bearer token из HttpContextAccessor.")]
public void ShouldProperlySetBearerTokenFromRequest()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers.Add("Authorization", "Bearer token355");

var client = new HttpClient(CreateDefaultResponseHandler(HttpStatusCode.Unauthorized, "{}"));

var httpService = CreateRestHttpClient(client, httpContext);

Assert.Equal("token355", client.DefaultRequestHeaders?.Authorization?.Parameter);
Assert.Equal("Bearer", client.DefaultRequestHeaders?.Authorization?.Scheme);
}

[Fact(DisplayName = "Проверка НЕустановки Bearer token из HttpContextAccessor.")]
public void ShouldNotSetBearerTokenFromRequestIfBearerNotValid()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Headers.Add("Authorization", "Bearetoken355");
var client = new HttpClient(CreateDefaultResponseHandler(HttpStatusCode.Unauthorized, "{}"));

var httpService = CreateRestHttpClient(client, httpContext);
Assert.Null(client.DefaultRequestHeaders.Authorization);
}

RestHttpClientMock CreateRestHttpClient(HttpClient httpClient,
HttpContext? httpContext = null,
RestHttpClientOptions? configuration = null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>6.0.2</Version>
<Version>6.1.0</Version>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
Expand Down
5 changes: 0 additions & 5 deletions src/Monq.Core.HttpClientExtensions/RestHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ public RestHttpClient(HttpClient httpClient,

_log = loggerFactory.CreateLogger<RestHttpClient>();

// To reuse the HttpClient instance, we will use the cancellation token to manage timeouts.
// To do this, you need to set the main timeout to the maximum value,
// because it will override the value specified in the cancellation token.
_httpClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan;

if (HttpContextAccessor?.HttpContext is not null
&& HttpContextAccessor.HttpContext.Request.Headers.TryGetValue(AuthorizationHeader, out var authorizeHeader)
&& !string.IsNullOrEmpty(authorizeHeader))
Expand Down
71 changes: 0 additions & 71 deletions src/Monq.Core.HttpClientExtensions/RestHttpClientFromOptions.cs

This file was deleted.