2022-10-21 00:23:54 +02:00
|
|
|
using System.Reflection;
|
2022-10-15 00:07:59 +02:00
|
|
|
using Flurl.Http.Configuration;
|
2022-10-17 23:52:58 +02:00
|
|
|
using Flurl.Http.Content;
|
2022-10-15 00:07:59 +02:00
|
|
|
using Flurl.Http.Testing;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Piwigo.Client.Contract;
|
|
|
|
|
|
|
|
namespace Piwigo.Client.Tests;
|
|
|
|
|
|
|
|
public class ApiTestsBase
|
|
|
|
{
|
|
|
|
private const string TestUri = "http://localhost:8080/ws.php?format=json";
|
|
|
|
private const string Username = "admin";
|
|
|
|
private const string Password = "admin";
|
|
|
|
private const bool UseHttpTest = true;
|
|
|
|
protected HttpTest? HttpTest { get; private set; }
|
|
|
|
protected IPiwigoContext Context { get; private set; } = null!;
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
|
|
|
if (UseHttpTest)
|
|
|
|
{
|
|
|
|
HttpTest = new HttpTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
Context = new PiwigoContext(new PiwigoConfiguration(TestUri, Username, Password), new NullLogger<PiwigoContext>());
|
|
|
|
OnSetUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void OnSetUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
|
|
|
HttpTest?.Dispose();
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:25 +02:00
|
|
|
protected void SetOkResult()
|
2022-10-16 23:53:17 +02:00
|
|
|
{
|
2022-10-17 23:52:58 +02:00
|
|
|
SetJsonResult(@"{stat: ""ok"", result: null}");
|
2022-10-16 23:53:17 +02:00
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:25 +02:00
|
|
|
protected async Task SetJsonResultFromFileAsync(string fileName)
|
2022-10-21 00:23:54 +02:00
|
|
|
{
|
|
|
|
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
|
|
|
|
var directory = Path.GetDirectoryName(assemblyLocation) ?? Environment.CurrentDirectory;
|
|
|
|
var path = Path.Combine(directory, fileName);
|
|
|
|
|
|
|
|
using var reader = File.OpenText(path);
|
|
|
|
|
|
|
|
var fileContent = await reader.ReadToEndAsync();
|
|
|
|
|
|
|
|
SetJsonResult(fileContent);
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:25 +02:00
|
|
|
protected void SetJsonResult(string json)
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 23:53:17 +02:00
|
|
|
HttpTest?.RespondWith(json);
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 23:52:58 +02:00
|
|
|
protected void CorrectMethodShouldGetCalled(string methodName)
|
|
|
|
{
|
|
|
|
CorrectParamShouldGetSent("method", methodName);
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:38:25 +02:00
|
|
|
protected void ParamShouldNotGetSent(string paramName)
|
|
|
|
{
|
|
|
|
HttpTest?.ShouldHaveMadeACall().With(c =>
|
|
|
|
{
|
|
|
|
return !c.HttpRequestMessage.Content.As<CapturedMultipartContent>().Parts.OfType<CapturedStringContent>()
|
|
|
|
.Select(p => new { p.Headers.ContentDisposition?.Name, p.Content })
|
|
|
|
.Any(s => s.Name?.Equals(paramName) ?? false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void CorrectParamShouldGetSent(string paramName, string expectedValue)
|
2022-10-17 23:52:58 +02:00
|
|
|
{
|
|
|
|
HttpTest?.ShouldHaveMadeACall().With(c =>
|
|
|
|
c.HttpRequestMessage.Content.As<CapturedMultipartContent>().Parts.OfType<CapturedStringContent>().Select(p => new { p.Headers.ContentDisposition?.Name, p.Content })
|
2022-10-22 22:38:25 +02:00
|
|
|
.Where(s => s.Name?.Equals(paramName) ?? false).Any(s => s.Content.Equals(expectedValue, StringComparison.OrdinalIgnoreCase)));
|
2022-10-17 23:52:58 +02:00
|
|
|
}
|
|
|
|
|
2022-10-15 00:07:59 +02:00
|
|
|
internal void SetJsonResult<T>(PiwigoResponse<T> serverResponse)
|
|
|
|
{
|
|
|
|
var settings = new JsonSerializerSettings
|
|
|
|
{
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
};
|
|
|
|
var serializer = new NewtonsoftJsonSerializer(settings);
|
|
|
|
var jsonResponse = serializer.Serialize(serverResponse);
|
|
|
|
HttpTest?.RespondWith(jsonResponse);
|
|
|
|
}
|
|
|
|
}
|