1.添加测试文件2.添加文件下载接口实现
This commit is contained in:
@@ -1,92 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Moq;
|
|
||||||
using RestSharp;
|
|
||||||
|
|
||||||
namespace Hua.DotNet.Code.Helper.Http
|
|
||||||
{
|
|
||||||
public class RestClientHttpHelperTests
|
|
||||||
{
|
|
||||||
[Test]
|
|
||||||
public void DownloadFile_ShouldReturnTrue_WhenDownloadSucceeds()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var loggerMock = new Mock<ILogger<RestClientHttpHelper>>();
|
|
||||||
var helper = new RestClientHttpHelper(loggerMock.Object);
|
|
||||||
var testUrl = "https://example.com/testfile.txt";
|
|
||||||
var testFilePath = Path.GetTempFileName();
|
|
||||||
|
|
||||||
var restClientMock = new Mock<RestClient>();
|
|
||||||
var responseData = System.Text.Encoding.UTF8.GetBytes("Test content");
|
|
||||||
restClientMock.Setup(c => c.DownloadData(It.IsAny<RestRequest>())).Returns(responseData);
|
|
||||||
|
|
||||||
var type = helper.GetType();
|
|
||||||
var getHttpClientMethod = type.GetMethod("GetHttpClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
||||||
var mockGetter = new Func<RestClient>(() => restClientMock.Object);
|
|
||||||
|
|
||||||
var mockMethod = mockGetter.Method;
|
|
||||||
var mockInstance = mockGetter.Target;
|
|
||||||
|
|
||||||
var proxy = new DynamicProxy(helper, getHttpClientMethod, mockMethod, mockInstance);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = helper.DownloadFile(testUrl, testFilePath);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(result, Is.True);
|
|
||||||
Assert.That(File.Exists(testFilePath), Is.True);
|
|
||||||
File.Delete(testFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void DownloadFile_ShouldReturnFalse_WhenDownloadFails()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var loggerMock = new Mock<ILogger<RestClientHttpHelper>>();
|
|
||||||
var helper = new RestClientHttpHelper(loggerMock.Object);
|
|
||||||
var testUrl = "https://example.com/nonexistentfile.txt";
|
|
||||||
var testFilePath = Path.GetTempFileName();
|
|
||||||
|
|
||||||
var restClientMock = new Mock<RestClient>();
|
|
||||||
restClientMock.Setup(c => c.DownloadData(It.IsAny<RestRequest>())).Returns((byte[])null);
|
|
||||||
|
|
||||||
var type = helper.GetType();
|
|
||||||
var getHttpClientMethod = type.GetMethod("GetHttpClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
||||||
var mockGetter = new Func<RestClient>(() => restClientMock.Object);
|
|
||||||
|
|
||||||
var mockMethod = mockGetter.Method;
|
|
||||||
var mockInstance = mockGetter.Target;
|
|
||||||
|
|
||||||
var proxy = new DynamicProxy(helper, getHttpClientMethod, mockMethod, mockInstance);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = helper.DownloadFile(testUrl, testFilePath);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(result, Is.False);
|
|
||||||
Assert.That(File.Exists(testFilePath), Is.False);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DynamicProxy
|
|
||||||
{
|
|
||||||
private readonly object _target;
|
|
||||||
private readonly System.Reflection.MethodInfo _originalMethod;
|
|
||||||
private readonly System.Reflection.MethodInfo _mockMethod;
|
|
||||||
private readonly object _mockInstance;
|
|
||||||
|
|
||||||
public DynamicProxy(object target, System.Reflection.MethodInfo originalMethod, System.Reflection.MethodInfo mockMethod, object mockInstance)
|
|
||||||
{
|
|
||||||
_target = target;
|
|
||||||
_originalMethod = originalMethod;
|
|
||||||
_mockMethod = mockMethod;
|
|
||||||
_mockInstance = mockInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Invoke(object[] parameters)
|
|
||||||
{
|
|
||||||
return _mockMethod.Invoke(_mockInstance, parameters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -77,12 +78,12 @@ public class RestClientHttpHelper : IHttpHelper
|
|||||||
var client = GetHttpClient();
|
var client = GetHttpClient();
|
||||||
var request = new RestRequest(url, Method.Get);
|
var request = new RestRequest(url, Method.Get);
|
||||||
var response = client.DownloadData(request);
|
var response = client.DownloadData(request);
|
||||||
if (response != null)
|
if (response == null) return false;
|
||||||
{
|
|
||||||
System.IO.File.WriteAllBytes(fileFullName, response);
|
var dir = Path.GetDirectoryName(fileFullName);
|
||||||
return true;
|
Directory.CreateDirectory(dir);
|
||||||
}
|
File.WriteAllBytes(fileFullName, response);
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
@@ -10,11 +10,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||||
<PackageReference Include="NUnit" Version="3.14.0" />
|
<PackageReference Include="NUnit" Version="4.3.2" />
|
||||||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
|
</ItemGroup>
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\src\Hua.Dotnet.Code\Hua.DotNet.Code.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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