1.添加测试文件2.添加文件下载接口实现

This commit is contained in:
2025-06-27 14:21:24 +08:00
parent 47bba1c3dc
commit d99fa77d93
4 changed files with 91 additions and 103 deletions

View 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>쳣!");
}
}
}