This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d650a3
commit 4af5a33
Showing
6 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/Duende.Bff.Blazor.Client.UnitTests/AntiforgeryHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using NSubstitute; | ||
using Shouldly; | ||
|
||
namespace Duende.Bff.Blazor.Client.UnitTests; | ||
|
||
public class AntiforgeryHandlerTests | ||
{ | ||
[Fact] | ||
public async Task Adds_expected_header() | ||
{ | ||
var sut = new TestAntiforgeryHandler() | ||
{ | ||
InnerHandler = Substitute.For<HttpMessageHandler>() | ||
}; | ||
|
||
var request = new HttpRequestMessage(); | ||
|
||
await sut.SendAsync(request, CancellationToken.None); | ||
|
||
request.Headers.ShouldContain(h => h.Key == "X-CSRF" && h.Value.Contains("1")); | ||
} | ||
} | ||
|
||
public class TestAntiforgeryHandler : AntiforgeryHandler | ||
{ | ||
public new Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
return base.SendAsync(request, cancellationToken); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
test/Duende.Bff.Blazor.Client.UnitTests/Duende.Bff.Blazor.Client.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NSubstitute" Version="5.1.0" /> | ||
<PackageReference Include="Shouldly" Version="4.2.1" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../../src/Duende.Bff.Blazor.Client/Duende.Bff.Blazor.Client.csproj"></ProjectReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
106 changes: 106 additions & 0 deletions
106
test/Duende.Bff.Blazor.Client.UnitTests/ServiceCollectionExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NSubstitute; | ||
using Shouldly; | ||
|
||
namespace Duende.Bff.Blazor.Client.UnitTests; | ||
|
||
public class ServiceCollectionExtensionsTests | ||
{ | ||
[Theory] | ||
[InlineData("https://example.com/", "https://example.com/")] | ||
[InlineData("https://example.com", "https://example.com/")] | ||
public void When_base_address_option_is_set_AddBffBlazorClient_configures_HttpClient_base_address(string configuredRemoteAddress, string expectedBaseAddress) | ||
{ | ||
var sut = new ServiceCollection(); | ||
sut.AddBffBlazorClient(); | ||
sut.Configure<BffBlazorOptions>(opt => | ||
{ | ||
opt.StateProviderBaseAddress = configuredRemoteAddress; | ||
}); | ||
|
||
|
||
var sp = sut.BuildServiceProvider(); | ||
var httpClientFactory = sp.GetService<IHttpClientFactory>(); | ||
var httpClient = httpClientFactory?.CreateClient(nameof(BffClientAuthenticationStateProvider)); | ||
httpClient.ShouldNotBeNull(); | ||
httpClient.BaseAddress.ShouldNotBeNull(); | ||
httpClient.BaseAddress.AbsoluteUri.ShouldBe(expectedBaseAddress); | ||
} | ||
|
||
[Fact] | ||
public void When_base_address_option_is_default_AddBffBlazorClient_configures_HttpClient_base_address_from_host_env() | ||
{ | ||
var expectedBaseAddress = "https://example.com/"; | ||
|
||
var sut = new ServiceCollection(); | ||
sut.AddBffBlazorClient(); | ||
var env = Substitute.For<IWebAssemblyHostEnvironment>(); | ||
env.BaseAddress.Returns(expectedBaseAddress); | ||
sut.AddSingleton(env); | ||
|
||
var sp = sut.BuildServiceProvider(); | ||
var httpClientFactory = sp.GetService<IHttpClientFactory>(); | ||
var httpClient = httpClientFactory?.CreateClient(nameof(BffClientAuthenticationStateProvider)); | ||
httpClient.ShouldNotBeNull(); | ||
httpClient.BaseAddress.ShouldNotBeNull(); | ||
httpClient.BaseAddress.AbsoluteUri.ShouldBe(expectedBaseAddress); | ||
} | ||
|
||
[Theory] | ||
[InlineData("https://example.com/", "remote-apis", "https://example.com/remote-apis/")] | ||
[InlineData("https://example.com/", null, "https://example.com/remote-apis/")] | ||
[InlineData("https://example.com", null, "https://example.com/remote-apis/")] | ||
[InlineData("https://example.com", "custom/route/to/apis", "https://example.com/custom/route/to/apis/")] | ||
[InlineData("https://example.com/with/base/path", "custom/route/to/apis", "https://example.com/with/base/path/custom/route/to/apis/")] | ||
[InlineData("https://example.com/with/base/path/", "custom/route/to/apis", "https://example.com/with/base/path/custom/route/to/apis/")] | ||
[InlineData("https://example.com/with/base/path", "/custom/route/to/apis", "https://example.com/with/base/path/custom/route/to/apis/")] | ||
[InlineData("https://example.com/with/base/path/", "/custom/route/to/apis", "https://example.com/with/base/path/custom/route/to/apis/")] | ||
[InlineData("https://example.com/with/base/path", null, "https://example.com/with/base/path/remote-apis/")] | ||
public void AddRemoteApiHttpClient_configures_HttpClient_base_address(string configuredRemoteAddress, string? configuredRemotePath, string expectedBaseAddress) | ||
{ | ||
var sut = new ServiceCollection(); | ||
sut.AddBffBlazorClient(); | ||
sut.AddRemoteApiHttpClient("clientName"); | ||
sut.Configure<BffBlazorOptions>(opt => | ||
{ | ||
if (configuredRemoteAddress != null) | ||
{ | ||
opt.RemoteApiBaseAddress = configuredRemoteAddress; | ||
} | ||
if (configuredRemotePath != null) | ||
{ | ||
opt.RemoteApiPath = configuredRemotePath; | ||
} | ||
}); | ||
|
||
|
||
var sp = sut.BuildServiceProvider(); | ||
var httpClientFactory = sp.GetService<IHttpClientFactory>(); | ||
var httpClient = httpClientFactory?.CreateClient("clientName"); | ||
httpClient.ShouldNotBeNull(); | ||
httpClient.BaseAddress.ShouldNotBeNull(); | ||
httpClient.BaseAddress.AbsoluteUri.ShouldBe(expectedBaseAddress); | ||
} | ||
|
||
[Fact] | ||
public void When_base_address_option_is_default_AddRemoteApiHttpClient_configures_HttpClient_base_address_from_host_env() | ||
{ | ||
var hostBaseAddress = "https://example.com/"; | ||
var expectedBaseAddress = "https://example.com/remote-apis/"; | ||
|
||
var sut = new ServiceCollection(); | ||
sut.AddBffBlazorClient(); | ||
sut.AddRemoteApiHttpClient("clientName"); | ||
var env = Substitute.For<IWebAssemblyHostEnvironment>(); | ||
env.BaseAddress.Returns(hostBaseAddress); | ||
sut.AddSingleton(env); | ||
|
||
var sp = sut.BuildServiceProvider(); | ||
var httpClientFactory = sp.GetService<IHttpClientFactory>(); | ||
var httpClient = httpClientFactory?.CreateClient("clientName"); | ||
httpClient.ShouldNotBeNull(); | ||
httpClient.BaseAddress.ShouldNotBeNull(); | ||
httpClient.BaseAddress.AbsoluteUri.ShouldBe(expectedBaseAddress); | ||
} | ||
} |