Compare commits

...

4 Commits

Author SHA1 Message Date
ShaoHua c1842877d5 修改项目目录 2025-06-27 15:06:42 +08:00
ShaoHua d99fa77d93 1.添加测试文件2.添加文件下载接口实现 2025-06-27 14:57:02 +08:00
ShaoHua 47bba1c3dc 添加项目文件。 2025-06-27 13:48:15 +08:00
ShaoHua 1330ae3c26 添加 .gitattributes 和 .gitignore。 2025-06-27 13:48:07 +08:00
22 changed files with 165 additions and 9 deletions
+19 -5
View File
@@ -3,7 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36212.18
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hua.DotNet.Code", "Hua.DotNet.Code.csproj", "{839E442B-62EA-1AA3-8675-66245AF23185}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7FAF528A-A606-4558-9300-4E62DC6B36F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hua.DotNet.Code", "src\Hua.Dotnet.Code\Hua.DotNet.Code.csproj", "{2ADCA6AB-5E13-35AD-57F0-9C010636F1EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hua.Dotnet.Code.Test", "test\Hua.Dotnet.Code.Test\Hua.Dotnet.Code.Test.csproj", "{B6421CF3-CB5F-89D4-C388-BD8E27089990}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -11,14 +17,22 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{839E442B-62EA-1AA3-8675-66245AF23185}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{839E442B-62EA-1AA3-8675-66245AF23185}.Debug|Any CPU.Build.0 = Debug|Any CPU
{839E442B-62EA-1AA3-8675-66245AF23185}.Release|Any CPU.ActiveCfg = Release|Any CPU
{839E442B-62EA-1AA3-8675-66245AF23185}.Release|Any CPU.Build.0 = Release|Any CPU
{2ADCA6AB-5E13-35AD-57F0-9C010636F1EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ADCA6AB-5E13-35AD-57F0-9C010636F1EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ADCA6AB-5E13-35AD-57F0-9C010636F1EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2ADCA6AB-5E13-35AD-57F0-9C010636F1EC}.Release|Any CPU.Build.0 = Release|Any CPU
{B6421CF3-CB5F-89D4-C388-BD8E27089990}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6421CF3-CB5F-89D4-C388-BD8E27089990}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6421CF3-CB5F-89D4-C388-BD8E27089990}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6421CF3-CB5F-89D4-C388-BD8E27089990}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2ADCA6AB-5E13-35AD-57F0-9C010636F1EC} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{B6421CF3-CB5F-89D4-C388-BD8E27089990} = {7FAF528A-A606-4558-9300-4E62DC6B36F1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {523CC539-A05F-4F3B-9833-E15A0AFF3584}
EndGlobalSection
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
@@ -71,7 +72,23 @@ public class RestClientHttpHelper : IHttpHelper
public bool DownloadFile(string url, string fileFullName)
{
throw new NotImplementedException();
}
{
try
{
var client = GetHttpClient();
var request = new RestRequest(url, Method.Get);
var response = client.DownloadData(request);
if (response == null) return false;
var dir = Path.GetDirectoryName(fileFullName);
Directory.CreateDirectory(dir);
File.WriteAllBytes(fileFullName, response);
return true;
}
catch (Exception ex)
{
_logger.LogError("下载文件失败: {0}", ex.Message);
return false;
}
}
}
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>Hua.DotNet.Code</AssemblyName>
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
@@ -14,6 +14,12 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RestSharp" Version="112.1.0" />
<PackageReference Include="System.Net.Http.Json" Version="9.0.6" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit.Analyzers" Version="4.9.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
@@ -0,0 +1,25 @@
<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="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Hua.Dotnet.Code\Hua.DotNet.Code.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>
</Project>
@@ -0,0 +1,78 @@
using Hua.DotNet.Code.Helper.Http;
using Microsoft.Extensions.Logging;
using Moq;
namespace Hua.Dotnet.Code.Test
{
[TestFixture]
public class RestClientHttpHelperTests
{
//[Test]
//public async Task GetAsync_ShouldReturnSuccessResponse()
//{
// // Arrange
// var mockLogger = new Mock<ILogger<RestClientHttpHelper>>();
// var helper = new RestClientHttpHelper(mockLogger.Object);
// // Act
// var result = await helper.GetAsync("https://api.example.com");
// // Assert
// Assert.IsTrue(result.IsSuccessStatusCode);
//}
//[Test]
//public async Task PostAsync_ShouldHandleException()
//{
// // Arrange
// var mockLogger = new Mock<ILogger<RestClientHttpHelper>>();
// var helper = new RestClientHttpHelper(mockLogger.Object);
// // Act & Assert
// Assert.ThrowsAsync<HttpRequestException>(() =>
// helper.PostAsync("https://api.example.com", new StringContent("")));
//}
[Test]
public void DownloadFile_ShouldCreateLocalFile()
{
// Arrange
var testPath = Path.Combine(Path.GetTempPath(), "testfile.dat");
var mockResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StreamContent(new MemoryStream(new byte[1024]))
};
var mockLogger = new Mock<ILogger<RestClientHttpHelper>>();
var helper = new RestClientHttpHelper(mockLogger.Object);
// Act
var result = helper.DownloadFile("https://example.com/file", testPath);
// Assert
if (result&& File.Exists(testPath))
{
Assert.Pass();
}
// Cleanup
File.Delete(testPath);
}
[Test]
public void DownloadFile_ShouldHandleIOException()
{
// Arrange
var invalidPath = "D:\\Temp\\123\\123.jpg";
var mockLogger = new Mock<ILogger<RestClientHttpHelper>>();
var helper = new RestClientHttpHelper(mockLogger.Object);
helper.DownloadFile("http://gips3.baidu.com/it/u=1821127123,1149655687&fm=3028&app=3028&f=JPEG&fmt=auto?w=720&h=1280", invalidPath);
if (File.Exists(invalidPath))
{
Assert.Pass("ÏÂÔØ³É¹¦!");
}
Assert.Pass("δ֪Òì³£!");
}
}
}
+16
View File
@@ -0,0 +1,16 @@
namespace Hua.Dotnet.Code.Test
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass();
}
}
}