From 147e6109420ba48090040a49ba2042da38651342 Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 14 Oct 2026 09:10:24 +0800 Subject: [PATCH] test(maxmindgeoip2): Cover the empty GeoIP2 reader and the external database options This also repairs the LINGYUN.Abp.MaxMindGeoIP2.Tests build: the previous commit removed AbpMaxMindGeoIP2Module.DefaultDatabaseFile while this project still referenced it. --- .../AbpMaxMindGeoIP2TestModule.cs | 12 +++ .../EmptyGeoIP2DatabaseReader_Tests.cs | 98 +++++++++++++++++++ .../MaxMindGeoIP2/IPLocationResolverTest.cs | 10 +- 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/EmptyGeoIP2DatabaseReader_Tests.cs diff --git a/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/AbpMaxMindGeoIP2TestModule.cs b/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/AbpMaxMindGeoIP2TestModule.cs index 590a1f6e9..2f3c755b7 100644 --- a/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/AbpMaxMindGeoIP2TestModule.cs +++ b/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/AbpMaxMindGeoIP2TestModule.cs @@ -1,5 +1,7 @@ using LINGYUN.Abp.IP.Location; using LINGYUN.Abp.Tests; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; namespace LINGYUN.Abp.MaxMindGeoIP2; @@ -9,6 +11,16 @@ namespace LINGYUN.Abp.MaxMindGeoIP2; typeof(AbpTestsBaseModule))] public class AbpMaxMindGeoIP2TestModule : AbpModule { + private const string UserSecretsId = "4569336C-3EC4-4E55-A9B7-156A1D7E051E"; + + public override void PreConfigureServices(ServiceConfigurationContext context) + { + context.Services.ReplaceConfiguration(ConfigurationHelper.BuildConfiguration(builderAction: builder => + { + builder.AddUserSecrets(UserSecretsId); + })); + } + public override void ConfigureServices(ServiceConfigurationContext context) { Configure(options => diff --git a/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/EmptyGeoIP2DatabaseReader_Tests.cs b/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/EmptyGeoIP2DatabaseReader_Tests.cs new file mode 100644 index 000000000..d22eee4c4 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/EmptyGeoIP2DatabaseReader_Tests.cs @@ -0,0 +1,98 @@ +using MaxMind.Db; +using MaxMind.GeoIP2; +using MaxMind.GeoIP2.Exceptions; +using Shouldly; +using System.Net; +using Xunit; + +namespace LINGYUN.Abp.MaxMindGeoIP2; + +/// +/// 空 GeoIP2 解读器 +/// +/// +/// 未提供离线库文件时使用, 相当于解读了一份空内容: 所有查询都返回"无数据" +/// +public class EmptyGeoIP2DatabaseReader_Tests +{ + private readonly IGeoIP2DatabaseReader _reader = new EmptyGeoIP2DatabaseReader(); + + [Fact] + public void Should_Not_Find_Any_City() + { + _reader.TryCity(IPAddress.Parse("223.5.5.5"), out var response).ShouldBeFalse(); + response.ShouldBeNull(); + + _reader.TryCity("223.5.5.5", out var stringResponse).ShouldBeFalse(); + stringResponse.ShouldBeNull(); + } + + [Fact] + public void Should_Not_Find_Any_Data() + { + _reader.TryCountry(IPAddress.Parse("223.5.5.5"), out var country).ShouldBeFalse(); + country.ShouldBeNull(); + + _reader.TryAnonymousIP(IPAddress.Parse("223.5.5.5"), out var anonymousIp).ShouldBeFalse(); + anonymousIp.ShouldBeNull(); + + _reader.TryAnonymousPlus(IPAddress.Parse("223.5.5.5"), out var anonymousPlus).ShouldBeFalse(); + anonymousPlus.ShouldBeNull(); + + _reader.TryAsn(IPAddress.Parse("223.5.5.5"), out var asn).ShouldBeFalse(); + asn.ShouldBeNull(); + + _reader.TryConnectionType(IPAddress.Parse("223.5.5.5"), out var connectionType).ShouldBeFalse(); + connectionType.ShouldBeNull(); + + _reader.TryDomain(IPAddress.Parse("223.5.5.5"), out var domain).ShouldBeFalse(); + domain.ShouldBeNull(); + + _reader.TryEnterprise(IPAddress.Parse("223.5.5.5"), out var enterprise).ShouldBeFalse(); + enterprise.ShouldBeNull(); + + _reader.TryIsp(IPAddress.Parse("223.5.5.5"), out var isp).ShouldBeFalse(); + isp.ShouldBeNull(); + } + + /// + /// 非 Try 方法与真实解读器未命中时的行为一致: 抛出 AddressNotFoundException + /// + [Fact] + public void Should_Throw_Address_Not_Found() + { + Should.Throw(() => _reader.City(IPAddress.Parse("223.5.5.5"))); + Should.Throw(() => _reader.City("223.5.5.5")); + Should.Throw(() => _reader.Country(IPAddress.Parse("223.5.5.5"))); + Should.Throw(() => _reader.Country("223.5.5.5")); + Should.Throw(() => _reader.Asn(IPAddress.Parse("223.5.5.5"))); + Should.Throw(() => _reader.Isp("223.5.5.5")); + Should.Throw(() => _reader.Enterprise(IPAddress.Parse("223.5.5.5"))); + Should.Throw(() => _reader.AnonymousIP("223.5.5.5")); + Should.Throw(() => _reader.AnonymousPlus("223.5.5.5")); + Should.Throw(() => _reader.ConnectionType("223.5.5.5")); + Should.Throw(() => _reader.Domain("223.5.5.5")); + } + + /// + /// 空解读器不提供数据库元数据 + /// + [Fact] + public void Should_Not_Provide_Metadata() + { + _reader.Metadata.ShouldBeNull(); + } + + /// + /// 选项: 未指定数据库文件时判定为不存在 + /// + [Fact] + public void Should_Check_Database_File() + { + var options = new AbpMaxMindGeoIP2Options(); + options.DatabaseFileExists().ShouldBeFalse(); + + options.DatabaseFile = "not-exists.mmdb"; + options.DatabaseFileExists().ShouldBeFalse(); + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/IPLocationResolverTest.cs b/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/IPLocationResolverTest.cs index d8ecaf8f8..f4a2c5936 100644 --- a/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/IPLocationResolverTest.cs +++ b/aspnet-core/tests/LINGYUN.Abp.MaxMindGeoIP2.Tests/LINGYUN/Abp/MaxMindGeoIP2/IPLocationResolverTest.cs @@ -13,14 +13,18 @@ namespace LINGYUN.Abp.MaxMindGeoIP2; public class IPLocationResolverTest : AbpMaxMindGeoIP2TestBase { /// - /// 默认选项: 使用内置 GeoLite2-City.mmdb、内存映射模式 + /// 默认选项: 数据库文件需由使用方提供(不再内置离线库)、内存映射模式 /// [Fact] public void TestDefaultOptions() { - var options = GetRequiredService>().Value; + var defaultOptions = new AbpMaxMindGeoIP2Options(); + defaultOptions.DatabaseFile.ShouldBeNullOrEmpty(); + defaultOptions.DatabaseFileExists().ShouldBeFalse(); + defaultOptions.FileAccessMode.ShouldBe(FileAccessMode.MemoryMapped); - options.DatabaseFile.ShouldBe(AbpMaxMindGeoIP2Module.DefaultDatabaseFile); + // 测试宿主: 离线库路径由配置提供(未配置时回退到本地离线库文件) + var options = GetRequiredService>().Value; options.FileAccessMode.ShouldBe(FileAccessMode.MemoryMapped); }