Added some Zone features
This commit is contained in:
@@ -200,6 +200,54 @@ namespace Cloudflare.Core.Tests.CloudflareClientTests
|
|||||||
VerifyNoOtherCalls();
|
VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPostWithoutContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_httpHandlerMock.Responses.Enqueue(new HttpResponseMessage
|
||||||
|
{
|
||||||
|
StatusCode = HttpStatusCode.OK,
|
||||||
|
Content = new StringContent(@"{""success"": true, ""errors"": [], ""messages"": [], ""result"": { ""string"": ""some-string"", ""integer"": 123 }}", Encoding.UTF8, MediaTypeNames.Application.Json),
|
||||||
|
});
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PostAsync<TestClass, object>("posting", null);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.IsNotNull(response.Errors);
|
||||||
|
Assert.IsNotNull(response.Messages);
|
||||||
|
Assert.IsNull(response.ResultInfo);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, response.Errors.Count);
|
||||||
|
Assert.AreEqual(0, response.Messages.Count);
|
||||||
|
|
||||||
|
Assert.IsNotNull(response.Result);
|
||||||
|
Assert.AreEqual("some-string", response.Result.Str);
|
||||||
|
Assert.AreEqual(123, response.Result.Int);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _httpHandlerMock.Callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _httpHandlerMock.Callbacks.First();
|
||||||
|
Assert.AreEqual(HttpMethod.Post, callback.Method);
|
||||||
|
Assert.AreEqual("https://localhost/api/v4/posting", callback.Url);
|
||||||
|
Assert.IsNull(callback.Content);
|
||||||
|
|
||||||
|
Assert.AreEqual(3, callback.Headers.Count);
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("Accept"));
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("Authorization"));
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("User-Agent"));
|
||||||
|
|
||||||
|
Assert.AreEqual("application/json", callback.Headers["Accept"]);
|
||||||
|
Assert.AreEqual("Bearer Some-API-Token", callback.Headers["Authorization"]);
|
||||||
|
Assert.AreEqual("AMWD.CloudflareClient/1.0.0", callback.Headers["User-Agent"]);
|
||||||
|
|
||||||
|
_httpHandlerMock.Mock.Protected().Verify("SendAsync", Times.Once(), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>());
|
||||||
|
}
|
||||||
|
|
||||||
[DataTestMethod]
|
[DataTestMethod]
|
||||||
[DataRow(HttpStatusCode.Unauthorized)]
|
[DataRow(HttpStatusCode.Unauthorized)]
|
||||||
[DataRow(HttpStatusCode.Forbidden)]
|
[DataRow(HttpStatusCode.Forbidden)]
|
||||||
@@ -310,6 +358,63 @@ namespace Cloudflare.Core.Tests.CloudflareClientTests
|
|||||||
await client.PostAsync<TestClass, TestClass>("some-path", _request);
|
await client.PostAsync<TestClass, TestClass>("some-path", _request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldOnlySerializeNonNullValues()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Str = null;
|
||||||
|
_httpHandlerMock.Responses.Enqueue(new HttpResponseMessage
|
||||||
|
{
|
||||||
|
StatusCode = HttpStatusCode.OK,
|
||||||
|
Content = new StringContent("This is an awesome text ;-)", Encoding.UTF8, MediaTypeNames.Text.Plain),
|
||||||
|
});
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PostAsync<string, TestClass>("path", _request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.IsNotNull(response.Errors);
|
||||||
|
Assert.IsNotNull(response.Messages);
|
||||||
|
Assert.IsNotNull(response.ResultInfo);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, response.Errors.Count);
|
||||||
|
Assert.AreEqual(0, response.Messages.Count);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, response.ResultInfo.Count);
|
||||||
|
Assert.AreEqual(0, response.ResultInfo.Page);
|
||||||
|
Assert.AreEqual(0, response.ResultInfo.PerPage);
|
||||||
|
Assert.AreEqual(0, response.ResultInfo.TotalCount);
|
||||||
|
|
||||||
|
Assert.AreEqual("This is an awesome text ;-)", response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _httpHandlerMock.Callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _httpHandlerMock.Callbacks.First();
|
||||||
|
Assert.AreEqual(HttpMethod.Post, callback.Method);
|
||||||
|
Assert.AreEqual("https://localhost/api/v4/path", callback.Url);
|
||||||
|
Assert.AreEqual(@"{""integer"":54321}", callback.Content);
|
||||||
|
|
||||||
|
Assert.AreEqual(3, callback.Headers.Count);
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("Accept"));
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("Authorization"));
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("User-Agent"));
|
||||||
|
|
||||||
|
Assert.AreEqual("application/json", callback.Headers["Accept"]);
|
||||||
|
Assert.AreEqual("Bearer Some-API-Token", callback.Headers["Authorization"]);
|
||||||
|
Assert.AreEqual("AMWD.CloudflareClient/1.0.0", callback.Headers["User-Agent"]);
|
||||||
|
|
||||||
|
_httpHandlerMock.Mock.Protected().Verify("SendAsync", Times.Once(), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>());
|
||||||
|
|
||||||
|
_authenticationMock.Verify(m => m.AddHeader(It.IsAny<HttpClient>()), Times.Once);
|
||||||
|
|
||||||
|
VerifyDefaults();
|
||||||
|
VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
private void VerifyDefaults()
|
private void VerifyDefaults()
|
||||||
{
|
{
|
||||||
_authenticationMock.Verify(m => m.AddHeader(It.IsAny<HttpClient>()), Times.Once);
|
_authenticationMock.Verify(m => m.AddHeader(It.IsAny<HttpClient>()), Times.Once);
|
||||||
|
|||||||
@@ -200,6 +200,54 @@ namespace Cloudflare.Core.Tests.CloudflareClientTests
|
|||||||
VerifyNoOtherCalls();
|
VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPutWithoutContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_httpHandlerMock.Responses.Enqueue(new HttpResponseMessage
|
||||||
|
{
|
||||||
|
StatusCode = HttpStatusCode.OK,
|
||||||
|
Content = new StringContent(@"{""success"": true, ""errors"": [], ""messages"": [], ""result"": { ""string"": ""some-string"", ""integer"": 123 }}", Encoding.UTF8, MediaTypeNames.Application.Json),
|
||||||
|
});
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PutAsync<TestClass, object>("putput", null);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.IsNotNull(response.Errors);
|
||||||
|
Assert.IsNotNull(response.Messages);
|
||||||
|
Assert.IsNull(response.ResultInfo);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, response.Errors.Count);
|
||||||
|
Assert.AreEqual(0, response.Messages.Count);
|
||||||
|
|
||||||
|
Assert.IsNotNull(response.Result);
|
||||||
|
Assert.AreEqual("some-string", response.Result.Str);
|
||||||
|
Assert.AreEqual(123, response.Result.Int);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _httpHandlerMock.Callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _httpHandlerMock.Callbacks.First();
|
||||||
|
Assert.AreEqual(HttpMethod.Put, callback.Method);
|
||||||
|
Assert.AreEqual("https://localhost/api/v4/putput", callback.Url);
|
||||||
|
Assert.IsNull(callback.Content);
|
||||||
|
|
||||||
|
Assert.AreEqual(3, callback.Headers.Count);
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("Accept"));
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("Authorization"));
|
||||||
|
Assert.IsTrue(callback.Headers.ContainsKey("User-Agent"));
|
||||||
|
|
||||||
|
Assert.AreEqual("application/json", callback.Headers["Accept"]);
|
||||||
|
Assert.AreEqual("Bearer Some-API-Token", callback.Headers["Authorization"]);
|
||||||
|
Assert.AreEqual("AMWD.CloudflareClient/1.0.0", callback.Headers["User-Agent"]);
|
||||||
|
|
||||||
|
_httpHandlerMock.Mock.Protected().Verify("SendAsync", Times.Once(), ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>());
|
||||||
|
}
|
||||||
|
|
||||||
[DataTestMethod]
|
[DataTestMethod]
|
||||||
[DataRow(HttpStatusCode.Unauthorized)]
|
[DataRow(HttpStatusCode.Unauthorized)]
|
||||||
[DataRow(HttpStatusCode.Forbidden)]
|
[DataRow(HttpStatusCode.Forbidden)]
|
||||||
|
|||||||
131
Cloudflare.Tests/Extensions/StringExtensionsTest.cs
Normal file
131
Cloudflare.Tests/Extensions/StringExtensionsTest.cs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
using System;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
|
||||||
|
namespace Cloudflare.Tests.Extensions
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class StringExtensionsTest
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldValidateId()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string id = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
id.ValidateCloudflareId();
|
||||||
|
|
||||||
|
// Assert - no exception thrown
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public void ShouldThrowArgumentNullExceptionForValidateId(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
|
||||||
|
// Act
|
||||||
|
name.ValidateCloudflareId();
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public void ShouldThrowArgumentOutOfRangeExceptionForValidateId()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string id = new('a', 33);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
id.ValidateCloudflareId();
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldValidateName()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string name = "Example Account Name";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
name.ValidateCloudflareName();
|
||||||
|
|
||||||
|
// Assert - no exception thrown
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public void ShouldThrowArgumentNullExceptionForValidateName(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
|
||||||
|
// Act
|
||||||
|
name.ValidateCloudflareName();
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public void ShouldThrowArgumentOutOfRangeExceptionForValidateName()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string name = new('a', 254);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
name.ValidateCloudflareName();
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldValidateEmail()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string email = "test@example.com";
|
||||||
|
|
||||||
|
// Act
|
||||||
|
email.ValidateCloudflareEmailAddress();
|
||||||
|
|
||||||
|
// Assert - no exception thrown
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public void ShouldThrowArgumentNullExceptionForValidateEmail(string email)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
|
||||||
|
// Act
|
||||||
|
email.ValidateCloudflareEmailAddress();
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow("test")]
|
||||||
|
[DataRow("test@example")]
|
||||||
|
[DataRow("example.com")]
|
||||||
|
[ExpectedException(typeof(ArgumentException))]
|
||||||
|
public void ShouldThrowArgumentExceptionForValidateEmail(string email)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
|
||||||
|
// Act
|
||||||
|
email.ValidateCloudflareEmailAddress();
|
||||||
|
|
||||||
|
// Assert - ArgumentException
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace AMWD.Net.Api.Cloudflare.Auth
|
namespace AMWD.Net.Api.Cloudflare.Auth
|
||||||
{
|
{
|
||||||
@@ -9,7 +8,6 @@ namespace AMWD.Net.Api.Cloudflare.Auth
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ApiKeyAuthentication : IAuthentication
|
public class ApiKeyAuthentication : IAuthentication
|
||||||
{
|
{
|
||||||
private static readonly Regex _emailCheckRegex = new(@"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$", RegexOptions.Compiled);
|
|
||||||
private readonly string _emailAddress;
|
private readonly string _emailAddress;
|
||||||
private readonly string _apiKey;
|
private readonly string _apiKey;
|
||||||
|
|
||||||
@@ -20,15 +18,11 @@ namespace AMWD.Net.Api.Cloudflare.Auth
|
|||||||
/// <param name="apiKey">The global API key.</param>
|
/// <param name="apiKey">The global API key.</param>
|
||||||
public ApiKeyAuthentication(string emailAddress, string apiKey)
|
public ApiKeyAuthentication(string emailAddress, string apiKey)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(emailAddress))
|
emailAddress.ValidateCloudflareEmailAddress();
|
||||||
throw new ArgumentNullException(nameof(emailAddress));
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(apiKey))
|
if (string.IsNullOrWhiteSpace(apiKey))
|
||||||
throw new ArgumentNullException(nameof(apiKey));
|
throw new ArgumentNullException(nameof(apiKey));
|
||||||
|
|
||||||
if (!_emailCheckRegex.IsMatch(emailAddress))
|
|
||||||
throw new ArgumentException("Invalid email address", nameof(emailAddress));
|
|
||||||
|
|
||||||
_emailAddress = emailAddress;
|
_emailAddress = emailAddress;
|
||||||
_apiKey = apiKey;
|
_apiKey = apiKey;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,15 +94,19 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, CancellationToken cancellationToken = default)
|
public async Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
ValidateRequestPath(requestPath);
|
ValidateRequestPath(requestPath);
|
||||||
|
|
||||||
string requestUrl = BuildRequestUrl(requestPath);
|
string requestUrl = BuildRequestUrl(requestPath, queryFilter);
|
||||||
|
|
||||||
HttpContent httpRequestContent;
|
HttpContent httpRequestContent;
|
||||||
if (request is HttpContent httpContent)
|
if (request == null)
|
||||||
|
{
|
||||||
|
httpRequestContent = null;
|
||||||
|
}
|
||||||
|
else if (request is HttpContent httpContent)
|
||||||
{
|
{
|
||||||
httpRequestContent = httpContent;
|
httpRequestContent = httpContent;
|
||||||
}
|
}
|
||||||
@@ -125,7 +129,11 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
string requestUrl = BuildRequestUrl(requestPath);
|
string requestUrl = BuildRequestUrl(requestPath);
|
||||||
|
|
||||||
HttpContent httpRequestContent;
|
HttpContent httpRequestContent;
|
||||||
if (request is HttpContent httpContent)
|
if (request == null)
|
||||||
|
{
|
||||||
|
httpRequestContent = null;
|
||||||
|
}
|
||||||
|
else if (request is HttpContent httpContent)
|
||||||
{
|
{
|
||||||
httpRequestContent = httpContent;
|
httpRequestContent = httpContent;
|
||||||
}
|
}
|
||||||
@@ -140,12 +148,12 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public async Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, CancellationToken cancellationToken = default)
|
public async Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
ValidateRequestPath(requestPath);
|
ValidateRequestPath(requestPath);
|
||||||
|
|
||||||
string requestUrl = BuildRequestUrl(requestPath);
|
string requestUrl = BuildRequestUrl(requestPath, queryFilter);
|
||||||
|
|
||||||
var response = await _httpClient.DeleteAsync(requestUrl, cancellationToken).ConfigureAwait(false);
|
var response = await _httpClient.DeleteAsync(requestUrl, cancellationToken).ConfigureAwait(false);
|
||||||
return await GetCloudflareResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
|
return await GetCloudflareResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
|
||||||
@@ -285,7 +293,7 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
return new CloudflareResponse<TRes>
|
return new CloudflareResponse<TRes>
|
||||||
{
|
{
|
||||||
Success = true,
|
Success = true,
|
||||||
ResultInfo = new ResultInfo(),
|
ResultInfo = new PaginationInfo(),
|
||||||
Result = (TRes)cObj,
|
Result = (TRes)cObj,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,21 +4,21 @@ using Newtonsoft.Json.Converters;
|
|||||||
namespace AMWD.Net.Api.Cloudflare
|
namespace AMWD.Net.Api.Cloudflare
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The direction to order the entity.
|
/// The direction to sort the entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonConverter(typeof(StringEnumConverter))]
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
public enum OrderDirection
|
public enum SortDirection
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Order in ascending order.
|
/// Sort in ascending order.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EnumMember(Value = "asc")]
|
[EnumMember(Value = "asc")]
|
||||||
Asc = 1,
|
Ascending = 1,
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Order in descending order.
|
/// Sort in descending order.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[EnumMember(Value = "desc")]
|
[EnumMember(Value = "desc")]
|
||||||
Desc = 2
|
Descending = 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
64
Cloudflare/Extensions/StringExtensions.cs
Normal file
64
Cloudflare/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for <see cref="string"/>s.
|
||||||
|
/// </summary>
|
||||||
|
public static class StringExtensions
|
||||||
|
{
|
||||||
|
private static readonly Regex _emailCheckRegex = new(@"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$", RegexOptions.Compiled);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate basic information for a Cloudflare ID.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// An Cloudflare ID has max. 32 characters.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id">The string to check.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">The <paramref name="id"/> is <see langword="null"/> or any kind of whitespace.</exception>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="id"/> has more than 32 characters.</exception>
|
||||||
|
public static void ValidateCloudflareId(this string id)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(id))
|
||||||
|
throw new ArgumentNullException(nameof(id));
|
||||||
|
|
||||||
|
if (id.Length > 32)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate basic information for a Cloudflare name.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// An Cloudflare name has max. 253 characters.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="name">The string to check.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">The <paramref name="name"/> is <see langword="null"/> or any kind of whitespace.</exception>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="name"/> has more than 253 characters.</exception>
|
||||||
|
public static void ValidateCloudflareName(this string name)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(name))
|
||||||
|
throw new ArgumentNullException(nameof(name));
|
||||||
|
|
||||||
|
if (name.Length > 253)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Validate basic information for an email address.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="emailAddress">The string to check.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">The <paramref name="emailAddress"/> is <see langword="null"/> or any kind of whitespace.</exception>
|
||||||
|
/// <exception cref="ArgumentException">The <paramref name="emailAddress"/> does not match the regular expression pattern for an email address.</exception>
|
||||||
|
public static void ValidateCloudflareEmailAddress(this string emailAddress)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(emailAddress))
|
||||||
|
throw new ArgumentNullException(nameof(emailAddress));
|
||||||
|
|
||||||
|
if (!_emailCheckRegex.IsMatch(emailAddress))
|
||||||
|
throw new ArgumentException("Invalid email address", nameof(emailAddress));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,8 +31,9 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
/// <typeparam name="TRequest">The request type.</typeparam>
|
/// <typeparam name="TRequest">The request type.</typeparam>
|
||||||
/// <param name="requestPath">The request path (extending the base URL).</param>
|
/// <param name="requestPath">The request path (extending the base URL).</param>
|
||||||
/// <param name="request">The request content.</param>
|
/// <param name="request">The request content.</param>
|
||||||
|
/// <param name="queryFilter">The query parameters.</param>
|
||||||
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, CancellationToken cancellationToken = default);
|
Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Makes a PUT request to the Cloudflare API.
|
/// Makes a PUT request to the Cloudflare API.
|
||||||
@@ -55,9 +56,10 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <typeparam name="TResponse">The response type.</typeparam>
|
/// <typeparam name="TResponse">The response type.</typeparam>
|
||||||
/// <param name="requestPath">The request path (extending the base URL).</param>
|
/// <param name="requestPath">The request path (extending the base URL).</param>
|
||||||
|
/// <param name="queryFilter">The query parameters.</param>
|
||||||
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, CancellationToken cancellationToken = default);
|
Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Makes a PATCH request to the Cloudflare API.
|
/// Makes a PATCH request to the Cloudflare API.
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace AMWD.Net.Api.Cloudflare
|
|||||||
/// Information about the result of the request.
|
/// Information about the result of the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonProperty("result_info")]
|
[JsonProperty("result_info")]
|
||||||
public ResultInfo ResultInfo { get; set; }
|
public PaginationInfo ResultInfo { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether the API call was successful.
|
/// Whether the API call was successful.
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
namespace AMWD.Net.Api.Cloudflare
|
namespace AMWD.Net.Api.Cloudflare
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Cloudflare Result Information.
|
/// Cloudflare pagination information.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ResultInfo
|
public class PaginationInfo
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Total number of results for the requested service.
|
/// Total number of results for the requested service.
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
<Company>AM.WD</Company>
|
<Company>AM.WD</Company>
|
||||||
<Authors>Andreas Müller</Authors>
|
<Authors>Andreas Müller</Authors>
|
||||||
<Copyright>© {copyright:2024-} AM.WD</Copyright>
|
<Copyright>© {copyright:2024-} AM.WD</Copyright>
|
||||||
|
|
||||||
|
<PublicKey>0024000004800000940000000602000000240000525341310004000001000100a96b0435a48fcae5d097c19c0c3a312d0316c1217a7d5984236f430625510dfdbedc3ffdaea7b3bad77adbe5d85cecdd788a43cd02a8a4950313587bbcb804ff2ef68346f9d6a79f79338e4f12293f216df0536d2b05ab7977b6c50946a42422cb1ddc109c0151a3d65fbe636ce6734070fb6e3eaf000a33ac6a36cab5292ed1</PublicKey>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests
|
||||||
|
{
|
||||||
|
internal class PurgeRequest
|
||||||
|
{
|
||||||
|
[JsonProperty("purge_everything")]
|
||||||
|
public bool? PurgeEverything { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("tags")]
|
||||||
|
public IList<string> Tags { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("hosts")]
|
||||||
|
public IList<string> Hostnames { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("prefixes")]
|
||||||
|
public IList<string> Prefixes { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("files")]
|
||||||
|
public IList<string> Urls { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("files")]
|
||||||
|
public IList<UrlWithHeaders> UrlsWithHeaders { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests
|
||||||
|
{
|
||||||
|
internal class UrlWithHeaders
|
||||||
|
{
|
||||||
|
[JsonProperty("headers")]
|
||||||
|
public Dictionary<string, string> Headers { get; set; } = [];
|
||||||
|
|
||||||
|
[JsonProperty("url")]
|
||||||
|
public string Url { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Url with headers to purge.
|
||||||
|
/// </summary>
|
||||||
|
public class ZonePurgeCachedUrlRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Defined headers to specifiy the purge request.
|
||||||
|
/// </summary>
|
||||||
|
public Dictionary<string, string> Headers { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The file url to purge.
|
||||||
|
/// </summary>
|
||||||
|
public string Url { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
189
Extensions/Cloudflare.Zones/Cache/ZoneCacheExtensions.cs
Normal file
189
Extensions/Cloudflare.Zones/Cache/ZoneCacheExtensions.cs
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extends the <see cref="ICloudflareClient"/> with methods for working with zones.
|
||||||
|
/// </summary>
|
||||||
|
public static class ZoneCacheExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Purges all cached contents for a zone.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Removes ALL files from Cloudflare's cache. All tiers can purge everything.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContent(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
var req = new PurgeRequest
|
||||||
|
{
|
||||||
|
PurgeEverything = true
|
||||||
|
};
|
||||||
|
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Purges cached contents by URLs.
|
||||||
|
/// <br />
|
||||||
|
/// Granularly removes one or more files from Cloudflare's cache by specifying URLs.
|
||||||
|
/// All tiers can purge by URL.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// To purge files with custom cache keys, include the headers used to compute the cache key as in the example.
|
||||||
|
/// If you have a device type or geo in your cache key, you will need to include the CF-Device-Type or CF-IPCountry headers.
|
||||||
|
/// If you have lang in your cache key, you will need to include the Accept-Language header.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// <strong>NB</strong>: When including the Origin header, be sure to include the <strong>scheme</strong> and <strong>hostname</strong>.
|
||||||
|
/// The port number can be omitted if it is the default port (80 for http, 443 for https), but must be included otherwise.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// <strong>NB</strong>: For Zones on Free/Pro/Business plan, you may purge up to 30 URLs in one API call.
|
||||||
|
/// For Zones on Enterprise plan, you may purge up to 500 URLs in one API call.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="urls">List of URLs to purge.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByUrl(this ICloudflareClient client, string zoneId, IReadOnlyList<ZonePurgeCachedUrlRequest> urls, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (urls == null)
|
||||||
|
throw new ArgumentNullException(nameof(urls));
|
||||||
|
|
||||||
|
var req = new PurgeRequest();
|
||||||
|
|
||||||
|
if (urls.Any(u => u.Headers.Count > 0))
|
||||||
|
{
|
||||||
|
req.UrlsWithHeaders = urls.Where(u => !string.IsNullOrWhiteSpace(u.Url))
|
||||||
|
.Select(u => new UrlWithHeaders
|
||||||
|
{
|
||||||
|
Url = u.Url,
|
||||||
|
Headers = u.Headers
|
||||||
|
.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Key))
|
||||||
|
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
|
||||||
|
}).ToList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
req.Urls = urls.Where(u => !string.IsNullOrWhiteSpace(u.Url)).Select(u => u.Url).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Purges cached contents by cache-tags.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// For more information on cache tags and purging by tags, please refer to
|
||||||
|
/// <see href="https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-tags/#purge-cache-by-cache-tags-enterprise-only">purge by cache-tags documentation page</see>.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Cache-Tag purging has a rate limit of 30_000 purge API calls in every 24 hour period.
|
||||||
|
/// You may purge up to 30 tags in one API call.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// <em>This rate limit can be raised for customers who need to purge at higher volume.</em>
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="tags">List of tags to purge.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByTag(this ICloudflareClient client, string zoneId, IReadOnlyList<string> tags, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (tags == null)
|
||||||
|
throw new ArgumentNullException(nameof(tags));
|
||||||
|
|
||||||
|
var req = new PurgeRequest
|
||||||
|
{
|
||||||
|
Tags = tags.Where(t => !string.IsNullOrWhiteSpace(t)).ToList()
|
||||||
|
};
|
||||||
|
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Purges cached contents by hosts.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// For more information purging by hostnames, please refer to
|
||||||
|
/// <see href="https://developers.cloudflare.com/cache/how-to/purge-cache/purge-by-hostname/">purge by hostname documentation page</see>.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Host purging has a rate limit of 30_000 purge API calls in every 24 hour period.
|
||||||
|
/// You may purge up to 30 hosts in one API call.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// <em>This rate limit can be raised for customers who need to purge at higher volume.</em>
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="hosts">List of hostnames to purge.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByHost(this ICloudflareClient client, string zoneId, IReadOnlyList<string> hosts, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (hosts == null)
|
||||||
|
throw new ArgumentNullException(nameof(hosts));
|
||||||
|
|
||||||
|
var req = new PurgeRequest
|
||||||
|
{
|
||||||
|
Hostnames = hosts.Where(h => !string.IsNullOrWhiteSpace(h)).ToList()
|
||||||
|
};
|
||||||
|
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Purges cached contents by prefixes.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// For more information purging by prefixes, please refer to
|
||||||
|
/// <see href="https://developers.cloudflare.com/cache/how-to/purge-cache/purge_by_prefix/">purge by prefix documentation page</see>.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// Prefix purging has a rate limit of 30_000 purge API calls in every 24 hour period.
|
||||||
|
/// You may purge up to 30 prefixes in one API call.
|
||||||
|
/// </para>
|
||||||
|
/// <para>
|
||||||
|
/// <em>This rate limit can be raised for customers who need to purge at higher volume.</em>
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="prefixes">List of prefixes to purge.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> PurgeCachedContentByPrefix(this ICloudflareClient client, string zoneId, IReadOnlyList<string> prefixes, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (prefixes == null)
|
||||||
|
throw new ArgumentNullException(nameof(prefixes));
|
||||||
|
|
||||||
|
var req = new PurgeRequest
|
||||||
|
{
|
||||||
|
Prefixes = prefixes.Where(h => !string.IsNullOrWhiteSpace(h)).ToList()
|
||||||
|
};
|
||||||
|
return client.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{zoneId}/purge_cache", req, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,4 +17,8 @@
|
|||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<InternalsVisibleTo Include="Cloudflare.Zones.Tests" PublicKey="$(PublicKey)" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
internal class CreateZoneHoldFilter : IQueryParameterFilter
|
||||||
|
{
|
||||||
|
public bool IncludeSubdomains { get; set; }
|
||||||
|
|
||||||
|
public IDictionary<string, string> GetQueryParameters()
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
if (IncludeSubdomains)
|
||||||
|
dict.Add("include_subdomains", "true");
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
internal class DeleteZoneHoldFilter : IQueryParameterFilter
|
||||||
|
{
|
||||||
|
public DateTime? HoldAfter { get; set; }
|
||||||
|
|
||||||
|
public IDictionary<string, string> GetQueryParameters()
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
if (HoldAfter.HasValue)
|
||||||
|
dict.Add("hold_after", HoldAfter.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
64
Extensions/Cloudflare.Zones/Hold/ZoneHoldExtensions.cs
Normal file
64
Extensions/Cloudflare.Zones/Hold/ZoneHoldExtensions.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extends the <see cref="ICloudflareClient"/> with methods for working with zones.
|
||||||
|
/// </summary>
|
||||||
|
public static class ZoneHoldExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve whether the zone is subject to a zone hold, and metadata about the hold.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneHold>> GetZoneHold(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
return client.GetAsync<ZoneHold>($"zones/{zoneId}/hold", cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enforce a zone hold on the zone, blocking the creation and activation of zones with this zone's hostname.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="includeSubdomains">
|
||||||
|
/// If set, the zone hold will extend to block any subdomain of the given zone, as well as SSL4SaaS Custom Hostnames.
|
||||||
|
/// For example, a zone hold on a zone with the hostname 'example.com' and <paramref name="includeSubdomains"/>=<see langword="true"/> will block 'example.com', 'staging.example.com', 'api.staging.example.com', etc.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneHold>> CreateZoneHold(this ICloudflareClient client, string zoneId, bool includeSubdomains = false, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
var filter = new CreateZoneHoldFilter
|
||||||
|
{
|
||||||
|
IncludeSubdomains = includeSubdomains
|
||||||
|
};
|
||||||
|
return client.PostAsync<ZoneHold, object>($"zones/{zoneId}/hold", null, filter, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Stop enforcement of a zone hold on the zone, permanently or temporarily, allowing the creation and activation of zones with this zone's hostname.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="holdAfter">
|
||||||
|
/// If <paramref name="holdAfter"/> is provided, the hold will be temporarily disabled, then automatically re-enabled by the system at the time specified.
|
||||||
|
/// Otherwise, the hold will be disabled indefinitely.
|
||||||
|
/// </param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneHold>> DeleteZoneHold(this ICloudflareClient client, string zoneId, DateTime? holdAfter = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
var filter = new DeleteZoneHoldFilter
|
||||||
|
{
|
||||||
|
HoldAfter = holdAfter
|
||||||
|
};
|
||||||
|
return client.DeleteAsync<ZoneHold>($"zones/{zoneId}/hold", filter, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
120
Extensions/Cloudflare.Zones/Models/Zone.cs
Normal file
120
Extensions/Cloudflare.Zones/Models/Zone.cs
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A DNS Zone.
|
||||||
|
/// </summary>
|
||||||
|
public class Zone
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Identifier.
|
||||||
|
/// </summary>
|
||||||
|
// <= 32 characters
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The account the zone belongs to.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("account")]
|
||||||
|
public AccountBase Account { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The last time proof of ownership was detected and the zone was made active.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("activated_on")]
|
||||||
|
public DateTime ActivatedOn { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// When the zone was created.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("created_on")]
|
||||||
|
public DateTime CreatedOn { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The interval (in seconds) from when development mode expires (positive integer)
|
||||||
|
/// or last expired (negative integer) for the domain.
|
||||||
|
/// If development mode has never been enabled, this value is 0.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("development_mode")]
|
||||||
|
public int DevelopmentMode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Metadata about the zone.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("meta")]
|
||||||
|
public ZoneMetaData Meta { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// When the zone was last modified.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("modified_on")]
|
||||||
|
public DateTime ModifiedOn { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The domain name.
|
||||||
|
/// </summary>
|
||||||
|
// <= 253 characters
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name servers Cloudflare assigns to a zone.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("name_servers")]
|
||||||
|
public IReadOnlyList<string> NameServers { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DNS host at the time of switching to Cloudflare.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("original_dnshost")]
|
||||||
|
public string OriginalDnshost { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Original name servers before moving to Cloudflare.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("original_name_servers")]
|
||||||
|
public IReadOnlyList<string> OriginalNameServers { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Registrar for the domain at the time of switching to Cloudflare.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("original_registrar")]
|
||||||
|
public string OriginalRegistrar { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The owner of the zone.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("owner")]
|
||||||
|
public OwnerBase Owner { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Indicates whether the zone is only using Cloudflare DNS services.
|
||||||
|
/// A <see langword="true"/> value means the zone will not receive security or performance benefits.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("paused")]
|
||||||
|
public bool Paused { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The zone status on Cloudflare.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("status")]
|
||||||
|
public ZoneStatus Status { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A full zone implies that DNS is hosted with Cloudflare.
|
||||||
|
/// A partial zone is typically a partner-hosted zone or a CNAME setup..
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("type")]
|
||||||
|
public ZoneType Type { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An array of domains used for custom name servers.
|
||||||
|
/// <em>This is only available for Business and Enterprise plans.</em>
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("vanity_name_servers")]
|
||||||
|
public IReadOnlyList<string> VanityNameServers { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Extensions/Cloudflare.Zones/Models/ZoneHold.cs
Normal file
28
Extensions/Cloudflare.Zones/Models/ZoneHold.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A zone hold.
|
||||||
|
/// </summary>
|
||||||
|
public class ZoneHold
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether the zone is on hold.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("hold")]
|
||||||
|
public bool Hold { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets an information whether subdomains are included in the hold.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("include_subdomains")]
|
||||||
|
public string IncludeSubdomains { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the time after which the zone is no longer on hold.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("hold_after")]
|
||||||
|
public DateTime HoldAfter { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Extensions/Cloudflare.Zones/Models/ZoneIdResponse.cs
Normal file
15
Extensions/Cloudflare.Zones/Models/ZoneIdResponse.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The deleted zone.
|
||||||
|
/// </summary>
|
||||||
|
public class ZoneIdResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Identifier.
|
||||||
|
/// </summary>
|
||||||
|
// <= 32 characters
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public string Id { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
50
Extensions/Cloudflare.Zones/Models/ZoneMetaData.cs
Normal file
50
Extensions/Cloudflare.Zones/Models/ZoneMetaData.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The zone metadata.
|
||||||
|
/// </summary>
|
||||||
|
public class ZoneMetaData
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The zone is only configured for CDN.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("cdn_only")]
|
||||||
|
public bool CdnOnly { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Number of Custom Certificates the zone can have.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("custom_certificate_quota")]
|
||||||
|
public int CustomCertificateQuota { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The zone is only configured for DNS.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("dns_only")]
|
||||||
|
public bool DnsOnly { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The zone is setup with Foundation DNS.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("foundation_dns")]
|
||||||
|
public bool FoundationDns { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Number of Page Rules a zone can have.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("page_rule_quota")]
|
||||||
|
public int PageRuleQuota { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The zone has been flagged for phishing.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("phishing_detected")]
|
||||||
|
public bool PhishingDetected { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Step.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("step")]
|
||||||
|
public int Step { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,13 +4,34 @@ With this extension package, you'll get all features available to manage a Zone
|
|||||||
|
|
||||||
## Methods
|
## Methods
|
||||||
|
|
||||||
|
### Zone
|
||||||
|
|
||||||
- [ListZones](https://developers.cloudflare.com/api/operations/zones-get)
|
- [ListZones](https://developers.cloudflare.com/api/operations/zones-get)
|
||||||
- [ZoneDetails](https://developers.cloudflare.com/api/operations/zones-0-get)
|
|
||||||
- [CreateZone](https://developers.cloudflare.com/api/operations/zones-post)
|
- [CreateZone](https://developers.cloudflare.com/api/operations/zones-post)
|
||||||
- [DeleteZone](https://developers.cloudflare.com/api/operations/zones-0-delete)
|
- [DeleteZone](https://developers.cloudflare.com/api/operations/zones-0-delete)
|
||||||
|
- [ZoneDetails](https://developers.cloudflare.com/api/operations/zones-0-get)
|
||||||
- [EditZone](https://developers.cloudflare.com/api/operations/zones-0-patch)
|
- [EditZone](https://developers.cloudflare.com/api/operations/zones-0-patch)
|
||||||
|
- [RerunActivationCheck](https://developers.cloudflare.com/api/operations/put-zones-zone_id-activation_check)
|
||||||
|
- [PurgeCachedContent](https://developers.cloudflare.com/api/operations/zone-purge)
|
||||||
|
|
||||||
|
|
||||||
|
### Zone Holds
|
||||||
|
|
||||||
|
- [DeleteZoneHold](https://developers.cloudflare.com/api/operations/zones-0-hold-delete)
|
||||||
|
- [GetZoneHold](https://developers.cloudflare.com/api/operations/zones-0-hold-get)
|
||||||
|
- [CreateZoneHold](https://developers.cloudflare.com/api/operations/zones-0-hold-post)
|
||||||
|
|
||||||
|
|
||||||
|
### DNS Settings for a Zone
|
||||||
|
|
||||||
- TBD
|
- TBD
|
||||||
|
|
||||||
|
|
||||||
|
### DNS Records for a Zone
|
||||||
|
|
||||||
|
- TBD
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Published under MIT License (see [choose a license])
|
Published under MIT License (see [choose a license])
|
||||||
|
|||||||
9
Extensions/Cloudflare.Zones/RegexPatterns.cs
Normal file
9
Extensions/Cloudflare.Zones/RegexPatterns.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
internal static class RegexPatterns
|
||||||
|
{
|
||||||
|
public static readonly Regex ZoneName = new(@"^([a-zA-Z0-9][\-a-zA-Z0-9]*\.)+[\-a-zA-Z0-9]{2,20}$", RegexOptions.Compiled);
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Extensions/Cloudflare.Zones/Zone/Enums/ZoneStatus.cs
Normal file
36
Extensions/Cloudflare.Zones/Zone/Enums/ZoneStatus.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A zone status.
|
||||||
|
/// </summary>
|
||||||
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
|
public enum ZoneStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Zone is initializing.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "initializing")]
|
||||||
|
Initializing = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Zone is pending.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "pending")]
|
||||||
|
Pending = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Zone is active.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "active")]
|
||||||
|
Active = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Zone has been moved.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "moved")]
|
||||||
|
Moved = 4,
|
||||||
|
}
|
||||||
|
}
|
||||||
46
Extensions/Cloudflare.Zones/Zone/Enums/ZoneType.cs
Normal file
46
Extensions/Cloudflare.Zones/Zone/Enums/ZoneType.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Zone type.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// A full zone implies that DNS is hosted with Cloudflare.
|
||||||
|
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
|
||||||
|
/// </remarks>
|
||||||
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
|
public enum ZoneType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Full Setup (most common).
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Use Cloudflare as your primary DNS provider and manage your DNS records on Cloudflare.
|
||||||
|
/// </remarks>
|
||||||
|
[EnumMember(Value = "full")]
|
||||||
|
Full = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Zone transfers.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Use Cloudflare and another DNS provider together across your entire zone to increase availability and fault tolerance.
|
||||||
|
/// <br />
|
||||||
|
/// DNS records will be transferred between providers using
|
||||||
|
/// <see href="https://datatracker.ietf.org/doc/html/rfc5936">AXFR</see> or <see href="https://datatracker.ietf.org/doc/html/rfc1995">IXFR</see>.
|
||||||
|
/// </remarks>
|
||||||
|
[EnumMember(Value = "secondary")]
|
||||||
|
Secondary = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Partial (CNAME) setup.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Keep your primary DNS provider and only use Cloudflare's reverse proxy for individual subdomains.
|
||||||
|
/// </remarks>
|
||||||
|
[EnumMember(Value = "partial")]
|
||||||
|
Partial = 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Extensions/Cloudflare.Zones/Zone/Enums/ZonesOrderBy.cs
Normal file
36
Extensions/Cloudflare.Zones/Zone/Enums/ZonesOrderBy.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Field to order zones by.
|
||||||
|
/// </summary>
|
||||||
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
|
public enum ZonesOrderBy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Order by zone name.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "name")]
|
||||||
|
Name = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Order by zone status.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "status")]
|
||||||
|
Status = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Order by account ID.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "account.id")]
|
||||||
|
AccountId = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Order by account name.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "account.name")]
|
||||||
|
AccountName = 4,
|
||||||
|
}
|
||||||
|
}
|
||||||
132
Extensions/Cloudflare.Zones/Zone/Filters/ListZonesFilter.cs
Normal file
132
Extensions/Cloudflare.Zones/Zone/Filters/ListZonesFilter.cs
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Filter for listing zones.
|
||||||
|
/// </summary>
|
||||||
|
public class ListZonesFilter : IQueryParameterFilter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An account ID.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>account.id</value>
|
||||||
|
public string AccountId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An account Name.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Optional filter operators can be provided to extend refine the search:
|
||||||
|
/// <list type="bullet">
|
||||||
|
/// <item><description>equal</description> (default)</item>
|
||||||
|
/// <item><description>not_equal</description></item>
|
||||||
|
/// <item><description>starts_with</description></item>
|
||||||
|
/// <item><description>ends_with</description></item>
|
||||||
|
/// <item><description>contains</description></item>
|
||||||
|
/// <item><description>starts_with_case_sensitive</description></item>
|
||||||
|
/// <item><description>ends_with_case_sensitive</description></item>
|
||||||
|
/// <item><description>contains_case_sensitive</description></item>
|
||||||
|
/// </list>
|
||||||
|
/// </remarks>
|
||||||
|
/// <example>Dev Account</example>
|
||||||
|
/// <example>contains:Test</example>
|
||||||
|
/// <value>account.name</value>
|
||||||
|
public string AccountName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Direction to order zones.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>direction</value>
|
||||||
|
public SortDirection? OrderDirection { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether to match all search requirements or at least one (any).
|
||||||
|
/// </summary>
|
||||||
|
/// <value>match</value>
|
||||||
|
public FilterMatchType? MatchType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A domain name.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Optional filter operators can be provided to extend refine the search:
|
||||||
|
/// <list type="bullet">
|
||||||
|
/// <item><description>equal</description> (default)</item>
|
||||||
|
/// <item><description>not_equal</description></item>
|
||||||
|
/// <item><description>starts_with</description></item>
|
||||||
|
/// <item><description>ends_with</description></item>
|
||||||
|
/// <item><description>contains</description></item>
|
||||||
|
/// <item><description>starts_with_case_sensitive</description></item>
|
||||||
|
/// <item><description>ends_with_case_sensitive</description></item>
|
||||||
|
/// <item><description>contains_case_sensitive</description></item>
|
||||||
|
/// </list>
|
||||||
|
/// </remarks>
|
||||||
|
/// <example>example.com</example>
|
||||||
|
/// <example>contains:.org</example>
|
||||||
|
/// <example>ends_with:arpa</example>
|
||||||
|
/// <example>starts_with:dev</example>
|
||||||
|
/// <value>name</value>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Field to order zones by.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>order</value>
|
||||||
|
public ZonesOrderBy? OrderBy { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Page number of paginated results.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>page</value>
|
||||||
|
public int? Page { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Number of zones per page.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>per_page</value>
|
||||||
|
public int? PerPage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A zone status.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>status</value>
|
||||||
|
public ZoneStatus? Status { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IDictionary<string, string> GetQueryParameters()
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(AccountId))
|
||||||
|
dict.Add("account.id", AccountId);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(AccountName))
|
||||||
|
dict.Add("account.name", AccountName);
|
||||||
|
|
||||||
|
if (OrderDirection.HasValue && Enum.IsDefined(typeof(SortDirection), OrderDirection.Value))
|
||||||
|
dict.Add("direction", OrderDirection.Value.GetEnumMemberValue());
|
||||||
|
|
||||||
|
if (MatchType.HasValue && Enum.IsDefined(typeof(FilterMatchType), MatchType.Value))
|
||||||
|
dict.Add("match", MatchType.Value.GetEnumMemberValue());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(Name))
|
||||||
|
dict.Add("name", Name);
|
||||||
|
|
||||||
|
if (OrderBy.HasValue && Enum.IsDefined(typeof(ZonesOrderBy), OrderBy.Value))
|
||||||
|
dict.Add("order", OrderBy.Value.GetEnumMemberValue());
|
||||||
|
|
||||||
|
if (Page.HasValue && Page.Value >= 1)
|
||||||
|
dict.Add("page", Page.Value.ToString());
|
||||||
|
|
||||||
|
if (PerPage.HasValue && PerPage.Value >= 5 && PerPage.Value <= 50)
|
||||||
|
dict.Add("per_page", PerPage.Value.ToString());
|
||||||
|
|
||||||
|
if (Status.HasValue && Enum.IsDefined(typeof(ZoneStatus), Status.Value))
|
||||||
|
dict.Add("status", Status.Value.GetEnumMemberValue());
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests
|
||||||
|
{
|
||||||
|
internal class CreateRequest
|
||||||
|
{
|
||||||
|
[JsonProperty("account")]
|
||||||
|
public AccountBase Account { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("type")]
|
||||||
|
public ZoneType Type { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests
|
||||||
|
{
|
||||||
|
internal class EditRequest
|
||||||
|
{
|
||||||
|
[JsonProperty("type")]
|
||||||
|
public ZoneType? Type { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("vanity_name_servers")]
|
||||||
|
public IList<string> VanityNameServers { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Request to create a new zone.
|
||||||
|
/// </summary>
|
||||||
|
public class CreateZoneRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The account identifier.
|
||||||
|
/// </summary>
|
||||||
|
public string AccountId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The domain name.
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The zone type.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// A full zone implies that DNS is hosted with Cloudflare.
|
||||||
|
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
|
||||||
|
/// </remarks>
|
||||||
|
public ZoneType Type { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
29
Extensions/Cloudflare.Zones/Zone/Requests/EditZoneRequest.cs
Normal file
29
Extensions/Cloudflare.Zones/Zone/Requests/EditZoneRequest.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A request to edit a zone.
|
||||||
|
/// </summary>
|
||||||
|
public class EditZoneRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Identifier.
|
||||||
|
/// </summary>
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A full zone implies that DNS is hosted with Cloudflare. A partial zone is typically a partner-hosted zone or a CNAME setup.
|
||||||
|
/// <br/>
|
||||||
|
/// <em>This parameter is only available to Enterprise customers or if it has been explicitly enabled on a zone.</em>
|
||||||
|
/// </summary>
|
||||||
|
public ZoneType? Type { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An array of domains used for custom name servers.
|
||||||
|
/// <br/>
|
||||||
|
/// <em>This is only available for Business and Enterprise plans.</em>
|
||||||
|
/// </summary>
|
||||||
|
public IList<string> VanityNameServers { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
127
Extensions/Cloudflare.Zones/Zone/ZoneExtensions.cs
Normal file
127
Extensions/Cloudflare.Zones/Zone/ZoneExtensions.cs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Zones
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extends the <see cref="ICloudflareClient"/> with methods for working with zones.
|
||||||
|
/// </summary>
|
||||||
|
public static class ZoneExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Lists, searches, sorts, and filters your zones.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="options">Filter options (optional).</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<IReadOnlyList<Zone>>> ListZones(this ICloudflareClient client, ListZonesFilter options = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return client.GetAsync<IReadOnlyList<Zone>>("zones", options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get details for a zone.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<Zone>> ZoneDetails(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
return client.GetAsync<Zone>($"zones/{zoneId}", cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a new zone.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="request">The request information.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<Zone>> CreateZone(this ICloudflareClient client, CreateZoneRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (request == null)
|
||||||
|
throw new ArgumentNullException(nameof(request));
|
||||||
|
|
||||||
|
request.AccountId.ValidateCloudflareId();
|
||||||
|
request.Name.ValidateCloudflareName();
|
||||||
|
|
||||||
|
if (!RegexPatterns.ZoneName.IsMatch(request.Name))
|
||||||
|
throw new ArgumentException("Does not match the zone name pattern", nameof(request.Name));
|
||||||
|
|
||||||
|
if (!Enum.IsDefined(typeof(ZoneType), request.Type))
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(request.Type));
|
||||||
|
|
||||||
|
var req = new CreateRequest
|
||||||
|
{
|
||||||
|
Account = new AccountBase { Id = request.AccountId },
|
||||||
|
Name = request.Name,
|
||||||
|
Type = request.Type
|
||||||
|
};
|
||||||
|
|
||||||
|
return client.PostAsync<Zone, CreateRequest>("zones", req, cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes an existing zone.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> DeleteZone(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
return client.DeleteAsync<ZoneIdResponse>($"zones/{zoneId}", cancellationToken: cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Edits a zone.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Only one zone property can be changed at a time.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="request">The request information.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<Zone>> EditZone(this ICloudflareClient client, EditZoneRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
request.Id.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (request.Type.HasValue && request.VanityNameServers != null)
|
||||||
|
throw new CloudflareException("Only one zone property can be changed at a time.");
|
||||||
|
|
||||||
|
if (request.Type.HasValue && !Enum.IsDefined(typeof(ZoneType), request.Type.Value))
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(request.Type));
|
||||||
|
|
||||||
|
var req = new EditRequest();
|
||||||
|
|
||||||
|
if (request.Type.HasValue)
|
||||||
|
req.Type = request.Type.Value;
|
||||||
|
|
||||||
|
if (request.VanityNameServers != null)
|
||||||
|
req.VanityNameServers = request.VanityNameServers.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
|
||||||
|
|
||||||
|
return client.PatchAsync<Zone, EditRequest>($"zones/{request.Id}", req, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Triggeres a new activation check for a PENDING Zone. This can be triggered every 5 min for paygo/ent customers, every hour for FREE Zones.
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Triggeres a new activation check for a <see cref="ZoneStatus.Pending"/> zone.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This can be triggered every 5 min for paygo/enterprise customers, every hour for FREE Zones.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/>.</param>
|
||||||
|
/// <param name="zoneId">The zone ID.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<ZoneIdResponse>> RerunActivationCheck(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
zoneId.ValidateCloudflareId();
|
||||||
|
return client.PutAsync<ZoneIdResponse, object>($"zones/{zoneId}/activation_check", null, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,19 +32,15 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" PublicKey="0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="$(SolutionDir)/package-icon.png" Pack="true" PackagePath="/" />
|
<None Include="$(SolutionDir)/package-icon.png" Pack="true" PackagePath="/" />
|
||||||
<None Include="README.md" Pack="true" PackagePath="/" />
|
<None Include="README.md" Pack="true" PackagePath="/" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(Configuration)' == 'Release'">
|
|
||||||
<PackageReference Include="AMWD.Net.API.Cloudflare" Version="0.0.1" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
|
|
||||||
<ProjectReference Include="$(SolutionDir)\Cloudflare\Cloudflare.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AMWD.NetRevisionTask" Version="1.2.1">
|
<PackageReference Include="AMWD.NetRevisionTask" Version="1.2.1">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
@@ -52,5 +48,12 @@
|
|||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition="'$(Configuration)' == 'Release'">
|
||||||
|
<PackageReference Include="AMWD.Net.API.Cloudflare" Version="0.1.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
|
||||||
|
<ProjectReference Include="$(SolutionDir)\Cloudflare\Cloudflare.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
|
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Cache
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class PurgeCachedContentByHostTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, PurgeRequest Request, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPurgeCachedContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var list = new List<string>
|
||||||
|
{
|
||||||
|
"www.example.com",
|
||||||
|
"example.org",
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PurgeCachedContentByHost(ZoneId, list);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/purge_cache", callback.RequestPath);
|
||||||
|
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
Assert.IsNull(callback.Request.PurgeEverything);
|
||||||
|
Assert.IsNull(callback.Request.Urls);
|
||||||
|
Assert.IsNull(callback.Request.UrlsWithHeaders);
|
||||||
|
Assert.IsNull(callback.Request.Tags);
|
||||||
|
Assert.IsNotNull(callback.Request.Hostnames);
|
||||||
|
Assert.AreEqual(2, callback.Request.Hostnames.Count);
|
||||||
|
Assert.AreEqual("www.example.com", callback.Request.Hostnames[0]);
|
||||||
|
Assert.AreEqual("example.org", callback.Request.Hostnames[1]);
|
||||||
|
Assert.IsNull(callback.Request.Prefixes);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{ZoneId}/purge_cache", It.IsAny<PurgeRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumenNullExceptionIfListIsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.PurgeCachedContentByHost(ZoneId, null);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<ZoneIdResponse, PurgeRequest>(It.IsAny<string>(), It.IsAny<PurgeRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, PurgeRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, queryFilter, _) => _callbacks.Add((requestPath, request, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Cache
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class PurgeCachedContentByPrefixTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, PurgeRequest Request, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPurgeCachedContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var list = new List<string>
|
||||||
|
{
|
||||||
|
null,
|
||||||
|
"example.com/foo",
|
||||||
|
"example.com/bar",
|
||||||
|
"example.org/hello/world",
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PurgeCachedContentByPrefix(ZoneId, list);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/purge_cache", callback.RequestPath);
|
||||||
|
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
Assert.IsNull(callback.Request.PurgeEverything);
|
||||||
|
Assert.IsNull(callback.Request.Urls);
|
||||||
|
Assert.IsNull(callback.Request.UrlsWithHeaders);
|
||||||
|
Assert.IsNull(callback.Request.Tags);
|
||||||
|
Assert.IsNull(callback.Request.Hostnames);
|
||||||
|
Assert.IsNotNull(callback.Request.Prefixes);
|
||||||
|
Assert.AreEqual(3, callback.Request.Prefixes.Count);
|
||||||
|
Assert.AreEqual("example.com/foo", callback.Request.Prefixes[0]);
|
||||||
|
Assert.AreEqual("example.com/bar", callback.Request.Prefixes[1]);
|
||||||
|
Assert.AreEqual("example.org/hello/world", callback.Request.Prefixes[2]);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{ZoneId}/purge_cache", It.IsAny<PurgeRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumenNullExceptionIfListIsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.PurgeCachedContentByPrefix(ZoneId, null);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<ZoneIdResponse, PurgeRequest>(It.IsAny<string>(), It.IsAny<PurgeRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, PurgeRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, queryFilter, _) => _callbacks.Add((requestPath, request, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Cache
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class PurgeCachedContentByTagTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, PurgeRequest Request, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPurgeCachedContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var list = new List<string>
|
||||||
|
{
|
||||||
|
"Tag-1",
|
||||||
|
"",
|
||||||
|
"Tag-2",
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PurgeCachedContentByTag(ZoneId, list);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/purge_cache", callback.RequestPath);
|
||||||
|
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
Assert.IsNull(callback.Request.PurgeEverything);
|
||||||
|
Assert.IsNull(callback.Request.Urls);
|
||||||
|
Assert.IsNull(callback.Request.UrlsWithHeaders);
|
||||||
|
Assert.IsNotNull(callback.Request.Tags);
|
||||||
|
Assert.AreEqual(2, callback.Request.Tags.Count);
|
||||||
|
Assert.AreEqual("Tag-1", callback.Request.Tags[0]);
|
||||||
|
Assert.AreEqual("Tag-2", callback.Request.Tags[1]);
|
||||||
|
Assert.IsNull(callback.Request.Hostnames);
|
||||||
|
Assert.IsNull(callback.Request.Prefixes);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{ZoneId}/purge_cache", It.IsAny<PurgeRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumenNullExceptionIfListIsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.PurgeCachedContentByTag(ZoneId, null);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<ZoneIdResponse, PurgeRequest>(It.IsAny<string>(), It.IsAny<PurgeRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, PurgeRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, queryFilter, _) => _callbacks.Add((requestPath, request, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,165 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Cache
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class PurgeCachedContentByUrlTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, PurgeRequest Request, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPurgeCachedContentByUrl()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var list = new List<ZonePurgeCachedUrlRequest>
|
||||||
|
{
|
||||||
|
new ZonePurgeCachedUrlRequest { Url = "https://example.com/foo.txt" },
|
||||||
|
new ZonePurgeCachedUrlRequest { Url = "https://example.com/bar.baz" },
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PurgeCachedContentByUrl(ZoneId, list);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/purge_cache", callback.RequestPath);
|
||||||
|
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
Assert.IsNull(callback.Request.PurgeEverything);
|
||||||
|
Assert.IsNotNull(callback.Request.Urls);
|
||||||
|
Assert.AreEqual(2, callback.Request.Urls.Count);
|
||||||
|
Assert.AreEqual("https://example.com/foo.txt", callback.Request.Urls[0]);
|
||||||
|
Assert.AreEqual("https://example.com/bar.baz", callback.Request.Urls[1]);
|
||||||
|
Assert.IsNull(callback.Request.UrlsWithHeaders);
|
||||||
|
Assert.IsNull(callback.Request.Tags);
|
||||||
|
Assert.IsNull(callback.Request.Hostnames);
|
||||||
|
Assert.IsNull(callback.Request.Prefixes);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{ZoneId}/purge_cache", It.IsAny<PurgeRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPurgeCachedContentByUrlWithHeader()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var list = new List<ZonePurgeCachedUrlRequest>
|
||||||
|
{
|
||||||
|
new ZonePurgeCachedUrlRequest { Url = "https://example.com/foo.txt", Headers = new Dictionary<string, string> { { "X-Test1", "Test" } } },
|
||||||
|
new ZonePurgeCachedUrlRequest { Url = "https://example.com/bar.baz", Headers = new Dictionary<string, string> { { "X-Test2", "Test" } } },
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PurgeCachedContentByUrl(ZoneId, list);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/purge_cache", callback.RequestPath);
|
||||||
|
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
Assert.IsNull(callback.Request.PurgeEverything);
|
||||||
|
Assert.IsNull(callback.Request.Urls);
|
||||||
|
Assert.IsNotNull(callback.Request.UrlsWithHeaders);
|
||||||
|
Assert.AreEqual(2, callback.Request.UrlsWithHeaders.Count);
|
||||||
|
Assert.AreEqual("https://example.com/foo.txt", callback.Request.UrlsWithHeaders[0].Url);
|
||||||
|
Assert.AreEqual("Test", callback.Request.UrlsWithHeaders[0].Headers["X-Test1"]);
|
||||||
|
Assert.AreEqual("https://example.com/bar.baz", callback.Request.UrlsWithHeaders[1].Url);
|
||||||
|
Assert.AreEqual("Test", callback.Request.UrlsWithHeaders[1].Headers["X-Test2"]);
|
||||||
|
Assert.IsNull(callback.Request.Tags);
|
||||||
|
Assert.IsNull(callback.Request.Hostnames);
|
||||||
|
Assert.IsNull(callback.Request.Prefixes);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{ZoneId}/purge_cache", It.IsAny<PurgeRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumenNullExceptionIfListIsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.PurgeCachedContentByUrl(ZoneId, null);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<ZoneIdResponse, PurgeRequest>(It.IsAny<string>(), It.IsAny<PurgeRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, PurgeRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, queryFilter, _) => _callbacks.Add((requestPath, request, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Cache.InternalRequests;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Cache
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class PurgeCachedContentTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, PurgeRequest Request, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldPurgeCachedContent()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.PurgeCachedContent(ZoneId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/purge_cache", callback.RequestPath);
|
||||||
|
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
Assert.IsNotNull(callback.Request.PurgeEverything);
|
||||||
|
Assert.IsTrue(callback.Request.PurgeEverything.Value);
|
||||||
|
Assert.IsNull(callback.Request.Urls);
|
||||||
|
Assert.IsNull(callback.Request.UrlsWithHeaders);
|
||||||
|
Assert.IsNull(callback.Request.Tags);
|
||||||
|
Assert.IsNull(callback.Request.Hostnames);
|
||||||
|
Assert.IsNull(callback.Request.Prefixes);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneIdResponse, PurgeRequest>($"zones/{ZoneId}/purge_cache", It.IsAny<PurgeRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<ZoneIdResponse, PurgeRequest>(It.IsAny<string>(), It.IsAny<PurgeRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, PurgeRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, queryFilter, _) => _callbacks.Add((requestPath, request, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
150
UnitTests/Cloudflare.Zones.Tests/Hold/CreateZoneHoldTest.cs
Normal file
150
UnitTests/Cloudflare.Zones.Tests/Hold/CreateZoneHoldTest.cs
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Hold
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class CreateZoneHoldTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneHold> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, object Request, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneHold>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneHold
|
||||||
|
{
|
||||||
|
Hold = true,
|
||||||
|
HoldAfter = DateTime,
|
||||||
|
IncludeSubdomains = "true"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldCreateZoneHold()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.CreateZoneHold(ZoneId, includeSubdomains: false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/hold", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType<CreateZoneHoldFilter>(callback.QueryFilter);
|
||||||
|
Assert.IsFalse(((CreateZoneHoldFilter)callback.QueryFilter).IncludeSubdomains);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneHold, object>($"zones/{ZoneId}/hold", null, It.IsAny<CreateZoneHoldFilter>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldCreateZoneHoldWithSubdomains()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.CreateZoneHold(ZoneId, includeSubdomains: true);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/hold", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType<CreateZoneHoldFilter>(callback.QueryFilter);
|
||||||
|
Assert.IsTrue(((CreateZoneHoldFilter)callback.QueryFilter).IncludeSubdomains);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<ZoneHold, object>($"zones/{ZoneId}/hold", null, It.IsAny<CreateZoneHoldFilter>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnEmptyDictionary()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new CreateZoneHoldFilter();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnQueryParameter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new CreateZoneHoldFilter { IncludeSubdomains = true };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(1, dict.Count);
|
||||||
|
Assert.IsTrue(dict.ContainsKey("include_subdomains"));
|
||||||
|
Assert.AreEqual("true", dict["include_subdomains"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<ZoneHold, object>(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, object, IQueryParameterFilter, CancellationToken>((requestPath, request, queryFilter, _) => _callbacks.Add((requestPath, request, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
150
UnitTests/Cloudflare.Zones.Tests/Hold/DeleteZoneHoldTest.cs
Normal file
150
UnitTests/Cloudflare.Zones.Tests/Hold/DeleteZoneHoldTest.cs
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Hold
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class DeleteZoneHoldTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime Date = new(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneHold> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneHold>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneHold
|
||||||
|
{
|
||||||
|
Hold = true,
|
||||||
|
HoldAfter = Date,
|
||||||
|
IncludeSubdomains = "true"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldDeleteZoneHold()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.DeleteZoneHold(ZoneId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/hold", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType<DeleteZoneHoldFilter>(callback.QueryFilter);
|
||||||
|
Assert.IsNull(((DeleteZoneHoldFilter)callback.QueryFilter).HoldAfter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.DeleteAsync<ZoneHold>($"zones/{ZoneId}/hold", It.IsAny<DeleteZoneHoldFilter>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldDeleteZoneHoldTemporarily()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.DeleteZoneHold(ZoneId, holdAfter: Date);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/hold", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
Assert.IsInstanceOfType<DeleteZoneHoldFilter>(callback.QueryFilter);
|
||||||
|
Assert.AreEqual(Date, ((DeleteZoneHoldFilter)callback.QueryFilter).HoldAfter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.DeleteAsync<ZoneHold>($"zones/{ZoneId}/hold", It.IsAny<DeleteZoneHoldFilter>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnEmptyDictionary()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new DeleteZoneHoldFilter();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnQueryParameter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new DeleteZoneHoldFilter { HoldAfter = Date };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(1, dict.Count);
|
||||||
|
Assert.IsTrue(dict.ContainsKey("hold_after"));
|
||||||
|
Assert.AreEqual("2024-10-10T20:30:40Z", dict["hold_after"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.DeleteAsync<ZoneHold>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
90
UnitTests/Cloudflare.Zones.Tests/Hold/GetZoneHoldTest.cs
Normal file
90
UnitTests/Cloudflare.Zones.Tests/Hold/GetZoneHoldTest.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Hold
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class GetZoneHoldTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private readonly DateTime DateTime = new DateTime(2024, 10, 10, 20, 30, 40, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneHold> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneHold>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneHold
|
||||||
|
{
|
||||||
|
Hold = true,
|
||||||
|
HoldAfter = DateTime,
|
||||||
|
IncludeSubdomains = "false"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldGetZoneHold()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.GetZoneHold(ZoneId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/hold", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<ZoneHold>($"zones/{ZoneId}/hold", null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.GetAsync<ZoneHold>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
192
UnitTests/Cloudflare.Zones.Tests/Zones/CreateZoneTest.cs
Normal file
192
UnitTests/Cloudflare.Zones.Tests/Zones/CreateZoneTest.cs
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Zones
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class CreateZoneTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<Zone> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, CreateRequest Request)> _callbacks;
|
||||||
|
|
||||||
|
private CreateZoneRequest _request;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<Zone>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new Zone
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Account = new AccountBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Account Name"
|
||||||
|
},
|
||||||
|
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
|
||||||
|
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
DevelopmentMode = 7200,
|
||||||
|
Meta = new ZoneMetaData
|
||||||
|
{
|
||||||
|
CdnOnly = true,
|
||||||
|
CustomCertificateQuota = 1,
|
||||||
|
DnsOnly = true,
|
||||||
|
FoundationDns = true,
|
||||||
|
PageRuleQuota = 100,
|
||||||
|
PhishingDetected = false,
|
||||||
|
Step = 2
|
||||||
|
},
|
||||||
|
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
Name = "example.com",
|
||||||
|
NameServers =
|
||||||
|
[
|
||||||
|
"bob.ns.cloudflare.com",
|
||||||
|
"lola.ns.cloudflare.com"
|
||||||
|
],
|
||||||
|
OriginalDnshost = "NameCheap",
|
||||||
|
OriginalNameServers =
|
||||||
|
[
|
||||||
|
"ns1.originaldnshost.com",
|
||||||
|
"ns2.originaldnshost.com"
|
||||||
|
],
|
||||||
|
OriginalRegistrar = "GoDaddy",
|
||||||
|
Owner = new OwnerBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Org",
|
||||||
|
Type = "organization"
|
||||||
|
},
|
||||||
|
Paused = true,
|
||||||
|
Status = ZoneStatus.Initializing,
|
||||||
|
Type = ZoneType.Full,
|
||||||
|
VanityNameServers =
|
||||||
|
[
|
||||||
|
"ns1.example.com",
|
||||||
|
"ns2.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_request = new CreateZoneRequest
|
||||||
|
{
|
||||||
|
AccountId = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "example.com",
|
||||||
|
Type = ZoneType.Full
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldReturnCreatedZone()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.CreateZone(_request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual("zones", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
|
||||||
|
Assert.AreEqual(_request.AccountId, callback.Request.Account.Id);
|
||||||
|
Assert.AreEqual(_request.Name, callback.Request.Name);
|
||||||
|
Assert.AreEqual(_request.Type, callback.Request.Type);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<Zone, CreateRequest>("zones", It.IsAny<CreateRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumentNullExceptionWhenRequestIsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.CreateZone(null);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(".internal")]
|
||||||
|
[DataRow("test@example")]
|
||||||
|
[DataRow("test@example.com")]
|
||||||
|
[DataRow("häppi.example.com")]
|
||||||
|
[ExpectedException(typeof(ArgumentException))]
|
||||||
|
public async Task ShouldThrowArgumentExceptionForInvalidName(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Name = name;
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.CreateZone(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidType()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Type = 0;
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.CreateZone(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<Zone, CreateRequest>(It.IsAny<string>(), It.IsAny<CreateRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, CreateRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, _) => _callbacks.Add((requestPath, request)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
86
UnitTests/Cloudflare.Zones.Tests/Zones/DeleteZoneTest.cs
Normal file
86
UnitTests/Cloudflare.Zones.Tests/Zones/DeleteZoneTest.cs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Zones
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class DeleteZoneTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldDeleteZone()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.DeleteZone(ZoneId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.DeleteAsync<ZoneIdResponse>($"zones/{ZoneId}", It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.DeleteAsync<ZoneIdResponse>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
206
UnitTests/Cloudflare.Zones.Tests/Zones/EditZoneTest.cs
Normal file
206
UnitTests/Cloudflare.Zones.Tests/Zones/EditZoneTest.cs
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones.Zones.InternalRequests;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Zones
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class EditZoneTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<Zone> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, EditRequest Request)> _callbacks;
|
||||||
|
|
||||||
|
private EditZoneRequest _request;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<Zone>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new Zone
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Account = new AccountBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Account Name"
|
||||||
|
},
|
||||||
|
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
|
||||||
|
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
DevelopmentMode = 7200,
|
||||||
|
Meta = new ZoneMetaData
|
||||||
|
{
|
||||||
|
CdnOnly = true,
|
||||||
|
CustomCertificateQuota = 1,
|
||||||
|
DnsOnly = true,
|
||||||
|
FoundationDns = true,
|
||||||
|
PageRuleQuota = 100,
|
||||||
|
PhishingDetected = false,
|
||||||
|
Step = 2
|
||||||
|
},
|
||||||
|
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
Name = "example.com",
|
||||||
|
NameServers =
|
||||||
|
[
|
||||||
|
"bob.ns.cloudflare.com",
|
||||||
|
"lola.ns.cloudflare.com"
|
||||||
|
],
|
||||||
|
OriginalDnshost = "NameCheap",
|
||||||
|
OriginalNameServers =
|
||||||
|
[
|
||||||
|
"ns1.originaldnshost.com",
|
||||||
|
"ns2.originaldnshost.com"
|
||||||
|
],
|
||||||
|
OriginalRegistrar = "GoDaddy",
|
||||||
|
Owner = new OwnerBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Org",
|
||||||
|
Type = "organization"
|
||||||
|
},
|
||||||
|
Paused = true,
|
||||||
|
Status = ZoneStatus.Initializing,
|
||||||
|
Type = ZoneType.Full,
|
||||||
|
VanityNameServers =
|
||||||
|
[
|
||||||
|
"ns1.example.com",
|
||||||
|
"ns2.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_request = new EditZoneRequest
|
||||||
|
{
|
||||||
|
Id = ZoneId,
|
||||||
|
Type = ZoneType.Full,
|
||||||
|
VanityNameServers = ["ns1.example.org", "ns2.example.org"]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldReturnModifiedZoneForType()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.VanityNameServers = null;
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.EditZone(_request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
|
||||||
|
Assert.AreEqual(_request.Type.Value, callback.Request.Type.Value);
|
||||||
|
Assert.IsNull(callback.Request.VanityNameServers);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PatchAsync<Zone, EditRequest>($"zones/{ZoneId}", It.IsAny<EditRequest>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldReturnModifiedZoneForVanityNameServers()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Type = null;
|
||||||
|
_request.VanityNameServers.Add("");
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.EditZone(_request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
|
||||||
|
Assert.IsNull(callback.Request.Type);
|
||||||
|
Assert.AreEqual(2, callback.Request.VanityNameServers.Count);
|
||||||
|
Assert.IsTrue(callback.Request.VanityNameServers.Contains("ns1.example.org"));
|
||||||
|
Assert.IsTrue(callback.Request.VanityNameServers.Contains("ns2.example.org"));
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PatchAsync<Zone, EditRequest>($"zones/{ZoneId}", It.IsAny<EditRequest>(), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(CloudflareException))]
|
||||||
|
public async Task ShouldThrowCloudflareExceptionOnMultiplePropertiesSet()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.EditZone(_request);
|
||||||
|
|
||||||
|
// Assert - CloudflareException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidType()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Type = 0;
|
||||||
|
_request.VanityNameServers = null;
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.EditZone(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PatchAsync<Zone, EditRequest>(It.IsAny<string>(), It.IsAny<EditRequest>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, EditRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
393
UnitTests/Cloudflare.Zones.Tests/Zones/ListZonesTest.cs
Normal file
393
UnitTests/Cloudflare.Zones.Tests/Zones/ListZonesTest.cs
Normal file
@@ -0,0 +1,393 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Zones
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ListZonesTest
|
||||||
|
{
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<IReadOnlyList<Zone>> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<IReadOnlyList<Zone>>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
ResultInfo = new PaginationInfo
|
||||||
|
{
|
||||||
|
Count = 1,
|
||||||
|
Page = 1,
|
||||||
|
PerPage = 20,
|
||||||
|
TotalCount = 2000
|
||||||
|
},
|
||||||
|
Result =
|
||||||
|
[
|
||||||
|
new Zone
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Account = new AccountBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Account Name"
|
||||||
|
},
|
||||||
|
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
|
||||||
|
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
DevelopmentMode = 7200,
|
||||||
|
Meta = new ZoneMetaData
|
||||||
|
{
|
||||||
|
CdnOnly = true,
|
||||||
|
CustomCertificateQuota = 1,
|
||||||
|
DnsOnly = true,
|
||||||
|
FoundationDns = true,
|
||||||
|
PageRuleQuota = 100,
|
||||||
|
PhishingDetected = false,
|
||||||
|
Step = 2
|
||||||
|
},
|
||||||
|
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
Name = "example.com",
|
||||||
|
NameServers =
|
||||||
|
[
|
||||||
|
"bob.ns.cloudflare.com",
|
||||||
|
"lola.ns.cloudflare.com"
|
||||||
|
],
|
||||||
|
OriginalDnshost = "NameCheap",
|
||||||
|
OriginalNameServers =
|
||||||
|
[
|
||||||
|
"ns1.originaldnshost.com",
|
||||||
|
"ns2.originaldnshost.com"
|
||||||
|
],
|
||||||
|
OriginalRegistrar = "GoDaddy",
|
||||||
|
Owner = new OwnerBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Org",
|
||||||
|
Type = "organization"
|
||||||
|
},
|
||||||
|
Paused = true,
|
||||||
|
Status = ZoneStatus.Initializing,
|
||||||
|
Type = ZoneType.Full,
|
||||||
|
VanityNameServers =
|
||||||
|
[
|
||||||
|
"ns1.example.com",
|
||||||
|
"ns2.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldReturnListOfZones()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.ListZones();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual("zones", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<IReadOnlyList<Zone>>("zones", null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldReturnListOfZonesWithFilter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
AccountId = "023e105f4ecef8ad9ca31a8372d0c353"
|
||||||
|
};
|
||||||
|
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.ListZones(filter);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual("zones", callback.RequestPath);
|
||||||
|
Assert.AreEqual(filter, callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<IReadOnlyList<Zone>>("zones", filter, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnEmptyParameterList()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnFullParameterList()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
AccountId = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
AccountName = "Example Account Name",
|
||||||
|
MatchType = FilterMatchType.Any,
|
||||||
|
Name = "example.com",
|
||||||
|
PerPage = 13,
|
||||||
|
Page = 5,
|
||||||
|
OrderBy = ZonesOrderBy.AccountName,
|
||||||
|
OrderDirection = SortDirection.Descending,
|
||||||
|
Status = ZoneStatus.Active
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(9, dict.Count);
|
||||||
|
|
||||||
|
Assert.IsTrue(dict.ContainsKey("account.id"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("account.name"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("direction"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("match"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("name"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("order"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("page"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("per_page"));
|
||||||
|
Assert.IsTrue(dict.ContainsKey("status"));
|
||||||
|
|
||||||
|
Assert.AreEqual("023e105f4ecef8ad9ca31a8372d0c353", dict["account.id"]);
|
||||||
|
Assert.AreEqual("Example Account Name", dict["account.name"]);
|
||||||
|
Assert.AreEqual("desc", dict["direction"]);
|
||||||
|
Assert.AreEqual("any", dict["match"]);
|
||||||
|
Assert.AreEqual("example.com", dict["name"]);
|
||||||
|
Assert.AreEqual("account.name", dict["order"]);
|
||||||
|
Assert.AreEqual("5", dict["page"]);
|
||||||
|
Assert.AreEqual("13", dict["per_page"]);
|
||||||
|
Assert.AreEqual("active", dict["status"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddAccountId(string id)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
AccountId = id
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddAccountName(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
AccountName = name
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldNotAddDirection()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
OrderDirection = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldNotAddMatch()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
MatchType = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddName(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
Name = name
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = new Dictionary<string, object>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldNotAddOrder()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
OrderBy = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldNotAddPage()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
Page = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(4)]
|
||||||
|
[DataRow(51)]
|
||||||
|
public void ShouldNotAddPerPage(int perPage)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
PerPage = perPage
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldNotAddStatus()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListZonesFilter
|
||||||
|
{
|
||||||
|
Status = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.GetAsync<IReadOnlyList<Zone>>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Zones
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class RerunActivationCheckTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<ZoneIdResponse> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, object Request)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<ZoneIdResponse>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new ZoneIdResponse
|
||||||
|
{
|
||||||
|
Id = ZoneId
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldRerunActivationCheck()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.RerunActivationCheck(ZoneId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}/activation_check", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.Request);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PutAsync<ZoneIdResponse, object>($"zones/{ZoneId}/activation_check", null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PutAsync<ZoneIdResponse, object>(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, object, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
163
UnitTests/Cloudflare.Zones.Tests/Zones/ZoneDetailsTest.cs
Normal file
163
UnitTests/Cloudflare.Zones.Tests/Zones/ZoneDetailsTest.cs
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AMWD.Net.Api.Cloudflare;
|
||||||
|
using AMWD.Net.Api.Cloudflare.Zones;
|
||||||
|
using Moq;
|
||||||
|
|
||||||
|
namespace Cloudflare.Zones.Tests.Zones
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ZoneDetailsTest
|
||||||
|
{
|
||||||
|
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
|
||||||
|
private CloudflareResponse<Zone> _response;
|
||||||
|
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<Zone>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Message 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo
|
||||||
|
{
|
||||||
|
Code = 1000,
|
||||||
|
Message = "Error 1",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
Result = new Zone
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Account = new AccountBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Account Name"
|
||||||
|
},
|
||||||
|
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
|
||||||
|
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
DevelopmentMode = 7200,
|
||||||
|
Meta = new ZoneMetaData
|
||||||
|
{
|
||||||
|
CdnOnly = true,
|
||||||
|
CustomCertificateQuota = 1,
|
||||||
|
DnsOnly = true,
|
||||||
|
FoundationDns = true,
|
||||||
|
PageRuleQuota = 100,
|
||||||
|
PhishingDetected = false,
|
||||||
|
Step = 2
|
||||||
|
},
|
||||||
|
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
|
||||||
|
Name = "example.com",
|
||||||
|
NameServers =
|
||||||
|
[
|
||||||
|
"bob.ns.cloudflare.com",
|
||||||
|
"lola.ns.cloudflare.com"
|
||||||
|
],
|
||||||
|
OriginalDnshost = "NameCheap",
|
||||||
|
OriginalNameServers =
|
||||||
|
[
|
||||||
|
"ns1.originaldnshost.com",
|
||||||
|
"ns2.originaldnshost.com"
|
||||||
|
],
|
||||||
|
OriginalRegistrar = "GoDaddy",
|
||||||
|
Owner = new OwnerBase
|
||||||
|
{
|
||||||
|
Id = "023e105f4ecef8ad9ca31a8372d0c353",
|
||||||
|
Name = "Example Org",
|
||||||
|
Type = "organization"
|
||||||
|
},
|
||||||
|
Paused = true,
|
||||||
|
Status = ZoneStatus.Initializing,
|
||||||
|
Type = ZoneType.Full,
|
||||||
|
VanityNameServers =
|
||||||
|
[
|
||||||
|
"ns1.example.com",
|
||||||
|
"ns2.example.com"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldReturnZoneDetails()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.ZoneDetails(ZoneId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.AreEqual(_response.Result, response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"zones/{ZoneId}", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<Zone>($"zones/{ZoneId}", null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumentNullExceptionWhenZoneIdIsNull(string id)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.ZoneDetails(id);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public async Task ShouldThrowArgumentOutOfRangeExceptionWhenZoneIdTooLong()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string id = new('a', 33);
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await client.ZoneDetails(id);
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.GetAsync<Zone>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user