diff --git a/test/Unit/IpData.Tests/CarrierData.json b/test/Unit/IpData.Tests/CarrierData.json new file mode 100644 index 0000000..67ea296 --- /dev/null +++ b/test/Unit/IpData.Tests/CarrierData.json @@ -0,0 +1,5 @@ +{ + "name": "T-Mobile", + "mcc": "310", + "mnc": "160" +} \ No newline at end of file diff --git a/test/Unit/IpData.Tests/DataSources/TestDataSource.cs b/test/Unit/IpData.Tests/DataSources/TestDataSource.cs deleted file mode 100644 index 54fb07b..0000000 --- a/test/Unit/IpData.Tests/DataSources/TestDataSource.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Collections.Generic; - -namespace IpData.Tests.DataSources -{ - public static class TestDataSource - { - public static IEnumerable EmptyOrWhitespaceString() - { - yield return new object[] { "" }; - yield return new object[] { " " }; - yield return new object[] { null }; - } - - public static IEnumerable IpInfoData() - { - yield return new object[] { "{\"ip\":\"91.225.201.108\",\"is_eu\":false,\"city\":\"Lviv\",\"region\":\"L'vivs'ka Oblast'\",\"region_code\":\"46\",\"country_name\":\"Ukraine\",\"country_code\":\"UA\",\"continent_name\":\"Europe\",\"continent_code\":\"EU\",\"latitude\":49.8486,\"longitude\":24.0323,\"postal\":\"79000\",\"calling_code\":\"380\",\"flag\":\"https:\\/\\/ipdata.co\\/flags\\/ua.png\",\"emoji_flag\":\"\uD83C\uDDFA\uD83C\uDDE6\",\"emoji_unicode\":\"U+1F1FA U+1F1E6\",\"asn\":{\"asn\":\"AS49824\",\"name\":\"PC \\\"Astra-net\\\"\",\"domain\":\"astra.in.ua\",\"route\":\"91.225.200.0\\/22\",\"type\":\"isp\"},\"languages\":[{\"name\":\"Ukrainian\",\"native\":\"\u0423\u043A\u0440\u0430\u0457\u043D\u0441\u044C\u043A\u0430\"}],\"currency\":{\"name\":\"Ukrainian Hryvnia\",\"code\":\"UAH\",\"symbol\":\"\u20B4\",\"native\":\"\u20B4\",\"plural\":\"Ukrainian hryvnias\"},\"time_zone\":{\"name\":\"Europe\\/Kiev\",\"abbr\":\"EET\",\"offset\":\"+0200\",\"is_dst\":false,\"current_time\":\"2020-01-30T23:16:19.129316+02:00\"},\"threat\":{\"is_tor\":false,\"is_proxy\":false,\"is_anonymous\":false,\"is_known_attacker\":false,\"is_known_abuser\":false,\"is_threat\":false,\"is_bogon\":false}}" }; - } - - public static IEnumerable CarrierData() - { - yield return new object[] { "{\"name\":\"T-Mobile\",\"mcc\":\"310\",\"mnc\":\"160\"\r\n}" }; - } - } -} diff --git a/test/Unit/IpData.Tests/Exceptions/ApiExceptionTests.cs b/test/Unit/IpData.Tests/Exceptions/ApiExceptionTests.cs index a7a9879..6be6630 100644 --- a/test/Unit/IpData.Tests/Exceptions/ApiExceptionTests.cs +++ b/test/Unit/IpData.Tests/Exceptions/ApiExceptionTests.cs @@ -1,82 +1,55 @@ -using System.Globalization; -using FluentAssertions; -using IpData.Exceptions; -using Xunit; - -namespace IpData.Tests.Exceptions -{ - public class ApiExceptionTests - { - [Fact] - public void ApiException_WhenCreate_ShouldReturnStatusCode() - { - // Act - var sut = new ApiException(); - - // Assert - sut.StatusCode.Should().Be(0); - } - - [Fact] - public void ApiExceptionException_WhenCreateWithoutParams_ShouldReturnApiError() - { - // Act - var sut = new ApiException(); - - // Assert - sut.ApiError.Should().NotBeNull(); - } - - [Fact] - public void ApiExceptionException_WhenCreateWithoutParams_ShouldBeDefaultMessage() - { - // Act - var sut = new ApiException(); - - // Assert - sut.Message.Should().Be("An error occurred with this API request"); - } - - [Theory, AutoMoqData] - public void ApiExceptionException_WhenCreateWithContent_ShouldReturnApiErrorWithMessage(string content) - { - // Act - var sut = new ApiException(content); - - // Assert - sut.ApiError.Message.Should().Be(content); - } - - [Theory, AutoMoqData] - public void ApiExceptionException_WhenCreateWithContent_ShouldBeMessage(string content) - { - // Act - var sut = new ApiException(content); - - // Assert - sut.Message.Should().Be(content); - } - - [Theory, AutoMoqData] - public void ApiExceptionException_WhenCreateWithValidJsonContent_ShouldDeserializeApiError(string message) - { - // Act - var json = $"{{\"message\":\"{message}\"}}"; - var sut = new ApiException(json); - - // Assert - sut.ApiError.Message.Should().Be(message); - } - - [Theory, AutoMoqData] - public void ApiExceptionException_WhenCreateWithValidJsonContent_ShouldBeDeserializedMessage(string message) - { - // Act - var json = $"{{\"message\":\"{message}\"}}"; - var sut = new ApiException(json); - - // Assert - sut.Message.Should().Be(message); - } - } -} +using System.Globalization; +using FluentAssertions; +using FluentAssertions.Execution; +using IpData.Exceptions; +using Xunit; + +namespace IpData.Tests.Exceptions +{ + public class ApiExceptionTests + { + [Fact] + public void ApiException_WhenCreate_ShouldSetDefaultValues() + { + // Act + var sut = new ApiException(); + + // Assert + using (new AssertionScope()) + { + sut.StatusCode.Should().Be(0); + sut.ApiError.Should().NotBeNull(); + sut.Message.Should().NotBeEmpty(); + } + } + + [Theory, AutoMoqData] + public void ApiExceptionException_WhenCreateWithContent_ShouldSetMessage(string content) + { + // Act + var sut = new ApiException(content); + + // Assert + using (new AssertionScope()) + { + sut.ApiError.Message.Should().Be(content); + sut.Message.Should().Be(content); + } + } + + [Theory, AutoMoqData] + public void ApiExceptionException_WhenCreateWithValidJsonContent_ShouldDeserializeToMessage(string message) + { + // Act + var json = $"{{\"message\":\"{message}\"}}"; + var sut = new ApiException(json); + + // Assert + using (new AssertionScope()) + { + sut.ApiError.Message.Should().Be(message); + sut.Message.Should().Be(message); + } + } + } +} diff --git a/test/Unit/IpData.Tests/Exceptions/BadRequestExceptionTests.cs b/test/Unit/IpData.Tests/Exceptions/BadRequestExceptionTests.cs index a2e74b6..adeeab1 100644 --- a/test/Unit/IpData.Tests/Exceptions/BadRequestExceptionTests.cs +++ b/test/Unit/IpData.Tests/Exceptions/BadRequestExceptionTests.cs @@ -1,50 +1,39 @@ -using System.Net; -using FluentAssertions; -using IpData.Exceptions; -using Xunit; - -namespace IpData.Tests.Exceptions -{ - public class BadRequestExceptionTests - { - [Fact] - public void BadRequestException_WhenCreate_ShouldReturnStatusCode() - { - // Act - var sut = new BadRequestException(); - - // Assert - sut.StatusCode.Should().Be(HttpStatusCode.BadRequest); - } - - [Fact] - public void BadRequestException_WhenCreateWithoutParams_ShouldReturnApiError() - { - // Act - var sut = new BadRequestException(); - - // Assert - sut.ApiError.Should().NotBeNull(); - } - - [Theory, AutoMoqData] - public void BadRequestException_WhenCreateWithContent_ShouldReturnApiErrorWithMessage(string content) - { - // Act - var sut = new BadRequestException(content); - - // Assert - sut.ApiError.Message.Should().Be(content); - } - - [Theory, AutoMoqData] - public void BadRequestException_WhenCreateWithContent_ShouldBeMessage(string content) - { - // Act - var sut = new BadRequestException(content); - - // Assert - sut.Message.Should().Be(content); - } - } -} +using System.Net; +using FluentAssertions; +using FluentAssertions.Execution; +using IpData.Exceptions; +using Xunit; + +namespace IpData.Tests.Exceptions +{ + public class BadRequestExceptionTests + { + [Fact] + public void BadRequestException_WhenCreate_ShouldSetDefaultValues() + { + // Act + var sut = new BadRequestException(); + + // Assert + using (new AssertionScope()) + { + sut.StatusCode.Should().Be(HttpStatusCode.BadRequest); + sut.ApiError.Should().NotBeNull(); + } + } + + [Theory, AutoMoqData] + public void BadRequestException_WhenCreateWithContent_ShouldSetMessage(string content) + { + // Act + var sut = new BadRequestException(content); + + // Assert + using (new AssertionScope()) + { + sut.ApiError.Message.Should().Be(content); + sut.Message.Should().Be(content); + } + } + } +} diff --git a/test/Unit/IpData.Tests/Exceptions/Factory/ApiExceptionFactoryTests.cs b/test/Unit/IpData.Tests/Exceptions/Factory/ApiExceptionFactoryTests.cs index 2d807cb..af43bd3 100644 --- a/test/Unit/IpData.Tests/Exceptions/Factory/ApiExceptionFactoryTests.cs +++ b/test/Unit/IpData.Tests/Exceptions/Factory/ApiExceptionFactoryTests.cs @@ -1,63 +1,29 @@ -using System.Net; -using FluentAssertions; -using IpData.Exceptions; -using IpData.Exceptions.Factory; -using Xunit; - -namespace IpData.Tests.Exceptions.Factory -{ - public class ApiExceptionFactoryTests - { - [Fact] - public void Create_WhenCalledWithBadRequest_ShouldReturnBadRequestException() - { - // Arrange - var sut = new ApiExceptionFactory(); - - // Act - var actual = sut.Create(HttpStatusCode.BadRequest, string.Empty); - - // Assert - actual.Should().BeOfType(); - } - - [Fact] - public void Create_WhenCalledWithUnauthorized_ShouldReturnUnauthorizedException() - { - // Arrange - var sut = new ApiExceptionFactory(); - - // Act - var actual = sut.Create(HttpStatusCode.Unauthorized, string.Empty); - - // Assert - actual.Should().BeOfType(); - } - - [Fact] - public void Create_WhenCalledWithForbidden_ShouldReturnForbiddenException() - { - // Arrange - var sut = new ApiExceptionFactory(); - - // Act - var actual = sut.Create(HttpStatusCode.Forbidden, string.Empty); - - // Assert - actual.Should().BeOfType(); - } - - [Fact] - public void Create_WhenCalled_ShouldReturnApiException() - { - // Arrange - var sut = new ApiExceptionFactory(); - - // Act - var actual = sut.Create(HttpStatusCode.InternalServerError, string.Empty); - - // Assert - actual.Should().BeOfType(); - } - } -} +using System; +using System.Net; +using FluentAssertions; +using IpData.Exceptions; +using IpData.Exceptions.Factory; +using Xunit; + +namespace IpData.Tests.Exceptions.Factory +{ + public class ApiExceptionFactoryTests + { + [Theory] + [InlineData(HttpStatusCode.InternalServerError, typeof(ApiException))] + [InlineData(HttpStatusCode.Unauthorized, typeof(UnauthorizedException))] + [InlineData(HttpStatusCode.BadRequest, typeof(BadRequestException))] + [InlineData(HttpStatusCode.Forbidden, typeof(ForbiddenException))] + public void Create_WhenCalledWithStatusCode_ShouldThrowExpectedException(HttpStatusCode statusCode, Type expected) + { + // Arrange + var sut = new ApiExceptionFactory(); + + // Act + var actual = sut.Create(statusCode, string.Empty); + + // Assert + actual.Should().BeOfType(expected); + } + } +} diff --git a/test/Unit/IpData.Tests/Exceptions/ForbiddenExceptionTests.cs b/test/Unit/IpData.Tests/Exceptions/ForbiddenExceptionTests.cs index 1cbd7b2..88d1b79 100644 --- a/test/Unit/IpData.Tests/Exceptions/ForbiddenExceptionTests.cs +++ b/test/Unit/IpData.Tests/Exceptions/ForbiddenExceptionTests.cs @@ -1,50 +1,39 @@ -using System.Net; -using FluentAssertions; -using IpData.Exceptions; -using Xunit; - -namespace IpData.Tests.Exceptions -{ - public class ForbiddenExceptionTests - { - [Fact] - public void ForbiddenException_WhenCreate_ShouldReturnStatusCode() - { - // Act - var sut = new ForbiddenException(); - - // Assert - sut.StatusCode.Should().Be(HttpStatusCode.Forbidden); - } - - [Fact] - public void ForbiddenException_WhenCreateWithoutParams_ShouldReturnApiError() - { - // Act - var sut = new ForbiddenException(); - - // Assert - sut.ApiError.Should().NotBeNull(); - } - - [Theory, AutoMoqData] - public void ForbiddenException_WhenCreateWithContent_ShouldReturnApiErrorWithMessage(string content) - { - // Act - var sut = new ForbiddenException(content); - - // Assert - sut.ApiError.Message.Should().Be(content); - } - - [Theory, AutoMoqData] - public void ForbiddenException_WhenCreateWithContent_ShouldBeMessage(string content) - { - // Act - var sut = new ForbiddenException(content); - - // Assert - sut.Message.Should().Be(content); - } - } -} +using System.Net; +using FluentAssertions; +using FluentAssertions.Execution; +using IpData.Exceptions; +using Xunit; + +namespace IpData.Tests.Exceptions +{ + public class ForbiddenExceptionTests + { + [Fact] + public void ForbiddenException_WhenCreate_ShouldSetDefaultValues() + { + // Act + var sut = new ForbiddenException(); + + // Assert + using (new AssertionScope()) + { + sut.StatusCode.Should().Be(HttpStatusCode.Forbidden); + sut.ApiError.Should().NotBeNull(); + } + } + + [Theory, AutoMoqData] + public void ForbiddenException_WhenCreateWithContent_ShouldSetMessage(string content) + { + // Act + var sut = new ForbiddenException(content); + + // Assert + using (new AssertionScope()) + { + sut.ApiError.Message.Should().Be(content); + sut.Message.Should().Be(content); + } + } + } +} diff --git a/test/Unit/IpData.Tests/Exceptions/UnauthorizedExceptionTests.cs b/test/Unit/IpData.Tests/Exceptions/UnauthorizedExceptionTests.cs index 4971005..10b11c1 100644 --- a/test/Unit/IpData.Tests/Exceptions/UnauthorizedExceptionTests.cs +++ b/test/Unit/IpData.Tests/Exceptions/UnauthorizedExceptionTests.cs @@ -1,50 +1,39 @@ -using System.Net; -using FluentAssertions; -using IpData.Exceptions; -using Xunit; - -namespace IpData.Tests.Exceptions -{ - public class UnauthorizedExceptionTests - { - [Fact] - public void UnauthorizedException_WhenCreate_ShouldReturnStatusCode() - { - // Act - var sut = new UnauthorizedException(); - - // Assert - sut.StatusCode.Should().Be(HttpStatusCode.Unauthorized); - } - - [Fact] - public void UnauthorizedException_WhenCreateWithoutParams_ShouldReturnApiError() - { - // Act - var sut = new UnauthorizedException(); - - // Assert - sut.ApiError.Should().NotBeNull(); - } - - [Theory, AutoMoqData] - public void UnauthorizedException_WhenCreateWithContent_ShouldReturnApiErrorWithMessage(string content) - { - // Act - var sut = new UnauthorizedException(content); - - // Assert - sut.ApiError.Message.Should().Be(content); - } - - [Theory, AutoMoqData] - public void UnauthorizedException_WhenCreateWithContent_ShouldBeMessage(string content) - { - // Act - var sut = new UnauthorizedException(content); - - // Assert - sut.Message.Should().Be(content); - } - } -} +using System.Net; +using FluentAssertions; +using FluentAssertions.Execution; +using IpData.Exceptions; +using Xunit; + +namespace IpData.Tests.Exceptions +{ + public class UnauthorizedExceptionTests + { + [Fact] + public void UnauthorizedException_WhenCreate_ShouldSetDefaultValues() + { + // Act + var sut = new UnauthorizedException(); + + // Assert + using (new AssertionScope()) + { + sut.StatusCode.Should().Be(HttpStatusCode.Unauthorized); + sut.ApiError.Should().NotBeNull(); + } + } + + [Theory, AutoMoqData] + public void UnauthorizedException_WhenCreateWithContent_ShouldSetMessage(string content) + { + // Act + var sut = new UnauthorizedException(content); + + // Assert + using (new AssertionScope()) + { + sut.ApiError.Message.Should().Be(content); + sut.Message.Should().Be(content); + } + } + } +} diff --git a/test/Unit/IpData.Tests/Http/HttpClientAdapterTests.cs b/test/Unit/IpData.Tests/Http/HttpClientAdapterTests.cs index 14ff7d6..11ea149 100644 --- a/test/Unit/IpData.Tests/Http/HttpClientAdapterTests.cs +++ b/test/Unit/IpData.Tests/Http/HttpClientAdapterTests.cs @@ -1,37 +1,35 @@ -using FluentAssertions; -using System; -using System.Net.Http; -using Xunit; - -namespace IpData.Tests.Http -{ - public class HttpClientAdapterTests - { - [Theory, AutoMoqData] - public void SendAsync_WhenCalled_ShouldReturnHttpResponseMessage( - HttpRequestMessage requestMessage) - { - // Arrange - var sut = new HttpClientAdapter(); - - // Act - var actual = sut.SendAsync(requestMessage); - - // Assert - actual.Should().NotBeNull(); - } - - [Fact] - public void HttpClientAdapter_WhenCreateWithInvalidHttpClient_ShouldThrowArgumentNullException() - { - // Act - Action act = () => new HttpClientAdapter(null); - - // Assert - act.Should() - .Throw() - .Where(e => e.ParamName == "httpClient") - .Where(e => e.Message.Contains("The httpClient can't be null")); - } - } -} +using FluentAssertions; +using System; +using System.Net.Http; +using Xunit; + +namespace IpData.Tests.Http +{ + public class HttpClientAdapterTests + { + [Theory, AutoMoqData] + public void SendAsync_WhenCalled_ShouldReturnHttpResponseMessage( + HttpRequestMessage requestMessage) + { + // Arrange + var sut = new HttpClientAdapter(); + + // Act + var actual = sut.SendAsync(requestMessage); + + // Assert + actual.Should().NotBeNull(); + } + + [Fact] + public void HttpClientAdapter_WhenCreateWithInvalidHttpClient_ShouldThrowArgumentNullException() + { + // Act + Action act = () => new HttpClientAdapter(null); + + // Assert + act.Should().Throw() + .Which.Message.Should().NotBeNullOrEmpty(); + } + } +} diff --git a/test/Unit/IpData.Tests/Http/Serializer/JsonSerializerTests.cs b/test/Unit/IpData.Tests/Http/Serializer/JsonSerializerTests.cs index da568e4..4f376a6 100644 --- a/test/Unit/IpData.Tests/Http/Serializer/JsonSerializerTests.cs +++ b/test/Unit/IpData.Tests/Http/Serializer/JsonSerializerTests.cs @@ -1,7 +1,7 @@ using FluentAssertions; using IpData.Http.Serializer; using IpData.Models; -using IpData.Tests.DataSources; +using IpData.Tests.Infrastructure; using Xunit; namespace IpData.Tests.Http.Serializer @@ -11,7 +11,7 @@ public class JsonSerializerTests private readonly JsonSerializer _sut = new JsonSerializer(); [Theory] - [MemberData(nameof(TestDataSource.IpInfoData), MemberType = typeof(TestDataSource))] + [JsonFile("IpInfo.json")] public void Deserialize_WhenCalled_ReturnedIpInfo(string json) { // Act @@ -32,7 +32,7 @@ public void SerializeIpInfo_WhenCalled_ReturnedString(IpInfo ipInfo) } [Theory] - [MemberData(nameof(TestDataSource.CarrierData), MemberType = typeof(TestDataSource))] + [JsonFile("CarrierData.json")] public void Deserialize_WhenCalled_ReturnedCarrierInfo(string json) { // Act diff --git a/test/Unit/IpData.Tests/Infrastructure/JsonFileAttribute.cs b/test/Unit/IpData.Tests/Infrastructure/JsonFileAttribute.cs new file mode 100644 index 0000000..988005a --- /dev/null +++ b/test/Unit/IpData.Tests/Infrastructure/JsonFileAttribute.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using Xunit.Sdk; + +namespace IpData.Tests.Infrastructure +{ + public class JsonFileAttribute : DataAttribute + { + private readonly string _filePath; + + public JsonFileAttribute(string filePath) + { + _filePath = filePath; + } + + public override IEnumerable GetData(MethodInfo testMethod) + { + _ = testMethod ?? throw new ArgumentNullException(nameof(testMethod)); + + var path = Path.IsPathRooted(_filePath) + ? _filePath + : Path.GetRelativePath(Directory.GetCurrentDirectory(), _filePath); + + if (File.Exists(path)) + { + yield return new object[] { File.ReadAllText(path) }; + } + else + { + throw new ArgumentException($"Could not find file at path: {path}"); + } + } + } +} \ No newline at end of file diff --git a/test/Unit/IpData.Tests/IpData.Tests.csproj b/test/Unit/IpData.Tests/IpData.Tests.csproj index 5cd9d6c..079795d 100644 --- a/test/Unit/IpData.Tests/IpData.Tests.csproj +++ b/test/Unit/IpData.Tests/IpData.Tests.csproj @@ -32,4 +32,13 @@ + + + PreserveNewest + + + PreserveNewest + + + diff --git a/test/Unit/IpData.Tests/IpDataClientTests.cs b/test/Unit/IpData.Tests/IpDataClientTests.cs index d20a45e..b0ac2dd 100644 --- a/test/Unit/IpData.Tests/IpDataClientTests.cs +++ b/test/Unit/IpData.Tests/IpDataClientTests.cs @@ -2,10 +2,8 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; -using AutoFixture.Xunit2; using FluentAssertions; using IpData.Exceptions; -using IpData.Tests.DataSources; using Moq; using Xunit; @@ -14,819 +12,505 @@ namespace IpData.Tests public class IpDataClientTests { [Theory] - [MemberData(nameof(TestDataSource.EmptyOrWhitespaceString), MemberType = typeof(TestDataSource))] - public void IpDataClient_WhenCreatedWithInvalidApiKey_ThrowArgumentException( - string apiKey) + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public void IpDataClient_WhenCreatedWithInvalidApiKey_ThrowArgumentException(string invalidApiKey) { // Arrange - Action act = () => new IpDataClient(apiKey); + Action act = () => new IpDataClient(invalidApiKey); // Act/Assert - act.Should() - .Throw() - .Where(e => e.ParamName == nameof(apiKey)) - .Where(e => e.Message.Contains($"The {nameof(apiKey)} {apiKey} must be not empty or whitespace string")); + act.Should().Throw() + .Which.Message.Should().NotBeNullOrEmpty(); } [Theory, AutoMoqData] - public void IpDataClient_WhenCreatedWithValidApiKey_ShouldCreateClient( - string apiKey) + public void IpDataClient_WhenCreatedWithValidApiKey_ShouldCreateClient(string validApiKey) { // Act - var sut = new IpDataClient(apiKey); + var sut = new IpDataClient(validApiKey); // Assert - sut.ApiKey.Should().Be(apiKey); + sut.ApiKey.Should().Be(validApiKey); } [Theory, AutoMoqData] - public void IpDataClient_WhenCreatedWithInvalidIHttpClient_ThrowArgumentNullException( - string apiKey) + public void IpDataClient_WhenCreatedWithInvalidIHttpClient_ThrowArgumentNullException(string validApiKey) { // Arrange - Action act = () => new IpDataClient(apiKey, (IHttpClient)null); + Action act = () => new IpDataClient(validApiKey, null as IHttpClient); // Act/Assert - act.Should() - .Throw() - .Where(e => e.ParamName == "httpClient") - .Where(e => e.Message.Contains("The httpClient can't be null")); + act.Should().Throw() + .Which.Message.Should().NotBeNullOrEmpty(); } [Theory, AutoMoqData] public void IpDataClient_WhenCreatedWithValidApiKeyAndHttpClient_ShouldCreateClient( IHttpClient httpClient, - string apiKey) + string validApiKey) { // Arrange - Action act = () => new IpDataClient(apiKey, httpClient); + Action act = () => new IpDataClient(validApiKey, httpClient); // Act/Assert act.Should().NotThrow(); } - - [Theory, AutoMoqData] - public async Task Lookup_WhenStatusCode400_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + + [Fact] + public Task Lookup_WhenStatusCode400_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Lookup(); - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup().ConfigureAwait(false); }; - // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenStatusCode403_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenStatusCode403_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup().ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Lookup(); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenStatusCode401_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenStatusCode401_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup().ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Lookup(); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenUnknownStatusCode_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenUnknownStatusCode_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup().ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Lookup(); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIp_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIp_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("69.78.70.144").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Lookup(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIp_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIp_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("69.78.70.144").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Lookup(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIp_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIp_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("69.78.70.144").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Lookup(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIp_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIp_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("69.78.70.144").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Lookup(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIpList_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIpList_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - var ipList = new string[] { "1.1.1.1", "2.2.2.2", "3.3.3.3" }; - Func act = async () => { await sut.Lookup(ipList).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + var ipList = new[] { IPAddress.Any.ToString(), IPAddress.Broadcast.ToString() }; + Func act = () => sut.Lookup(ipList); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIpList_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIpList_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - var ipList = new string[] { "1.1.1.1", "2.2.2.2", "3.3.3.3" }; - Func act = async () => { await sut.Lookup(ipList).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + var ipList = new[] { IPAddress.Any.ToString(), IPAddress.Broadcast.ToString() }; + Func act = () => sut.Lookup(ipList); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIpList_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIpList_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - var ipList = new string[] { "1.1.1.1", "2.2.2.2", "3.3.3.3" }; - Func act = async () => { await sut.Lookup(ipList).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + var ipList = new[] { IPAddress.Any.ToString(), IPAddress.Broadcast.ToString() }; + Func act = () => sut.Lookup(ipList); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithIpList_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithIpList_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - var ipList = new string[] { "1.1.1.1", "2.2.2.2", "3.3.3.3" }; - Func act = async () => { await sut.Lookup(ipList).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + var ipList = new[] { IPAddress.Any.ToString(), IPAddress.Broadcast.ToString() }; + Func act = () => sut.Lookup(ipList); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelector_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelector_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.CountryName).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.CountryName); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelector_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelector_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.CountryName).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.CountryName); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelector_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelector_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.CountryName).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.CountryName); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelector_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelector_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.CountryName).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.CountryName); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelectors_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + + [Fact] + public Task Lookup_WhenCalledWithSelectors_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.Asn, x => x.City).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.Asn, x => x.City); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelectors_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelectors_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.Asn, x => x.City).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = async () => { await sut.Lookup(IPAddress.Any.ToString(), x => x.Asn, x => x.City); }; // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelectors_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelectors_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.Asn, x => x.City).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.Asn, x => x.City); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Lookup_WhenCalledWithSelectors_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Lookup_WhenCalledWithSelectors_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Lookup("1.1.1.1", x => x.Asn, x => x.City).ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Lookup(IPAddress.Any.ToString(), x => x.Asn, x => x.City); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Carrier_WhenCalledWithIp_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Carrier_WhenCalledWithIp_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Carrier("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Carrier(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Carrier_WhenCalledWithIp_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Carrier_WhenCalledWithIp_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Carrier("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Carrier(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Carrier_WhenCalledWithIp_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Carrier_WhenCalledWithIp_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Carrier("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Carrier(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Carrier_WhenCalledWithIp_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Carrier_WhenCalledWithIp_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Carrier("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Carrier(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Asn_WhenCalledWithIp_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Asn_WhenCalledWithIp_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Asn("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Asn(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Asn_WhenCalledWithIp_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Asn_WhenCalledWithIp_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Asn("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Asn(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Asn_WhenCalledWithIp_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Asn_WhenCalledWithIp_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Asn("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Asn(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Asn_WhenCalledWithIp_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Asn_WhenCalledWithIp_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Asn("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Asn(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task TimeZone_WhenCalledWithIp_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task TimeZone_WhenCalledWithIp_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.TimeZone("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.TimeZone(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task TimeZone_WhenCalledWithIp_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task TimeZone_WhenCalledWithIp_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.TimeZone("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.TimeZone(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task TimeZone_WhenCalledWithIp_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task TimeZone_WhenCalledWithIp_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.TimeZone("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.TimeZone(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task TimeZone_WhenCalledWithIp_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task TimeZone_WhenCalledWithIp_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.TimeZone("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.TimeZone(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Currency_WhenCalledWithIp_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Currency_WhenCalledWithIp_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Currency("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Currency(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Currency_WhenCalledWithIp_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Currency_WhenCalledWithIp_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Currency("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Currency(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Currency_WhenCalledWithIp_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Currency_WhenCalledWithIp_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Currency("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Currency(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Currency_WhenCalledWithIp_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Currency_WhenCalledWithIp_ShouldThrowApiException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Currency("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Currency(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Threat_WhenCalledWithIp_ShouldThrowBadRequestException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Threat_WhenCalledWithIp_ShouldThrowBadRequestException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.BadRequest)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Threat("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.BadRequest); + Func act = () => sut.Threat(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Threat_WhenCalledWithIp_ShouldThrowForbiddenException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Threat_WhenCalledWithIp_ShouldThrowForbiddenException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Forbidden)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Threat("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Forbidden); + Func act = () => sut.Threat(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Threat_WhenCalledWithIp_ShouldThrowUnauthorizedException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Threat_WhenCalledWithIp_ShouldThrowUnauthorizedException() { // Arrange - httpClient - .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.Unauthorized)); - - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Threat("1.1.1.1").ConfigureAwait(false); }; + var sut = SetupSutWithStatusCode(HttpStatusCode.Unauthorized); + Func act = () => sut.Threat(IPAddress.Any.ToString()); // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return act.Should().ThrowAsync(); } - [Theory, AutoMoqData] - public async Task Threat_WhenCalledWithIp_ShouldThrowApiException( - [Frozen] Mock httpClient, - string apiKey) + [Fact] + public Task Threat_WhenCalledWithIp_ShouldThrowApiException() { // Arrange + var sut = SetupSutWithStatusCode(HttpStatusCode.InternalServerError); + Func act = () => sut.Threat(IPAddress.Any.ToString()); + + // Act/Assert + return act.Should().ThrowAsync(); + } + + private static IpDataClient SetupSutWithStatusCode(HttpStatusCode statusCode) + { + var httpClient = new Mock(); + httpClient .Setup(x => x.SendAsync(It.IsAny())) - .ReturnsAsync(new HttpResponseMessage(0)); + .ReturnsAsync(new HttpResponseMessage(statusCode)); - var sut = new IpDataClient(apiKey, httpClient.Object); - Func act = async () => { await sut.Threat("1.1.1.1").ConfigureAwait(false); }; - - // Act/Assert - await act.Should() - .ThrowAsync() - .ConfigureAwait(false); + return new IpDataClient("apiKey", httpClient.Object); } } -} +} \ No newline at end of file diff --git a/test/Unit/IpData.Tests/IpInfo.json b/test/Unit/IpData.Tests/IpInfo.json new file mode 100644 index 0000000..8020b61 --- /dev/null +++ b/test/Unit/IpData.Tests/IpInfo.json @@ -0,0 +1,59 @@ +{ + "ip":"166.215.40.3", + "is_eu":false, + "city":null, + "region":null, + "region_code":null, + "country_name":"United States", + "country_code":"US", + "continent_name":"North America", + "continent_code":"NA", + "latitude":37.751, + "longitude":-97.822, + "postal":null, + "calling_code":"1", + "flag":"https://ipdata.co/flags/us.png", + "emoji_flag":"🇺🇸", + "emoji_unicode":"U+1F1FA U+1F1F8", + "asn":{ + "asn":"AS20057", + "name":"AT&T Mobility LLC", + "domain":"att.com", + "route":"166.215.0.0/17", + "type":"isp" + }, + "carrier":{ + "name":"AT&T", + "mcc":"310", + "mnc":"016" + }, + "languages":[ + { + "name":"English", + "native":"English" + } + ], + "currency":{ + "name":"US Dollar", + "code":"USD", + "symbol":"$", + "native":"$", + "plural":"US dollars" + }, + "time_zone":{ + "name":"America/Chicago", + "abbr":"CDT", + "offset":"-0500", + "is_dst":true, + "current_time":"2020-04-27T18:09:31.359643-05:00" + }, + "threat":{ + "is_tor":false, + "is_proxy":false, + "is_anonymous":false, + "is_known_attacker":false, + "is_known_abuser":false, + "is_threat":false, + "is_bogon":false + } +} \ No newline at end of file