diff --git a/src/Analyzers.ruleset b/src/Analyzers.ruleset index 77cea96..2e4acdc 100644 --- a/src/Analyzers.ruleset +++ b/src/Analyzers.ruleset @@ -10,5 +10,6 @@ + \ No newline at end of file diff --git a/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.csproj b/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.csproj index ee981f3..090d9f6 100644 --- a/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.csproj +++ b/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.csproj @@ -57,8 +57,8 @@ ..\packages\Microsoft.Net.Http.2.2.22\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Primitives.dll True - - ..\packages\YamlDotNet.3.6.1\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\YamlDotNet.dll + + ..\packages\YamlDotNet.3.7.0\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\YamlDotNet.dll True @@ -94,7 +94,7 @@ - + diff --git a/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.nuspec b/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.nuspec index a193758..1012812 100644 --- a/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.nuspec +++ b/src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.nuspec @@ -2,7 +2,7 @@ $id$ - 0.0.4 + 0.0.5 $title$ James Skimming James Skimming @@ -12,7 +12,7 @@ $description$ CloudFlare YAML API Client - + \ No newline at end of file diff --git a/src/CloudFlare.NET.Yaml/Properties/AssemblyInfo.cs b/src/CloudFlare.NET.Yaml/Properties/AssemblyInfo.cs index 47e0fea..11bd297 100644 --- a/src/CloudFlare.NET.Yaml/Properties/AssemblyInfo.cs +++ b/src/CloudFlare.NET.Yaml/Properties/AssemblyInfo.cs @@ -24,5 +24,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.0.4.0")] -[assembly: AssemblyFileVersion("0.0.4.0")] +[assembly: AssemblyVersion("0.0.5.0")] +[assembly: AssemblyFileVersion("0.0.5.0")] diff --git a/src/CloudFlare.NET.Yaml/packages.config b/src/CloudFlare.NET.Yaml/packages.config index 802bcae..a4a1f36 100644 --- a/src/CloudFlare.NET.Yaml/packages.config +++ b/src/CloudFlare.NET.Yaml/packages.config @@ -1,11 +1,11 @@  - + - - + + \ No newline at end of file diff --git a/src/CloudFlare.NET/CloudFlare.NET.csproj b/src/CloudFlare.NET/CloudFlare.NET.csproj index ba8dfaf..5649e6f 100644 --- a/src/CloudFlare.NET/CloudFlare.NET.csproj +++ b/src/CloudFlare.NET/CloudFlare.NET.csproj @@ -84,17 +84,24 @@ + + + + + + + - + diff --git a/src/CloudFlare.NET/CloudFlare.NET.nuspec b/src/CloudFlare.NET/CloudFlare.NET.nuspec index f6d9df4..0dbc057 100644 --- a/src/CloudFlare.NET/CloudFlare.NET.nuspec +++ b/src/CloudFlare.NET/CloudFlare.NET.nuspec @@ -2,7 +2,7 @@ $id$ - 0.0.4 + 0.0.5 $title$ James Skimming James Skimming @@ -11,5 +11,9 @@ false $description$ CloudFlare API Client + + + + \ No newline at end of file diff --git a/src/CloudFlare.NET/CloudFlareClient.cs b/src/CloudFlare.NET/CloudFlareClient.cs index fcd0c24..63ecebc 100644 --- a/src/CloudFlare.NET/CloudFlareClient.cs +++ b/src/CloudFlare.NET/CloudFlareClient.cs @@ -12,14 +12,14 @@ /// public class CloudFlareClient : ICloudFlareClient { - private readonly HttpClient _client; - - private readonly CloudFlareAuth _auth; - private static readonly Lazy LazyClient = new Lazy( () => CreateDefaultHttpClient(), LazyThreadSafetyMode.PublicationOnly); + private readonly HttpClient _client; + + private readonly CloudFlareAuth _auth; + /// /// Initializes a new instance of the class. /// @@ -127,9 +127,12 @@ public static HttpClientHandler CreateDefaultHttpClientHandler() } /// - public Task> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null) + public Task> GetZonesAsync( + CancellationToken cancellationToken, + PagedZoneParameters parameters = null, + CloudFlareAuth auth = null) { - return _client.GetZonesAsync(cancellationToken, auth ?? _auth); + return _client.GetZonesAsync(cancellationToken, auth ?? _auth, parameters); } /// diff --git a/src/CloudFlare.NET/DnsRecordType.cs b/src/CloudFlare.NET/DnsRecordType.cs index e9c8959..041e520 100644 --- a/src/CloudFlare.NET/DnsRecordType.cs +++ b/src/CloudFlare.NET/DnsRecordType.cs @@ -11,7 +11,7 @@ /// The type of a . /// [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", - Justification = "Names a self-explanatory.")] + Justification = "Names are self-explanatory.")] [JsonConverter(typeof(StringEnumConverter))] public enum DnsRecordType { diff --git a/src/CloudFlare.NET/HttpClientZoneExtensions.cs b/src/CloudFlare.NET/HttpClientZoneExtensions.cs index fee7537..f001d29 100644 --- a/src/CloudFlare.NET/HttpClientZoneExtensions.cs +++ b/src/CloudFlare.NET/HttpClientZoneExtensions.cs @@ -20,7 +20,8 @@ public static class HttpClientZoneExtensions public static async Task> GetZonesAsync( this HttpClient client, CancellationToken cancellationToken, - CloudFlareAuth auth) + CloudFlareAuth auth, + PagedZoneParameters parameters = null) { if (client == null) throw new ArgumentNullException(nameof(client)); @@ -28,6 +29,11 @@ public static async Task> GetZonesAsync( throw new ArgumentNullException(nameof(auth)); Uri uri = new Uri(CloudFlareConstants.BaseUri, "zones"); + if (parameters != null) + { + uri = new UriBuilder(uri) { Query = parameters.ToQuery() }.Uri; + } + var request = new HttpRequestMessage(HttpMethod.Get, uri); request.AddAuth(auth); diff --git a/src/CloudFlare.NET/IZoneClient.cs b/src/CloudFlare.NET/IZoneClient.cs index b037da2..7314548 100644 --- a/src/CloudFlare.NET/IZoneClient.cs +++ b/src/CloudFlare.NET/IZoneClient.cs @@ -16,7 +16,10 @@ public interface IZoneClient /// Gets the zones for the subscription. /// /// - Task> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null); + Task> GetZonesAsync( + CancellationToken cancellationToken, + PagedZoneParameters parameters = null, + CloudFlareAuth auth = null); /// /// Gets the zone with the specified . diff --git a/src/CloudFlare.NET/PagedParameters.cs b/src/CloudFlare.NET/PagedParameters.cs new file mode 100644 index 0000000..9fd6846 --- /dev/null +++ b/src/CloudFlare.NET/PagedParameters.cs @@ -0,0 +1,62 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using Newtonsoft.Json; + + /// + /// Specifies the parameters of a request supports paging. + /// + /// The type of the property. + public abstract class PagedParameters + where TOrder : struct + { + /// + /// Initializes a new instance of the class. + /// + protected PagedParameters( + int page, + int perPage, + TOrder order, + PagedParametersOrderType direction, + PagedParametersMatchType match) + { + Page = page; + PerPage = perPage; + Order = order; + Direction = direction; + Match = match; + } + + /// + /// Gets the page number of paginated results. + /// + [JsonProperty("page")] + public int Page { get; } + + /// + /// Gets the number of results per page. + /// + [JsonProperty("per_page")] + public int PerPage { get; } + + /// + /// Field to order results by. + /// + [JsonProperty("order")] + public TOrder Order { get; } + + /// + /// Gets the direction to order. + /// + [JsonProperty("direction")] + public PagedParametersOrderType Direction { get; } + + /// + /// Gets the value of whether to match all search requirements or at least one (any). + /// + [JsonProperty("match")] + public PagedParametersMatchType Match { get; } + } +} diff --git a/src/CloudFlare.NET/PagedParametersExtensions.cs b/src/CloudFlare.NET/PagedParametersExtensions.cs new file mode 100644 index 0000000..a63f790 --- /dev/null +++ b/src/CloudFlare.NET/PagedParametersExtensions.cs @@ -0,0 +1,62 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using Newtonsoft.Json; + using Newtonsoft.Json.Linq; + + /// + /// Extension methinks on paged parameter objects. + /// + public static class PagedParametersExtensions + { + /// + /// Converts the to their equivalent key value pair representation. + /// NOTE. Default values are excluded + /// + /// The type of the property. + public static IEnumerable> ToKvp(this PagedParameters parameters) + where TOrder : struct + { + if (parameters == null) + throw new ArgumentNullException(nameof(parameters)); + + // Remove default values to ensure they're not passed as parameters. + JsonSerializer serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings + { + DefaultValueHandling = DefaultValueHandling.Ignore, + }); + + JObject json = JObject.FromObject(parameters, serializer); + + return + ((IDictionary)json).Select( + kvp => new KeyValuePair(kvp.Key, kvp.Value.Value())); + } + + /// + /// Converts the to their equivalent query string representation. + /// NOTE. Default values are excluded + /// + /// The type of the property. + public static string ToQuery(this PagedParameters parameters) + where TOrder : struct + { + IEnumerable> keyValuePairs = parameters.ToKvp(); + + StringBuilder stringBuilder = new StringBuilder(); + foreach (KeyValuePair keyValuePair in keyValuePairs) + { + if (stringBuilder.Length > 0) + stringBuilder.Append('&'); + stringBuilder.Append(Uri.EscapeDataString(keyValuePair.Key)); + stringBuilder.Append('='); + stringBuilder.Append(Uri.EscapeDataString(keyValuePair.Value)); + } + + return stringBuilder.ToString(); + } + } +} diff --git a/src/CloudFlare.NET/PagedParametersMatchType.cs b/src/CloudFlare.NET/PagedParametersMatchType.cs new file mode 100644 index 0000000..d285144 --- /dev/null +++ b/src/CloudFlare.NET/PagedParametersMatchType.cs @@ -0,0 +1,23 @@ +namespace CloudFlare.NET +{ + using System.Diagnostics.CodeAnalysis; + using Newtonsoft.Json; + using Newtonsoft.Json.Converters; + +// ReSharper disable InconsistentNaming +#pragma warning disable 1591 + + /// + /// The types by which a paged results are matched. + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", + Justification = "Names are self-explanatory.")] + [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter", + Justification = "Named to match serialized values.")] + [JsonConverter(typeof(StringEnumConverter))] + public enum PagedParametersMatchType + { + all, + any, + } +} diff --git a/src/CloudFlare.NET/PagedParametersOrderType.cs b/src/CloudFlare.NET/PagedParametersOrderType.cs new file mode 100644 index 0000000..b19e624 --- /dev/null +++ b/src/CloudFlare.NET/PagedParametersOrderType.cs @@ -0,0 +1,23 @@ +namespace CloudFlare.NET +{ + using System.Diagnostics.CodeAnalysis; + using Newtonsoft.Json; + using Newtonsoft.Json.Converters; + +// ReSharper disable InconsistentNaming +#pragma warning disable 1591 + + /// + /// The types by which a paged results an be ordered. + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", + Justification = "Names are self-explanatory.")] + [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter", + Justification = "Named to match serialized values.")] + [JsonConverter(typeof(StringEnumConverter))] + public enum PagedParametersOrderType + { + asc, + desc, + } +} diff --git a/src/CloudFlare.NET/PagedZoneOrderFieldTypes.cs b/src/CloudFlare.NET/PagedZoneOrderFieldTypes.cs new file mode 100644 index 0000000..8ae86e2 --- /dev/null +++ b/src/CloudFlare.NET/PagedZoneOrderFieldTypes.cs @@ -0,0 +1,25 @@ +namespace CloudFlare.NET +{ + using System.Diagnostics.CodeAnalysis; + using Newtonsoft.Json; + using Newtonsoft.Json.Converters; + + // ReSharper disable InconsistentNaming +#pragma warning disable 1591 + + /// + /// The fields by which zones can be ordered. + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", + Justification = "Names are self-explanatory.")] + [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter", + Justification = "Named to match serialized values.")] + [JsonConverter(typeof(StringEnumConverter))] + public enum PagedZoneOrderFieldTypes + { + none, + name, + status, + email, + } +} diff --git a/src/CloudFlare.NET/PagedZoneParameters.cs b/src/CloudFlare.NET/PagedZoneParameters.cs new file mode 100644 index 0000000..b093a94 --- /dev/null +++ b/src/CloudFlare.NET/PagedZoneParameters.cs @@ -0,0 +1,56 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using Newtonsoft.Json; + using Newtonsoft.Json.Linq; + + /// + /// Specifies the parameters of the get zone request. + /// + /// + public class PagedZoneParameters : PagedParameters + { + /// + /// Initializes a new instance of the class. + /// + public PagedZoneParameters( + string name = null, + ZoneStatusType? status = null, + int page = 0, + int perPage = 0, + PagedZoneOrderFieldTypes order = default(PagedZoneOrderFieldTypes), + PagedParametersOrderType direction = default(PagedParametersOrderType), + PagedParametersMatchType match = default(PagedParametersMatchType)) + : base(page, perPage, order, direction, match) + { + Name = name; + Status = status; + } + + /// + /// A domain name. + /// + [JsonProperty("name")] + public string Name { get; } + + /// + /// Status of the zone + /// + [JsonProperty("status")] + public ZoneStatusType? Status { get; } + + /// + /// Creates a from the copying any matching + /// properties. + /// + public static PagedZoneParameters Create(object data) + { + if (data == null) + throw new ArgumentNullException(nameof(data)); + + return JObject.FromObject(data).ToObject(); + } + } +} diff --git a/src/CloudFlare.NET/Properties/AssemblyInfo.cs b/src/CloudFlare.NET/Properties/AssemblyInfo.cs index 0e06e67..2e3a100 100644 --- a/src/CloudFlare.NET/Properties/AssemblyInfo.cs +++ b/src/CloudFlare.NET/Properties/AssemblyInfo.cs @@ -24,5 +24,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.0.4.0")] -[assembly: AssemblyFileVersion("0.0.4.0")] +[assembly: AssemblyVersion("0.0.5.0")] +[assembly: AssemblyFileVersion("0.0.5.0")] diff --git a/src/CloudFlare.NET/Zone.cs b/src/CloudFlare.NET/Zone.cs index 9cc31e1..ac90c14 100644 --- a/src/CloudFlare.NET/Zone.cs +++ b/src/CloudFlare.NET/Zone.cs @@ -26,7 +26,8 @@ public Zone( IReadOnlyList originalNameServers = null, string originalRegistrar = null, string originalDnshost = null, - IReadOnlyList nameServers = null) + IReadOnlyList nameServers = null, + ZoneStatusType status = ZoneStatusType.active) { if (id == null) throw new ArgumentNullException(nameof(id)); @@ -42,6 +43,7 @@ public Zone( OriginalRegistrar = originalRegistrar ?? string.Empty; OriginalDnshost = originalDnshost ?? string.Empty; NameServers = nameServers ?? EmptyStrings; + Status = status; } /// @@ -95,5 +97,11 @@ public Zone( /// [JsonProperty("name_servers")] public IReadOnlyList NameServers { get; } + + /// + /// Status of the zone. + /// + [JsonProperty("status")] + public ZoneStatusType Status { get; } } } diff --git a/src/CloudFlare.NET/ZoneClientExtensions.cs b/src/CloudFlare.NET/ZoneClientExtensions.cs index 1a5670e..09f7acc 100644 --- a/src/CloudFlare.NET/ZoneClientExtensions.cs +++ b/src/CloudFlare.NET/ZoneClientExtensions.cs @@ -15,12 +15,31 @@ public static class ZoneClientExtensions /// Gets the zones for the subscription. /// /// - public static Task> GetZonesAsync(this IZoneClient client, CloudFlareAuth auth = null) + public static Task> GetZonesAsync( + this IZoneClient client, + PagedZoneParameters parameters = null) + { + if (client == null) + throw new ArgumentNullException(nameof(client)); + + return client.GetZonesAsync(CancellationToken.None, parameters); + } + + /// + /// Gets the zones for the subscription. + /// + /// + public static Task> GetZonesAsync( + this IZoneClient client, + CloudFlareAuth auth, + PagedZoneParameters parameters = null) { if (client == null) throw new ArgumentNullException(nameof(client)); + if (auth == null) + throw new ArgumentNullException(nameof(auth)); - return client.GetZonesAsync(CancellationToken.None, auth); + return client.GetZonesAsync(CancellationToken.None, parameters, auth); } /// diff --git a/src/CloudFlare.NET/ZoneStatusType.cs b/src/CloudFlare.NET/ZoneStatusType.cs new file mode 100644 index 0000000..661104f --- /dev/null +++ b/src/CloudFlare.NET/ZoneStatusType.cs @@ -0,0 +1,27 @@ +namespace CloudFlare.NET +{ + using System.Diagnostics.CodeAnalysis; + using Newtonsoft.Json; + using Newtonsoft.Json.Converters; + +// ReSharper disable InconsistentNaming +#pragma warning disable 1591 + + /// + /// The types of status of a zone. + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", + Justification = "Names are self-explanatory.")] + [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter", + Justification = "Named to match serialized values.")] + [JsonConverter(typeof(StringEnumConverter))] + public enum ZoneStatusType + { + active, + pending, + initializing, + moved, + deleted, + deactivated, + } +} diff --git a/src/CloudFlare.NET/packages.config b/src/CloudFlare.NET/packages.config index 0e52d8d..7be0145 100644 --- a/src/CloudFlare.NET/packages.config +++ b/src/CloudFlare.NET/packages.config @@ -5,5 +5,5 @@ - + \ No newline at end of file diff --git a/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj b/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj index dc524f1..e9a97d4 100644 --- a/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj +++ b/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj @@ -61,8 +61,8 @@ ..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll True - - ..\..\packages\AutoFixture.3.31.1\lib\net40\Ploeh.AutoFixture.dll + + ..\..\packages\AutoFixture.3.31.3\lib\net40\Ploeh.AutoFixture.dll True @@ -84,6 +84,10 @@ ..\..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Extensions.dll True + + ..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + True + ..\..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Primitives.dll True @@ -115,6 +119,7 @@ + diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/BehaviorTemplates.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/BehaviorTemplates.cs index 67bdac2..57c51e2 100644 --- a/src/Tests/CloudFlare.NET.Tests/Serialization/BehaviorTemplates.cs +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/BehaviorTemplates.cs @@ -53,4 +53,41 @@ public class ModifiedSerializeBehavior It should_serialize_modified_on_for_ISO8601 = () => _json["modified_on"].ToString().ShouldEndWith("Z"); } + + [Behaviors] + public class PagedParametersSerializeBehavior + where TOrder : struct + { + protected static JObject _json; + protected static PagedParameters _sut; + + It should_serialize_id = () => _sut.Page.ShouldEqual(_json["page"].Value()); + + It should_serialize_per_page = () => _sut.PerPage.ShouldEqual(_json["per_page"].Value()); + + It should_serialize_order = () => _sut.Order.ToString().ShouldEqual(_json["order"].Value()); + + It should_serialize_direction = + () => _sut.Direction.ToString().ShouldEqual(_json["direction"].Value()); + + It should_serialize_match = () => _sut.Match.ToString().ShouldEqual(_json["match"].Value()); + } + + [Behaviors] + public class PagedParametersKvpBehavior + where TOrder : struct + { + protected static PagedParameters _parameters; + protected static IReadOnlyDictionary _result; + + It should_have_name_page = () => _result["page"].ShouldEqual(_parameters.Page.ToString()); + + It should_have_name_per_page = () => _result["per_page"].ShouldEqual(_parameters.PerPage.ToString()); + + It should_have_name_order = () => _result["order"].ShouldEqual(_parameters.Order.ToString()); + + It should_have_name_direction = () => _result["direction"].ShouldEqual(_parameters.Direction.ToString()); + + It should_have_name_match = () => _result["match"].ShouldEqual(_parameters.Match.ToString()); + } } diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/PagedZoneParametersSerializationSpec.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/PagedZoneParametersSerializationSpec.cs new file mode 100644 index 0000000..4c8ff9f --- /dev/null +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/PagedZoneParametersSerializationSpec.cs @@ -0,0 +1,147 @@ +namespace CloudFlare.NET.Serialization.PagedZoneParametersSpec +{ + using System; + using System.Collections.Generic; + using System.Collections.Specialized; + using System.Linq; + using System.Net.Http; + using Machine.Specifications; + using Newtonsoft.Json; + using Newtonsoft.Json.Linq; + using Ploeh.AutoFixture; + + [Subject(typeof(PagedZoneParameters))] + public class When_serializing : FixtureContext + { + protected static PagedZoneParameters _sut; + protected static JObject _json; + + Establish context = () => _sut = _fixture.Create(); + + Because of = () => _json = JObject.FromObject(_sut); + + Behaves_like> paged_parameters_serialize_behavior; + + It should_serialize_name = () => _sut.Name.ShouldEqual(_json["name"].Value()); + + It should_serialize_status = () => _sut.Status.ToString().ShouldEqual(_json["status"].Value()); + } + + [Subject(typeof(PagedZoneParameters))] + public class When_serializing_and_deserializing : FixtureContext + { + static PagedZoneParameters _expected; + static PagedZoneParameters _actual; + + Establish context = () => _expected = _fixture.Create(); + + Because of = () => + { + var serializeObject = JsonConvert.SerializeObject(_expected); + _actual = JObject.FromObject(_expected).ToObject(); + }; + + It should_retain_all_properties = () => _actual.AsLikeness().ShouldEqual(_expected); + } + + [Subject(typeof(PagedZoneParameters))] + public class When_creating_with_a_subset_of_properties : FixtureContext + { + static PagedZoneParameters _expected; + static object _source; + static PagedZoneParameters _actual; + + Establish context = () => + { + _expected = _fixture.Create(); + _source = new + { + _expected.Name, + _expected.Match, + _expected.PerPage, + }; + }; + + Because of = () => _actual = PagedZoneParameters.Create(_source); + + It should_retain_all_properties = () => + _actual.AsLikeness() + .OmitAutoComparison() + .WithDefaultEquality(e => e.Name) + .WithDefaultEquality(e => e.Match) + .WithDefaultEquality(e => e.PerPage) + .ShouldEqual(_expected); + } + + [Subject(typeof (PagedZoneParameters))] + public class When_converting_to_key_value_pair_with_all_default_values + { + static PagedZoneParameters _parameters; + static IEnumerable> _result; + + Establish context = () => _parameters = new PagedZoneParameters(); + + Because of = () => _result = _parameters.ToKvp(); + + It should_have_no_values = () => _result.ShouldBeEmpty(); + } + + [Subject(typeof(PagedZoneParameters))] + public class When_converting_to_key_value_pair_with_no_default_values : FixtureContext + { + protected static PagedZoneParameters _parameters; + protected static Dictionary _result; + + Establish context = () => + { + // Auto fixture chooses the default value for enumerations. + _fixture.Inject(PagedZoneOrderFieldTypes.email); + _fixture.Inject(PagedParametersOrderType.desc); + _fixture.Inject(PagedParametersMatchType.any); + + _parameters = _fixture.Create(); + }; + + Because of = () => _result = _parameters.ToKvp().ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + + Behaves_like> paged_parameters_kvp_behavior; + + It should_have_name_value = () => _result["name"].ShouldEqual(_parameters.Name); + + It should_have_name_status = () => _result["status"].ShouldEqual(_parameters.Status.ToString()); + } + + [Subject(typeof(PagedZoneParameters))] + public class When_converting_to_query_string : FixtureContext + { + protected static PagedZoneParameters _parameters; + protected static Dictionary _result; + + Establish context = () => + { + // Auto fixture chooses the default value for enumerations. + _fixture.Inject(PagedZoneOrderFieldTypes.email); + _fixture.Inject(PagedParametersOrderType.desc); + _fixture.Inject(PagedParametersMatchType.any); + + _parameters = _fixture.Create(); + }; + + Because of = () => + { + string query = _parameters.ToQuery(); + // Convert + var builder = new UriBuilder("http://localhost/path") { Query = query }; + + // Get the values back as a dictionary to validate their values. + NameValueCollection kvp = builder.Uri.ParseQueryString(); + _result = kvp.Cast().ToDictionary(k => k, k => kvp[k]); + }; + + Behaves_like> paged_parameters_kvp_behavior; + + It should_have_name_value = () => _result["name"].ShouldEqual(_parameters.Name); + + It should_have_name_status = () => _result["status"].ShouldEqual(_parameters.Status.ToString()); + } +} diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs index e56c092..52d4148 100644 --- a/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs @@ -41,6 +41,8 @@ public class When_deserializing It should_deserialize_name_servers = () => _sut.NameServers.ShouldContainOnly(_json["name_servers"].ToObject>()); + + It should_deserialize_status = () => _sut.Status.ToString().ShouldEqual(_json["status"].Value()); } [Subject(typeof(Zone))] @@ -91,6 +93,8 @@ public class When_serializing : FixtureContext It should_serialize_name_servers = () => _sut.NameServers.ShouldContainOnly(_json["name_servers"].ToObject>()); + + It should_serialize_status = () => _sut.Status.ToString().ShouldEqual(_json["status"].Value()); } [Subject(typeof(Zone))] diff --git a/src/Tests/CloudFlare.NET.Tests/ZoneClientExtensionsSpec.cs b/src/Tests/CloudFlare.NET.Tests/ZoneClientExtensionsSpec.cs index 00262b5..458d36a 100644 --- a/src/Tests/CloudFlare.NET.Tests/ZoneClientExtensionsSpec.cs +++ b/src/Tests/CloudFlare.NET.Tests/ZoneClientExtensionsSpec.cs @@ -10,7 +10,7 @@ using It = Machine.Specifications.It; [Subject(typeof(ZoneClientExtensions))] - public class When_getting_zones : FixtureContext + public class When_getting_all_zones : FixtureContext { static Mock _zoneClientMock; static CloudFlareAuth _auth; @@ -23,7 +23,7 @@ public class When_getting_zones : FixtureContext _auth = _fixture.Create(); _expected = _fixture.Create(); _zoneClientMock - .Setup(c => c.GetZonesAsync(CancellationToken.None, _auth)) + .Setup(c => c.GetZonesAsync(CancellationToken.None, default(PagedZoneParameters), _auth)) .ReturnsAsync(_expected); }; @@ -32,6 +32,29 @@ public class When_getting_zones : FixtureContext It should_return_the_zones = () => _actual.ShouldBeTheSameAs(_expected); } + [Subject(typeof(ZoneClientExtensions))] + public class When_getting_all_zones_with_parameters : FixtureContext + { + static Mock _zoneClientMock; + static PagedZoneParameters _parameters; + static IReadOnlyList _expected; + static IReadOnlyList _actual; + + Establish context = () => + { + _zoneClientMock = _fixture.Create>(); + _parameters = _fixture.Create(); + _expected = _fixture.Create(); + _zoneClientMock + .Setup(c => c.GetZonesAsync(CancellationToken.None, _parameters, default(CloudFlareAuth))) + .ReturnsAsync(_expected); + }; + + Because of = () => _actual = _zoneClientMock.Object.GetZonesAsync(_parameters).Await().AsTask.Result; + + It should_return_the_zones = () => _actual.ShouldBeTheSameAs(_expected); + } + [Subject(typeof(ZoneClientExtensions))] public class When_getting_a_zone : FixtureContext { diff --git a/src/Tests/CloudFlare.NET.Tests/ZoneClientSpec.cs b/src/Tests/CloudFlare.NET.Tests/ZoneClientSpec.cs index 2a77cc4..8baee57 100644 --- a/src/Tests/CloudFlare.NET.Tests/ZoneClientSpec.cs +++ b/src/Tests/CloudFlare.NET.Tests/ZoneClientSpec.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Linq; - using System.Net; using System.Net.Http; using Machine.Specifications; using Ploeh.AutoFixture; @@ -35,6 +34,42 @@ public class When_getting_all_zones : RequestContext _actual.Select(z => z.AsLikeness().CreateProxy()).SequenceEqual(_expected).ShouldBeTrue(); } + [Subject(typeof(CloudFlareClient))] + public class When_getting_all_zones_with_parameters : RequestContext + { + static IReadOnlyList _expected; + static IReadOnlyList _actual; + static PagedZoneParameters _parameters; + static Uri _expectedRequestUri; + + Establish context = () => + { + var response = _fixture.Create>>(); + _expected = response.Result; + _handler.SetResponseContent(response); + + // Auto fixture chooses the default value for enumerations. + _fixture.Inject(PagedZoneOrderFieldTypes.email); + _fixture.Inject(PagedParametersOrderType.desc); + _fixture.Inject(PagedParametersMatchType.any); + + _parameters = _fixture.Create(); + + _expectedRequestUri = new Uri(CloudFlareConstants.BaseUri, "zones?" + _parameters.ToQuery()); + }; + + Because of = () => _actual = _sut.GetZonesAsync(_auth, _parameters).Await().AsTask.Result; + + Behaves_like authenticated_request_behaviour; + + It should_make_a_GET_request = () => _handler.Request.Method.ShouldEqual(HttpMethod.Get); + + It should_request_the_zones_endpoint = () => _handler.Request.RequestUri.ShouldEqual(_expectedRequestUri); + + It should_return_the_expected_zones = () => + _actual.Select(z => z.AsLikeness().CreateProxy()).SequenceEqual(_expected).ShouldBeTrue(); + } + [Subject(typeof(CloudFlareClient))] public class When_getting_all_zones_and_an_error_occurs : ErredRequestContext { diff --git a/src/Tests/CloudFlare.NET.Tests/app.config b/src/Tests/CloudFlare.NET.Tests/app.config index 9725825..99f15bb 100644 --- a/src/Tests/CloudFlare.NET.Tests/app.config +++ b/src/Tests/CloudFlare.NET.Tests/app.config @@ -12,7 +12,11 @@ - + + + + + diff --git a/src/Tests/CloudFlare.NET.Tests/packages.config b/src/Tests/CloudFlare.NET.Tests/packages.config index 593ef36..896e14f 100644 --- a/src/Tests/CloudFlare.NET.Tests/packages.config +++ b/src/Tests/CloudFlare.NET.Tests/packages.config @@ -1,6 +1,6 @@  - + @@ -9,13 +9,14 @@ + - + diff --git a/src/Tests/CloudFlare.NET.Yaml.Tests/CloudFlare.NET.Yaml.Tests.csproj b/src/Tests/CloudFlare.NET.Yaml.Tests/CloudFlare.NET.Yaml.Tests.csproj index 6adc44c..8e3c738 100644 --- a/src/Tests/CloudFlare.NET.Yaml.Tests/CloudFlare.NET.Yaml.Tests.csproj +++ b/src/Tests/CloudFlare.NET.Yaml.Tests/CloudFlare.NET.Yaml.Tests.csproj @@ -61,8 +61,8 @@ ..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll True - - ..\..\packages\AutoFixture.3.31.1\lib\net40\Ploeh.AutoFixture.dll + + ..\..\packages\AutoFixture.3.31.3\lib\net40\Ploeh.AutoFixture.dll True diff --git a/src/Tests/CloudFlare.NET.Yaml.Tests/app.config b/src/Tests/CloudFlare.NET.Yaml.Tests/app.config index 9725825..3516892 100644 --- a/src/Tests/CloudFlare.NET.Yaml.Tests/app.config +++ b/src/Tests/CloudFlare.NET.Yaml.Tests/app.config @@ -12,7 +12,7 @@ - + diff --git a/src/Tests/CloudFlare.NET.Yaml.Tests/packages.config b/src/Tests/CloudFlare.NET.Yaml.Tests/packages.config index 1194439..4041af2 100644 --- a/src/Tests/CloudFlare.NET.Yaml.Tests/packages.config +++ b/src/Tests/CloudFlare.NET.Yaml.Tests/packages.config @@ -1,6 +1,6 @@  - +