Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<PackageReference Include="BootstrapBlazor.IconPark" Version="9.0.2" />
<PackageReference Include="BootstrapBlazor.ImageCropper" Version="9.0.0" />
<PackageReference Include="BootstrapBlazor.IP2Region" Version="9.0.1" />
<PackageReference Include="BootstrapBlazor.JuHeIpLocatorProvider" Version="9.0.0-beta01" />
<PackageReference Include="BootstrapBlazor.Live2DDisplay" Version="9.0.1" />
<PackageReference Include="BootstrapBlazor.Markdown" Version="9.0.1" />
<PackageReference Include="BootstrapBlazor.MaterialDesign" Version="9.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public static IServiceCollection AddBootstrapBlazorServices(this IServiceCollect
// 增加离线 IP 定位服务
services.AddBootstrapBlazorIP2RegionfService();

// 增加 JuHe 定位服务
services.AddBootstrapBlazorJuHeIpLocatorService();

// 增加 PetaPoco ORM 数据服务操作类
// 需要时打开下面代码
//services.AddPetaPoco(option =>
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.3.1-beta23</Version>
<Version>9.3.1-beta24</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv

// IP 地理位置定位服务
services.TryAddSingleton<IIpLocatorFactory, DefaultIpLocatorFactory>();
services.AddSingleton<IIpLocatorProvider, JuHeIpLocatorProvider>();
services.AddSingleton<IIpLocatorProvider, BaiduIpLocatorProvider>();
services.AddSingleton<IIpLocatorProvider, BaiduIpLocatorProviderV2>();

Expand Down
203 changes: 0 additions & 203 deletions src/BootstrapBlazor/Services/IPLocator/JuHeIpLocatorProvider.cs

This file was deleted.

93 changes: 0 additions & 93 deletions test/UnitTest/Services/IpLocatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,6 @@ public async Task BaiduIpLocatorProviderV2_Ok()
Assert.Equal("省份城市区县 测试", result);
}

[Fact]
public async Task JuHeIpLocatorProvider_Ok()
{
var factory = Context.Services.GetRequiredService<IHttpClientFactory>();
var option = Context.Services.GetRequiredService<IOptions<BootstrapBlazorOptions>>();
var logger = Context.Services.GetRequiredService<ILogger<JuHeIpLocatorProvider>>();
var configuration = Context.Services.GetRequiredService<IConfiguration>();
var provider = new MockJuHeProvider(factory, configuration, option, logger);

var result = await provider.Locate("127.0.0.1");
Assert.Equal("localhost", result);

result = await provider.Locate("");
Assert.Equal("localhost", result);

// 河南省漯河市 移动
result = await provider.Locate("223.91.188.112");
Assert.Equal("省份城市区县 测试", result);
}

[Fact]
public void Factory_Ok()
{
Expand Down Expand Up @@ -111,12 +91,6 @@ public async Task Fetch_Error()
var cancelPprovider = new MockProviderFetchCancelError(factory, option, logger);
result = await cancelPprovider.Locate("223.91.188.112");
Assert.Null(result);

var configuration = Context.Services.GetRequiredService<IConfiguration>();
var juHeLogger = Context.Services.GetRequiredService<ILogger<JuHeIpLocatorProvider>>();
var juHeProvider = new MockProviderJuHeFetchError(factory, configuration, option, juHeLogger);
result = await juHeProvider.Locate("223.91.188.112");
Assert.Null(result);
}

[Fact]
Expand All @@ -134,32 +108,6 @@ public async Task Fetch_Result_Fail()
var providerV2 = new MockBaiduProviderV2(factory, option, loggerV2);
result = await providerV2.Locate("223.91.188.112");
Assert.Null(result);

var configuration = Context.Services.GetRequiredService<IConfiguration>();
var loggerJuHe = Context.Services.GetRequiredService<ILogger<JuHeIpLocatorProvider>>();
var providerJuHe = new MockJuHeNullProvider(factory, configuration, option, loggerJuHe);
result = await providerJuHe.Locate("223.91.188.112");
Assert.Null(result);

var providerJuHeFailed = new MockJuHeFailProvider(factory, configuration, option, loggerJuHe);
result = await providerJuHeFailed.Locate("223.91.188.112");
Assert.Null(result);
}

[Fact]
public async Task JuHe_Section_Error()
{
var factory = Context.Services.GetRequiredService<IHttpClientFactory>();
var option = Context.Services.GetRequiredService<IOptions<BootstrapBlazorOptions>>();
var logger = Context.Services.GetRequiredService<ILogger<JuHeIpLocatorProvider>>();
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(new Dictionary<string, string?>
{
["JuHe:IpLocatorKey"] = ""
});
var configuration = builder.Build();
var provider = new MockJuHeNullProvider(factory, configuration, option, logger);
await Assert.ThrowsAsync<InvalidOperationException>(async () => await provider.Locate("223.91.188.112"));
}

class MockProviderFetchError(IHttpClientFactory httpClientFactory, IOptions<BootstrapBlazorOptions> option, ILogger<MockProviderFetchError> logger) : BaiduIpLocatorProvider(httpClientFactory, option, logger)
Expand All @@ -172,11 +120,6 @@ class MockProviderFetchCancelError(IHttpClientFactory httpClientFactory, IOption
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token) => throw new TaskCanceledException();
}

class MockProviderJuHeFetchError(IHttpClientFactory httpClientFactory, IConfiguration configuration, IOptions<BootstrapBlazorOptions> option, ILogger<JuHeIpLocatorProvider> logger) : JuHeIpLocatorProvider(httpClientFactory, configuration, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token) => throw new InvalidOperationException();
}

class MockBaiduProvider(IHttpClientFactory httpClientFactory, IOptions<BootstrapBlazorOptions> option, ILogger<MockBaiduProvider> logger) : BaiduIpLocatorProvider(httpClientFactory, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
Expand All @@ -195,24 +138,6 @@ class MockBaiduProviderV2(IHttpClientFactory httpClientFactory, IOptions<Bootstr
}
}

class MockJuHeNullProvider(IHttpClientFactory httpClientFactory, IConfiguration configuration, IOptions<BootstrapBlazorOptions> option, ILogger<JuHeIpLocatorProvider> logger) : JuHeIpLocatorProvider(httpClientFactory, configuration, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
{
client = new HttpClient(new MockHttpNullMessageHandler(), true);
return base.Fetch(url, client, token);
}
}

class MockJuHeFailProvider(IHttpClientFactory httpClientFactory, IConfiguration configuration, IOptions<BootstrapBlazorOptions> option, ILogger<JuHeIpLocatorProvider> logger) : JuHeIpLocatorProvider(httpClientFactory, configuration, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
{
client = new HttpClient(new MockHttpFailedMessageHandlerJuHe(), true);
return base.Fetch(url, client, token);
}
}

class MockProvider(IHttpClientFactory httpClientFactory, IOptions<BootstrapBlazorOptions> option, ILogger<MockProvider> logger) : BaiduIpLocatorProvider(httpClientFactory, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
Expand All @@ -231,24 +156,6 @@ class MockProviderV2(IHttpClientFactory httpClientFactory, IOptions<BootstrapBla
}
}

class MockJuHeProvider(IHttpClientFactory httpClientFactory, IConfiguration configuration, IOptions<BootstrapBlazorOptions> option, ILogger<JuHeIpLocatorProvider> logger) : JuHeIpLocatorProvider(httpClientFactory, configuration, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
{
client = new HttpClient(new MockHttpSuccessMessageHandlerJuHe(), true);
return base.Fetch(url, client, token);
}
}

class MockJuHeFailedProvider(IHttpClientFactory httpClientFactory, IConfiguration configuration, IOptions<BootstrapBlazorOptions> option, ILogger<JuHeIpLocatorProvider> logger) : JuHeIpLocatorProvider(httpClientFactory, configuration, option, logger)
{
protected override Task<string?> Fetch(string url, HttpClient client, CancellationToken token)
{
client = new HttpClient(new MockHttpFailedMessageHandlerJuHe(), true);
return base.Fetch(url, client, token);
}
}

class MockHttpNullMessageHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
Expand Down
Loading