diff --git a/appveyor.yml b/appveyor.yml index 84540af..779fc1a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -72,11 +72,18 @@ after_build: test_script: coverage.cmd after_test: | - 7z a CoverageResults.7z src\TestResults\ - appveyor PushArtifact CoverageResults.7z -FileName "CoverageResults_%GitVersion_FullSemVer%.7z" - appveyor PushArtifact src\TestResults\Specifications.html -FileName "Specifications_%GitVersion_FullSemVer%.html" FOR /r %%F IN (*coveralls.net.exe) DO SET coveralls_exe=%%F %coveralls_exe% --opencover src\TestResults\Test.Coverage.xml + appveyor PushArtifact src\TestResults\Specifications.html -FileName "Specifications_%GitVersion_FullSemVer%.html" + +#---------------------------------# +# global handlers # +#---------------------------------# + +# after build failure or success +on_finish: | + 7z a CoverageResults.7z src\TestResults\ + appveyor PushArtifact CoverageResults.7z -FileName "CoverageResults_%GitVersion_FullSemVer%.7z" #---------------------------------# # notifications # @@ -89,3 +96,5 @@ notifications: auth_token: secure: AkSNuuHmfzAFp2+Oq00NRQFOfrXl4ue08SHx0FC2WTeXSiZv8uVKI3ZBWns7HTcV channel: cloudflare_net + on_build_status_changed: true + diff --git a/coverage.cmd b/coverage.cmd index 2526d7e..db0d33c 100644 --- a/coverage.cmd +++ b/coverage.cmd @@ -44,11 +44,13 @@ ::@echo "%xunit_exe%" "%test_assemblies%" -noshadow -html "%xunit_results%" ::@"%xunit_exe%" "%test_assemblies%" -noshadow -html "%xunit_results%" -@echo "%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%" -@"%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%" +@echo "%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -returntargetcode -filter:%coverage_filter% "-output:%coverage_results%" +@"%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -returntargetcode -filter:%coverage_filter% "-output:%coverage_results%" +@IF ERRORLEVEL 1 GOTO :EOF -@echo "%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%" -@"%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%" +@echo "%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -returntargetcode -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%" +@"%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -returntargetcode -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%" +@IF ERRORLEVEL 1 GOTO :EOF @echo "%report_exe%" -verbosity:Error "-reports:%coverage_results%" "-targetdir:%~dp0src\TestResults" -reporttypes:XmlSummary @"%report_exe%" -verbosity:Error "-reports:%coverage_results%" "-targetdir:%~dp0src\TestResults" -reporttypes:XmlSummary diff --git a/src/CloudFlare.NET/CloudFlare.NET.csproj b/src/CloudFlare.NET/CloudFlare.NET.csproj index db73c96..f8cb314 100644 --- a/src/CloudFlare.NET/CloudFlare.NET.csproj +++ b/src/CloudFlare.NET/CloudFlare.NET.csproj @@ -70,10 +70,16 @@ + + + + + + diff --git a/src/CloudFlare.NET/CloudFlare.NET.nuspec b/src/CloudFlare.NET/CloudFlare.NET.nuspec index 60907e9..5f14a5f 100644 --- a/src/CloudFlare.NET/CloudFlare.NET.nuspec +++ b/src/CloudFlare.NET/CloudFlare.NET.nuspec @@ -2,7 +2,7 @@ $id$ - 0.0.1 + 0.0.2 $title$ James Skimming James Skimming diff --git a/src/CloudFlare.NET/CloudFlareClient.cs b/src/CloudFlare.NET/CloudFlareClient.cs index 80a059a..ec5c22e 100644 --- a/src/CloudFlare.NET/CloudFlareClient.cs +++ b/src/CloudFlare.NET/CloudFlareClient.cs @@ -33,7 +33,16 @@ public class CloudFlareClient : ICloudFlareClient /// public Task> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null) { - return Client.GetZonesAsync(auth, cancellationToken); + return Client.GetZonesAsync(cancellationToken, auth); + } + + /// + public Task> GetDnsRecordsAsync( + IdentifierTag zoneId, + CancellationToken cancellationToken, + CloudFlareAuth auth = null) + { + return Client.GetDnsRecordsAsync(zoneId, cancellationToken, auth); } } } diff --git a/src/CloudFlare.NET/DnsRecord.cs b/src/CloudFlare.NET/DnsRecord.cs new file mode 100644 index 0000000..f27611e --- /dev/null +++ b/src/CloudFlare.NET/DnsRecord.cs @@ -0,0 +1,149 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using Newtonsoft.Json; + using Newtonsoft.Json.Linq; + + /// + /// Represents a DNS record for a . + /// + /// + public class DnsRecord + { + /// + /// Initializes a new instance of the class. + /// + public DnsRecord( + IdentifierTag id, + DnsRecordType type, + string name, + bool proxiable, + bool proxied, + int ttl, + bool locked, + IdentifierTag zoneId, + DateTimeOffset createdOn, + DateTimeOffset modifiedOn, + int priority, + string content = null, + string zoneName = null, + JObject data = null, + JObject meta = null) + { + if (id == null) + throw new ArgumentNullException(nameof(id)); + if (name == null) + throw new ArgumentNullException(nameof(name)); + if (zoneId == null) + throw new ArgumentNullException(nameof(zoneId)); + + Id = id; + Type = type; + Name = name; + Content = content ?? string.Empty; + Proxiable = proxiable; + Proxied = proxied; + Ttl = ttl; + Locked = locked; + ZoneId = zoneId; + ZoneName = zoneName ?? string.Empty; + CreatedOn = createdOn; + ModifiedOn = modifiedOn; + Data = data ?? new JObject(); + Meta = meta ?? new JObject(); + Priority = priority; + } + + /// + /// API item identifier tag. + /// + [JsonProperty("id")] + public IdentifierTag Id { get; } + + /// + /// Record type. + /// + [JsonProperty("type")] + public DnsRecordType Type { get; } + + /// + /// A valid host name. + /// + [JsonProperty("name")] + public string Name { get; } + + /// + /// Valid content for the given . + /// + [JsonProperty("content")] + public string Content { get; } + + /// + /// Whether the record can be proxied by CloudFlare or not. + /// + [JsonProperty("proxiable")] + public bool Proxiable { get; } + + /// + /// Whether the record is receiving the performance and security benefits of CloudFlare + /// + [JsonProperty("proxied")] + public bool Proxied { get; } + + /// + /// Time to live for DNS record. Value of 1 is 'automatic'. + /// + [JsonProperty("ttl")] + public int Ttl { get; } + + /// + /// Whether this record can be modified/deleted ( means it's managed by CloudFlare). + /// + [JsonProperty("locked")] + public bool Locked { get; } + + /// + /// API item identifier tag. + /// + [JsonProperty("zone_id")] + public IdentifierTag ZoneId { get; } + + /// + /// The domain of the record. + /// + [JsonProperty("zone_name")] + public string ZoneName { get; } + + /// + /// When the record was created. + /// + [JsonProperty("created_on")] + public DateTimeOffset CreatedOn { get; } + + /// + /// When the record was last modified. + /// + [JsonProperty("modified_on")] + public DateTimeOffset ModifiedOn { get; } + + /// + /// Metadata about the record. + /// + [JsonProperty("data")] + public JObject Data { get; } + + /// + /// Extra CloudFlare-specific information about the record. + /// + [JsonProperty("meta")] + public JObject Meta { get; } + + /// + /// The priority of this is a record. + /// + [JsonProperty("priority")] + public int Priority { get; } + } +} diff --git a/src/CloudFlare.NET/DnsRecordClientExtensions.cs b/src/CloudFlare.NET/DnsRecordClientExtensions.cs new file mode 100644 index 0000000..91b8f6a --- /dev/null +++ b/src/CloudFlare.NET/DnsRecordClientExtensions.cs @@ -0,0 +1,32 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + + /// + /// Helper extension methods on . + /// + public static class DnsRecordClientExtensions + { + /// + /// Gets the zones for the subscription. + /// + /// The zones for the subscription. + public static Task> GetDnsRecordsAsync( + this IDnsRecordClient client, + IdentifierTag zoneId, + CloudFlareAuth auth = null) + { + if (client == null) + throw new ArgumentNullException(nameof(client)); + if (zoneId == null) + throw new ArgumentNullException(nameof(zoneId)); + + return client.GetDnsRecordsAsync(zoneId, CancellationToken.None, auth); + } + } +} diff --git a/src/CloudFlare.NET/DnsRecordType.cs b/src/CloudFlare.NET/DnsRecordType.cs new file mode 100644 index 0000000..e76c487 --- /dev/null +++ b/src/CloudFlare.NET/DnsRecordType.cs @@ -0,0 +1,25 @@ +namespace CloudFlare.NET +{ + using System.Diagnostics.CodeAnalysis; + +// ReSharper disable InconsistentNaming +#pragma warning disable 1591 + + /// + /// The type of a . + /// + [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", + Justification = "Names a self-explanatory.")] + public enum DnsRecordType + { + A, + AAAA, + CNAME, + TXT, + SRV, + LOC, + MX, + NS, + SPF, + } +} diff --git a/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs b/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs new file mode 100644 index 0000000..1f4e0e3 --- /dev/null +++ b/src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs @@ -0,0 +1,46 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + /// + /// Extension methods on to wrap the Zone APIs + /// + /// + public static class HttpClientDnsRecordExtensions + { + /// + /// Gets the zones for the account specified by the details. + /// + public static async Task> GetDnsRecordsAsync( + this HttpClient client, + IdentifierTag zoneId, + CancellationToken cancellationToken, + CloudFlareAuth auth) + { + if (client == null) + throw new ArgumentNullException(nameof(client)); + if (zoneId == null) + throw new ArgumentNullException(nameof(zoneId)); + if (auth == null) + throw new ArgumentNullException(nameof(auth)); + + Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}/dns_records"); + var request = new HttpRequestMessage(HttpMethod.Get, uri); + request.AddAuth(auth); + + HttpResponseMessage response = + await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) + .ConfigureAwait(false); + + return (await response + .GetResultAsync>(cancellationToken) + .ConfigureAwait(false)) + .Result; + } + } +} diff --git a/src/CloudFlare.NET/HttpClientExtensions.cs b/src/CloudFlare.NET/HttpClientExtensions.cs new file mode 100644 index 0000000..c87ed9d --- /dev/null +++ b/src/CloudFlare.NET/HttpClientExtensions.cs @@ -0,0 +1,64 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + /// + /// Extension methods on to wrap the CloudFlare APIs + /// + /// + public static class HttpClientExtensions + { + /// + /// Gets the base address of the CloudFlare API. + /// + public static Uri ZonesUri { get; } = new Uri(CloudFlareConstants.BaseUri, "zones"); + + /// + /// Gets the of a CloudFlare API . + /// + /// The type of the . + public static async Task> GetResultAsync( + this HttpResponseMessage response, + CancellationToken cancellationToken) + where T : class + { + if (response == null) + throw new ArgumentNullException(nameof(response)); + + if (response.IsSuccessStatusCode) + { + return await response + .Content + .ReadAsAsync>(cancellationToken) + .ConfigureAwait(false); + } + + var errorResult = await response + .Content + .ReadAsAsync(cancellationToken) + .ConfigureAwait(false); + + // TODO: Do some nice error handling. + throw new Exception("It's Not so good and stuff."); + } + + /// + /// Adds the authentication headers to the . + /// + public static void AddAuth(this HttpRequestMessage request, CloudFlareAuth auth) + { + if (request == null) + throw new ArgumentNullException(nameof(request)); + if (auth == null) + throw new ArgumentNullException(nameof(auth)); + + request.Headers.Add("X-Auth-Key", auth.Key); + request.Headers.Add("X-Auth-Email", auth.Email); + } + } +} diff --git a/src/CloudFlare.NET/HttpClientZoneExtensions.cs b/src/CloudFlare.NET/HttpClientZoneExtensions.cs index 9939450..129c197 100644 --- a/src/CloudFlare.NET/HttpClientZoneExtensions.cs +++ b/src/CloudFlare.NET/HttpClientZoneExtensions.cs @@ -10,7 +10,7 @@ /// /// Extension methods on to wrap the Zone APIs /// - /// + /// public static class HttpClientZoneExtensions { /// @@ -21,11 +21,10 @@ public static class HttpClientZoneExtensions /// /// Gets the zones for the account specified by the details. /// - /// The zones for the subscription. public static async Task> GetZonesAsync( this HttpClient client, - CloudFlareAuth auth, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + CloudFlareAuth auth) { if (client == null) throw new ArgumentNullException(nameof(client)); @@ -33,30 +32,16 @@ public static async Task> GetZonesAsync( throw new ArgumentNullException(nameof(auth)); var request = new HttpRequestMessage(HttpMethod.Get, ZonesUri); - request.Headers.Add("X-Auth-Key", auth.Key); - request.Headers.Add("X-Auth-Email", auth.Email); + request.AddAuth(auth); HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken) .ConfigureAwait(false); - if (response.IsSuccessStatusCode) - { - var result = await response - .Content - .ReadAsAsync>>(cancellationToken) - .ConfigureAwait(false); - - return result.Result; - } - - var errorResult = await response - .Content - .ReadAsAsync(cancellationToken) - .ConfigureAwait(false); - - // TODO: Do some nice error handling. - throw new Exception("It's Not so good and stuff."); + return (await response + .GetResultAsync>(cancellationToken) + .ConfigureAwait(false)) + .Result; } } } diff --git a/src/CloudFlare.NET/ICloudFlareClient.cs b/src/CloudFlare.NET/ICloudFlareClient.cs index d648740..b5cdfcf 100644 --- a/src/CloudFlare.NET/ICloudFlareClient.cs +++ b/src/CloudFlare.NET/ICloudFlareClient.cs @@ -8,7 +8,7 @@ /// The CloudFlare API Client. /// /// - public interface ICloudFlareClient : IZoneClient + public interface ICloudFlareClient : IZoneClient, IDnsRecordClient { } } diff --git a/src/CloudFlare.NET/IDnsRecordClient.cs b/src/CloudFlare.NET/IDnsRecordClient.cs new file mode 100644 index 0000000..2b631c6 --- /dev/null +++ b/src/CloudFlare.NET/IDnsRecordClient.cs @@ -0,0 +1,24 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + + /// + /// The CloudFlare DNS records for a zone API Client. + /// + /// + public interface IDnsRecordClient + { + /// + /// Gets the DNS records for the zone with the specified . + /// + /// + Task> GetDnsRecordsAsync( + IdentifierTag zoneId, + CancellationToken cancellationToken, + CloudFlareAuth auth = null); + } +} diff --git a/src/CloudFlare.NET/IZoneClient.cs b/src/CloudFlare.NET/IZoneClient.cs index a81c53c..2d63b44 100644 --- a/src/CloudFlare.NET/IZoneClient.cs +++ b/src/CloudFlare.NET/IZoneClient.cs @@ -9,7 +9,7 @@ /// /// The CloudFlare API Client. /// - /// + /// public interface IZoneClient { /// diff --git a/src/CloudFlare.NET/Properties/AssemblyInfo.cs b/src/CloudFlare.NET/Properties/AssemblyInfo.cs index 69e0c51..b6a97bf 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("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("0.0.2.0")] +[assembly: AssemblyFileVersion("0.0.2.0")] diff --git a/src/Tests/CloudFlare.NET.IntegrationTests/CloudFlare.NET.IntegrationTests.csproj b/src/Tests/CloudFlare.NET.IntegrationTests/CloudFlare.NET.IntegrationTests.csproj index a460320..be78d21 100644 --- a/src/Tests/CloudFlare.NET.IntegrationTests/CloudFlare.NET.IntegrationTests.csproj +++ b/src/Tests/CloudFlare.NET.IntegrationTests/CloudFlare.NET.IntegrationTests.csproj @@ -58,6 +58,7 @@ + diff --git a/src/Tests/CloudFlare.NET.IntegrationTests/DnsRecordsSpec.cs b/src/Tests/CloudFlare.NET.IntegrationTests/DnsRecordsSpec.cs new file mode 100644 index 0000000..6de85e2 --- /dev/null +++ b/src/Tests/CloudFlare.NET.IntegrationTests/DnsRecordsSpec.cs @@ -0,0 +1,29 @@ +namespace CloudFlare.NET +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Threading; + using Machine.Specifications; + + [Subject("Zones")] + public class DnsRecordsSpec + { + static IDnsRecordClient _client; + static CloudFlareAuth _auth; + static Zone _zone; + static IReadOnlyList _dnsRecords; + + Establish context = () => + { + var client = new CloudFlareClient(); + _auth = new CloudFlareAuth(Helper.AuthEmail, Helper.AuthKey); + _zone = client.GetZonesAsync(_auth).Await().AsTask.Result.First(); + _client = client; + }; + + Because of = () => _dnsRecords = _client.GetDnsRecordsAsync(_zone.Id, _auth).Await().AsTask.Result; + + It should_return_the_zones = () => _dnsRecords.ShouldNotBeEmpty(); + } +} diff --git a/src/Tests/CloudFlare.NET.IntegrationTests/ZonesSpec.cs b/src/Tests/CloudFlare.NET.IntegrationTests/ZonesSpec.cs index 2bc28c1..69208fb 100644 --- a/src/Tests/CloudFlare.NET.IntegrationTests/ZonesSpec.cs +++ b/src/Tests/CloudFlare.NET.IntegrationTests/ZonesSpec.cs @@ -8,7 +8,7 @@ [Subject("Zones")] public class ZonesSpec { - static ICloudFlareClient _client; + static IZoneClient _client; static CloudFlareAuth _auth; static IReadOnlyList _zones; diff --git a/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj b/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj index 0fdb83b..70496ec 100644 --- a/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj +++ b/src/Tests/CloudFlare.NET.Tests/CloudFlare.NET.Tests.csproj @@ -102,6 +102,7 @@ + @@ -109,6 +110,7 @@ + @@ -116,6 +118,8 @@ + + diff --git a/src/Tests/CloudFlare.NET.Tests/DnsRecordClientExtensionsSpec.cs b/src/Tests/CloudFlare.NET.Tests/DnsRecordClientExtensionsSpec.cs new file mode 100644 index 0000000..1349016 --- /dev/null +++ b/src/Tests/CloudFlare.NET.Tests/DnsRecordClientExtensionsSpec.cs @@ -0,0 +1,39 @@ +namespace CloudFlare.NET.DnsRecordSpec +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using Machine.Specifications; + using Moq; + using Ploeh.AutoFixture; + using MoqIt = Moq.It; + using It = Machine.Specifications.It; + + [Subject(typeof(DnsRecordClientExtensions))] + public class When_getting_dnsRecords : FixtureContext + { + static Mock _dnsRecordClientMock; + static CloudFlareAuth _auth; + static IdentifierTag _zoneId; + static IReadOnlyList _expected; + static IReadOnlyList _actual; + + Establish context = () => + { + _dnsRecordClientMock = _fixture.Create>(); + _auth = _fixture.Create(); + _zoneId = _fixture.Create(); + _expected = _fixture.Create(); + _dnsRecordClientMock + .Setup(c => c.GetDnsRecordsAsync(_zoneId, CancellationToken.None, _auth)) + .ReturnsAsync(_expected); + }; + + Because of = () => _actual = _dnsRecordClientMock.Object.GetDnsRecordsAsync(_zoneId, _auth).Await().AsTask.Result; + + It should_return_the_zones = () => _actual.ShouldBeTheSameAs(_expected); + } +} diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecord.json b/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecord.json new file mode 100644 index 0000000..82a0807 --- /dev/null +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecord.json @@ -0,0 +1,16 @@ +{ + "id": "9a7806061c88ada191ed06f989cc3dac", + "type": "A", + "name": "example.com", + "content": "1.2.3.4", + "proxiable": true, + "proxied": false, + "ttl": 120, + "locked": false, + "zone_id": "9a7806061c88ada191ed06f989cc3dac", + "zone_name": "example.com", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "data": {}, + "priority" : 10 +} \ No newline at end of file diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecordMinimal.json b/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecordMinimal.json new file mode 100644 index 0000000..52922cc --- /dev/null +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecordMinimal.json @@ -0,0 +1,5 @@ +{ + "id": "9a7806061c88ada191ed06f989cc3dac", + "name": "example.com", + "zone_id": "9a7806061c88ada191ed06f989cc3dac" +} \ No newline at end of file diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecordSerializationSpec.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecordSerializationSpec.cs new file mode 100644 index 0000000..2a524db --- /dev/null +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/DnsRecordSerializationSpec.cs @@ -0,0 +1,66 @@ +namespace CloudFlare.NET.Serialization +{ + using System; + using System.Collections.Generic; + using System.Linq; + using Machine.Specifications; + using Newtonsoft.Json.Linq; + + [Subject(typeof(DnsRecord))] + public class When_deserializing_dns_record + { + static JObject _json; + static DnsRecord _sut; + + Because of = () => + { + _json = SampleJson.DnsRecord; + _sut = _json.ToObject(); + }; + + It should_deserialize_id = () => _sut.Id.ToString().ShouldEqual(_json["id"].Value()); + + It should_deserialize_type = () => _sut.Type.ToString().ShouldEqual(_json["type"].Value()); + + It should_deserialize_name = () => _sut.Name.ShouldEqual(_json["name"].Value()); + + It should_deserialize_content = () => _sut.Content.ShouldEqual(_json["content"].Value()); + + It should_deserialize_proxiable = () => _sut.Proxiable.ShouldEqual(_json["proxiable"].Value()); + + It should_deserialize_proxied = () => _sut.Proxied.ShouldEqual(_json["proxied"].Value()); + + It should_deserialize_ttl = () => _sut.Ttl.ShouldEqual(_json["ttl"].Value()); + + It should_deserialize_locked = () => _sut.Locked.ShouldEqual(_json["locked"].Value()); + + It should_deserialize_zone_id = () => _sut.ZoneId.ToString().ShouldEqual(_json["zone_id"].Value()); + + It should_deserialize_zone_name = () => _sut.ZoneName.ShouldEqual(_json["zone_name"].Value()); + + It should_deserialize_created_on = () => _sut.CreatedOn.ShouldEqual(_json["created_on"].Value()); + + It should_deserialize_modified_on = () => _sut.ModifiedOn.ShouldEqual(_json["modified_on"].Value()); + + It should_deserialize_priority = () => _sut.Priority.ShouldEqual(_json["priority"].Value()); + } + + [Subject(typeof(DnsRecord))] + public class When_deserializing_dns_record_minimal + { + static JObject _json; + static DnsRecord _sut; + + Because of = () => + { + _json = SampleJson.DnsRecordMinimal; + _sut = _json.ToObject(); + }; + + It should_deserialize_id = () => _sut.Id.ToString().ShouldEqual(_json["id"].Value()); + + It should_deserialize_name = () => _sut.Name.ShouldEqual(_json["name"].Value()); + + It should_deserialize_zone_id = () => _sut.ZoneId.ToString().ShouldEqual(_json["zone_id"].Value()); + } +} diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/ErrorResponseSerializationSpec.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/ErrorResponseSerializationSpec.cs index 1c0fb27..5b8a142 100644 --- a/src/Tests/CloudFlare.NET.Tests/Serialization/ErrorResponseSerializationSpec.cs +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/ErrorResponseSerializationSpec.cs @@ -9,24 +9,24 @@ [Subject(typeof(CloudFlareResponseBase))] public class When_deserializing_error_response { - static JObject _errorResponseJson; - static CloudFlareResponseBase _errorResponse; + static JObject _json; + static CloudFlareResponseBase _sut; Because of = () => { - _errorResponseJson = SampleJson.ErrorResponse; - _errorResponse = _errorResponseJson.ToObject(); + _json = SampleJson.ErrorResponse; + _sut = _json.ToObject(); }; It should_deserialize_success = - () => _errorResponse.Success.ShouldEqual(_errorResponseJson["success"].Value()); + () => _sut.Success.ShouldEqual(_json["success"].Value()); It should_deserialize_errors = - () => _errorResponse.Errors - .ShouldContainOnly(_errorResponseJson["errors"].ToObject>()); + () => _sut.Errors + .ShouldContainOnly(_json["errors"].ToObject>()); It should_deserialize_messages = - () => _errorResponse.Messages - .ShouldContainOnly(_errorResponseJson["messages"].ToObject>()); + () => _sut.Messages + .ShouldContainOnly(_json["messages"].ToObject>()); } } diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/SampleJson.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/SampleJson.cs index 1d735a6..fb2f137 100644 --- a/src/Tests/CloudFlare.NET.Tests/Serialization/SampleJson.cs +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/SampleJson.cs @@ -25,6 +25,10 @@ public static JObject Load(string fileName) } } + public static JObject DnsRecord => Load(nameof(DnsRecord)); + + public static JObject DnsRecordMinimal => Load(nameof(DnsRecordMinimal)); + public static JObject ErrorResponse => Load(nameof(ErrorResponse)); public static JObject SuccessResponse => Load(nameof(SuccessResponse)); diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/SuccessResponseSerializationSpec.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/SuccessResponseSerializationSpec.cs index e0928cd..3167632 100644 --- a/src/Tests/CloudFlare.NET.Tests/Serialization/SuccessResponseSerializationSpec.cs +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/SuccessResponseSerializationSpec.cs @@ -9,43 +9,43 @@ [Subject(typeof(CloudFlareResponse<>))] public class When_deserializing_success_response { - static JObject _successResponseJson; - static CloudFlareResponse _successResponse; + static JObject _json; + static CloudFlareResponse _sut; Because of = () => { - _successResponseJson = SampleJson.SuccessResponse; - _successResponse = _successResponseJson.ToObject>(); + _json = SampleJson.SuccessResponse; + _sut = _json.ToObject>(); }; It should_deserialize_result = - () => _successResponse.Result["id"].ShouldEqual(_successResponseJson["result"]["id"]); + () => _sut.Result["id"].ShouldEqual(_json["result"]["id"]); It should_deserialize_success = - () => _successResponse.Success.ShouldEqual(_successResponseJson["success"].Value()); + () => _sut.Success.ShouldEqual(_json["success"].Value()); It should_deserialize_errors = - () => _successResponse.Errors - .ShouldContainOnly(_successResponseJson["errors"].ToObject>()); + () => _sut.Errors + .ShouldContainOnly(_json["errors"].ToObject>()); It should_deserialize_messages = - () => _successResponse.Messages - .ShouldContainOnly(_successResponseJson["messages"].ToObject>()); + () => _sut.Messages + .ShouldContainOnly(_json["messages"].ToObject>()); It should_deserialize_result_info_page = - () => _successResponse.ResultInfo.Page - .ShouldEqual(_successResponseJson["result_info"]["page"].Value()); + () => _sut.ResultInfo.Page + .ShouldEqual(_json["result_info"]["page"].Value()); It should_deserialize_result_info_per_page = - () => _successResponse.ResultInfo.PerPage - .ShouldEqual(_successResponseJson["result_info"]["per_page"].Value()); + () => _sut.ResultInfo.PerPage + .ShouldEqual(_json["result_info"]["per_page"].Value()); It should_deserialize_result_info_count = - () => _successResponse.ResultInfo.Count - .ShouldEqual(_successResponseJson["result_info"]["count"].Value()); + () => _sut.ResultInfo.Count + .ShouldEqual(_json["result_info"]["count"].Value()); It should_deserialize_result_info_total_count = - () => _successResponse.ResultInfo.TotalCount - .ShouldEqual(_successResponseJson["result_info"]["total_count"].Value()); + () => _sut.ResultInfo.TotalCount + .ShouldEqual(_json["result_info"]["total_count"].Value()); } } diff --git a/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs b/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs index a638459..2d7e5b0 100644 --- a/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs +++ b/src/Tests/CloudFlare.NET.Tests/Serialization/ZoneSerializationSpec.cs @@ -9,56 +9,56 @@ [Subject(typeof(Zone))] public class When_deserializing_zone { - static JObject _zoneJson; - static Zone _zone; + static JObject _json; + static Zone _sut; Because of = () => { - _zoneJson = SampleJson.Zone; - _zone = _zoneJson.ToObject(); + _json = SampleJson.Zone; + _sut = _json.ToObject(); }; - It should_deserialize_id = () => _zone.Id.ToString().ShouldEqual(_zoneJson["id"].Value()); + It should_deserialize_id = () => _sut.Id.ToString().ShouldEqual(_json["id"].Value()); - It should_deserialize_name = () => _zone.Name.ShouldEqual(_zoneJson["name"].Value()); + It should_deserialize_name = () => _sut.Name.ShouldEqual(_json["name"].Value()); It should_deserialize_development_mode = - () => _zone.DevelopmentMode.ShouldEqual(_zoneJson["development_mode"].Value()); + () => _sut.DevelopmentMode.ShouldEqual(_json["development_mode"].Value()); It should_deserialize_original_name_servers = - () => _zone.OriginalNameServers - .ShouldContainOnly(_zoneJson["original_name_servers"].ToObject>()); + () => _sut.OriginalNameServers + .ShouldContainOnly(_json["original_name_servers"].ToObject>()); It should_deserialize_original_registrar = - () => _zone.OriginalRegistrar.ShouldEqual(_zoneJson["original_registrar"].Value()); + () => _sut.OriginalRegistrar.ShouldEqual(_json["original_registrar"].Value()); It should_deserialize_original_dnshost = - () => _zone.OriginalDnshost.ShouldEqual(_zoneJson["original_dnshost"].Value()); + () => _sut.OriginalDnshost.ShouldEqual(_json["original_dnshost"].Value()); It should_deserialize_created_on = - () => _zone.CreatedOn.ShouldEqual(_zoneJson["created_on"].Value()); + () => _sut.CreatedOn.ShouldEqual(_json["created_on"].Value()); It should_deserialize_modified_on = - () => _zone.ModifiedOn.ShouldEqual(_zoneJson["modified_on"].Value()); + () => _sut.ModifiedOn.ShouldEqual(_json["modified_on"].Value()); It should_deserialize_name_servers = - () => _zone.NameServers.ShouldContainOnly(_zoneJson["name_servers"].ToObject>()); + () => _sut.NameServers.ShouldContainOnly(_json["name_servers"].ToObject>()); } [Subject(typeof(Zone))] public class When_deserializing_zone_minimal { - static JObject _zoneJson; - static Zone _zone; + static JObject _json; + static Zone _sut; Because of = () => { - _zoneJson = SampleJson.ZoneMinimal; - _zone = _zoneJson.ToObject(); + _json = SampleJson.ZoneMinimal; + _sut = _json.ToObject(); }; - It should_deserialize_id = () => _zone.Id.ToString().ShouldEqual(_zoneJson["id"].Value()); + It should_deserialize_id = () => _sut.Id.ToString().ShouldEqual(_json["id"].Value()); - It should_deserialize_name = () => _zone.Name.ShouldEqual(_zoneJson["name"].Value()); + It should_deserialize_name = () => _sut.Name.ShouldEqual(_json["name"].Value()); } }