1.添加测试文件2.添加文件下载接口实现
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@@ -10,11 +10,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||
<PackageReference Include="NUnit" Version="4.3.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Hua.Dotnet.Code\Hua.DotNet.Code.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
78
test/Hua.Dotnet.Code.Test/RestClientHttpHelperTests.cs
Normal file
78
test/Hua.Dotnet.Code.Test/RestClientHttpHelperTests.cs
Normal file
@@ -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("<22><><EFBFBD>سɹ<D8B3>!");
|
||||
}
|
||||
|
||||
Assert.Pass("δ֪<CEB4>쳣!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user