pull file
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
Close Stale Issues / close_stale_issues (push) Has been cancelled
repo-sync / repo-sync (push) Has been cancelled
auto-deploy-tencent-scf / pre-check (push) Has been cancelled
auto-deploy-tencent-scf / deploy serverless (push) Has been cancelled
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
Close Stale Issues / close_stale_issues (push) Has been cancelled
repo-sync / repo-sync (push) Has been cancelled
auto-deploy-tencent-scf / pre-check (push) Has been cancelled
auto-deploy-tencent-scf / deploy serverless (push) Has been cancelled
This commit is contained in:
21
test/ConfigTest/ConfigTest.csproj
Normal file
21
test/ConfigTest/ConfigTest.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" />
|
||||
<PackageReference Include="xunit" />
|
||||
<PackageReference Include="xunit.runner.visualstudio">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ray.BiliBiliTool.Console\Ray.BiliBiliTool.Console.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
35
test/ConfigTest/TestDefaultValue.cs
Normal file
35
test/ConfigTest/TestDefaultValue.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ray.BiliBiliTool.Config;
|
||||
using Ray.BiliBiliTool.Config.Options;
|
||||
using Ray.BiliBiliTool.Console;
|
||||
using Ray.BiliBiliTool.Infrastructure;
|
||||
using Xunit;
|
||||
|
||||
namespace ConfigTest
|
||||
{
|
||||
public class TestDefaultValue
|
||||
{
|
||||
public TestDefaultValue()
|
||||
{
|
||||
Program.CreateHost(null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
using var scope = Global.ServiceProviderRoot.CreateScope();
|
||||
|
||||
var options = scope.ServiceProvider.GetRequiredService<
|
||||
IOptionsMonitor<DailyTaskOptions>
|
||||
>();
|
||||
var re = options.CurrentValue.NumberOfCoins;
|
||||
Debug.WriteLine(re);
|
||||
}
|
||||
}
|
||||
}
|
||||
175
test/ConfigTest/UnitTest1.cs
Normal file
175
test/ConfigTest/UnitTest1.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ray.BiliBiliTool.Agent;
|
||||
using Ray.BiliBiliTool.Config;
|
||||
using Ray.BiliBiliTool.Config.Options;
|
||||
using Ray.BiliBiliTool.Console;
|
||||
using Ray.BiliBiliTool.Infrastructure;
|
||||
using Xunit;
|
||||
|
||||
namespace ConfigTest
|
||||
{
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void WebProxyTest()
|
||||
{
|
||||
Program.CreateHost(new string[] { });
|
||||
string proxyAddress = Global.ConfigurationRoot["Security:WebProxy"];
|
||||
|
||||
if (!proxyAddress.IsNullOrEmpty())
|
||||
{
|
||||
WebProxy webProxy = new WebProxy();
|
||||
|
||||
//user:password@host:port http proxy only .Tested with tinyproxy-1.11.0-rc1
|
||||
if (proxyAddress.Contains("@"))
|
||||
{
|
||||
string userPass = proxyAddress.Split("@")[0];
|
||||
string address = proxyAddress.Split("@")[1];
|
||||
|
||||
string proxyUser = userPass.Split(":")[0];
|
||||
string proxyPass = userPass.Split(":")[1];
|
||||
|
||||
webProxy.Address = new Uri("http://" + address);
|
||||
webProxy.Credentials = new NetworkCredential(proxyUser, proxyPass);
|
||||
}
|
||||
else
|
||||
{
|
||||
webProxy.Address = new Uri(proxyAddress);
|
||||
}
|
||||
|
||||
HttpClient.DefaultProxy = webProxy;
|
||||
|
||||
HttpClient httpClient = new HttpClient();
|
||||
var response = httpClient.GetAsync("http://api.ipify.org/");
|
||||
var resultIp = response.Result.Content.ReadAsStringAsync().Result;
|
||||
Debug.WriteLine(String.Format("<22><>ǰIP<49><50> {0}", resultIp));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
Program.CreateHost(new string[] { });
|
||||
|
||||
string s = Global.ConfigurationRoot["BiliBiliCookie:UserId"];
|
||||
Debug.WriteLine(s);
|
||||
|
||||
string logLevel = Global.ConfigurationRoot[
|
||||
"Serilog:WriteTo:0:Args:restrictedToMinimumLevel"
|
||||
];
|
||||
Debug.WriteLine(logLevel);
|
||||
|
||||
var cookie = Global.ServiceProviderRoot.GetRequiredService<BiliCookie>();
|
||||
|
||||
Debug.WriteLine(
|
||||
JsonSerializer.Serialize(cookie, new JsonSerializerOptions { WriteIndented = true })
|
||||
);
|
||||
Assert.True(!string.IsNullOrWhiteSpace(cookie.UserId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Key<65>ķָ<C4B7><D6B8><EFBFBD>
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestEnvKeyDelimiter()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("Ray_BiliBiliCookie__UserId", "123");
|
||||
Program.CreateHost(null);
|
||||
|
||||
string result = Global.ConfigurationRoot["BiliBiliCookie:UserId"];
|
||||
|
||||
Assert.Equal("123", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPrefixConfigByEnvWithNoError()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("Ray_BiliBiliCookie", "UserId: 123");
|
||||
Program.CreateHost(new string[] { });
|
||||
|
||||
string result = Global.ConfigurationRoot["BiliBiliCookie"];
|
||||
|
||||
Assert.Equal("UserId: 123", result);
|
||||
Environment.SetEnvironmentVariable("Ray_BiliBiliCookie", null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadPrefixConfigByEnvWhenValueIsNullWithNoError2()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("Ray_BiliBiliCookie", null);
|
||||
Program.CreateHost(new string[] { });
|
||||
|
||||
string result = Global.ConfigurationRoot["BiliBiliCookie"];
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CoverConfigByEnvWithNoError()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Production");
|
||||
Program.CreateHost(new string[] { });
|
||||
|
||||
string result = Global.ConfigurationRoot["IsPrd"];
|
||||
|
||||
Assert.Equal("True", result);
|
||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>ֵ
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestSetConfiguration()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
|
||||
Program.CreateHost(new string[] { });
|
||||
|
||||
var options = Global.ServiceProviderRoot.GetRequiredService<
|
||||
IOptionsMonitor<BiliBiliCookieOptions>
|
||||
>();
|
||||
Debug.WriteLine(options.CurrentValue.ToJsonStr());
|
||||
|
||||
//<2F>ֶ<EFBFBD><D6B6><EFBFBD>ֵ
|
||||
//RayConfiguration.Root["BiliBiliCookie:UserId"] = "123456";
|
||||
//options.CurrentValue.UserId = "123456";
|
||||
|
||||
Debug.WriteLine(
|
||||
$"<22><>Configuration<6F><6E>ȡ<EFBFBD><C8A1>{Global.ConfigurationRoot["BiliBiliCookie:UserId"]}"
|
||||
);
|
||||
|
||||
Debug.WriteLine($"<22><><EFBFBD><EFBFBD>options<6E><73>ȡ<EFBFBD><C8A1>{options.CurrentValue.ToJsonStr()}");
|
||||
|
||||
var optionsNew = Global.ServiceProviderRoot.GetRequiredService<
|
||||
IOptionsMonitor<BiliBiliCookieOptions>
|
||||
>();
|
||||
Debug.WriteLine($"<22><><EFBFBD><EFBFBD>options<6E><73>ȡ<EFBFBD><C8A1>{optionsNew.CurrentValue.ToJsonStr()}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>ֵ
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestHostDefaults()
|
||||
{
|
||||
Debug.WriteLine(Environment.GetEnvironmentVariable(HostDefaults.EnvironmentKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test()
|
||||
{
|
||||
var s = "0123456";
|
||||
|
||||
var s1 = s[..2];
|
||||
|
||||
var s2 = s[4..];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user