78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
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>쳣!");
|
||
}
|
||
}
|
||
} |