From e3cc987f1f8072d85da68062412e3b0a6759804c Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Tue, 22 Mar 2022 21:22:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20IPLocator=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/IPLocatorTest.cs | 38 +++++++++++++++++++ test/UnitTest/Core/BootstrapBlazorTestBase.cs | 4 ++ 2 files changed, 42 insertions(+) create mode 100644 test/UnitTest/Components/IPLocatorTest.cs diff --git a/test/UnitTest/Components/IPLocatorTest.cs b/test/UnitTest/Components/IPLocatorTest.cs new file mode 100644 index 000000000..3eb1df71b --- /dev/null +++ b/test/UnitTest/Components/IPLocatorTest.cs @@ -0,0 +1,38 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UnitTest.Components; + +public class IPLocatorTest : BootstrapBlazorTestBase +{ + [Fact] + public async Task Locator() + { + var cut = Context.RenderComponent(); + + var result = await cut.Instance.IPLocator.Locate("127.0.0.1"); + + Assert.Equal("本地连接", result); + + var widenet = await cut.Instance.IPLocator.Locate("223.91.188.112"); + + Assert.NotNull(widenet); + } + + private class IpLocatorTest : ComponentBase + { + [Inject] + [NotNull] + public IIPLocatorProvider? IPLocator { get; set; } + } +} + + diff --git a/test/UnitTest/Core/BootstrapBlazorTestBase.cs b/test/UnitTest/Core/BootstrapBlazorTestBase.cs index 21c79ea1d..7c7d64749 100644 --- a/test/UnitTest/Core/BootstrapBlazorTestBase.cs +++ b/test/UnitTest/Core/BootstrapBlazorTestBase.cs @@ -47,6 +47,10 @@ public class BootstrapBlazorTestHost : IDisposable protected virtual void ConfigureServices(IServiceCollection services) { services.AddBootstrapBlazor(); + services.ConfigureIPLocatorOption(options => + { + options.LocatorFactory = provider => new BaiDuIPLocator(); + }); services.ConfigureJsonLocalizationOptions(op => op.AdditionalJsonAssemblies = new[] { typeof(Alert).Assembly }); services.AddSingleton(); -- Gitee From 826fed34c0d9679fde04513926dd5785b576fb85 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Tue, 22 Mar 2022 22:25:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20BaiduIpLocator?= =?UTF-8?q?=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/IPLocatorTest.cs | 64 ++++++++++++++++++----- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/test/UnitTest/Components/IPLocatorTest.cs b/test/UnitTest/Components/IPLocatorTest.cs index 3eb1df71b..e5ad2a322 100644 --- a/test/UnitTest/Components/IPLocatorTest.cs +++ b/test/UnitTest/Components/IPLocatorTest.cs @@ -2,31 +2,69 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ -using BootstrapBlazor.Components; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - namespace UnitTest.Components; public class IPLocatorTest : BootstrapBlazorTestBase { [Fact] - public async Task Locator() + public async Task Locator_Ok() { var cut = Context.RenderComponent(); - var result = await cut.Instance.IPLocator.Locate("127.0.0.1"); - Assert.Equal("本地连接", result); - + result = await cut.Instance.IPLocator.Locate(""); + Assert.Equal("本地连接", result); var widenet = await cut.Instance.IPLocator.Locate("223.91.188.112"); - Assert.NotNull(widenet); } + [Fact] + public void BaiduIpLocator_Ok() + { + var locator = new BaiDuIPLocator + { + Status = "0" + }; + var ret = locator.ToString(); + Assert.Equal("XX XX", ret); + + locator.Data = Array.Empty(); + ret = locator.ToString(); + Assert.Equal("XX XX", ret); + + locator.Data = new LocationInfo[] + { + new() + }; + ret = locator.ToString(); + Assert.Equal("XX XX", ret); + + locator.Data = new LocationInfo[] + { + new() + { + Location = "Test" + } + }; + ret = locator.ToString(); + Assert.Equal("Test", ret); + + locator.Status = "1"; + ret = locator.ToString(); + Assert.Equal("Error", ret); + } + + [Fact] + public async Task DefaultIPLocator_Ok() + { + var locator = new DefaultIPLocator(); + var ret = await locator.Locate(new IPLocatorOption() + { + RequestTimeout = 3000 + }); + Assert.Null(ret); + } + private class IpLocatorTest : ComponentBase { [Inject] @@ -34,5 +72,3 @@ public class IPLocatorTest : BootstrapBlazorTestBase public IIPLocatorProvider? IPLocator { get; set; } } } - - -- Gitee