diff --git a/Source/Mockolate/Web/HttpClientExtensions.Setup.Delete.cs b/Source/Mockolate/Web/HttpClientExtensions.Setup.Delete.cs index 59e8eb8f..fc7de5e4 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Setup.Delete.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Setup.Delete.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Setup; @@ -43,28 +44,19 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? requestUri, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Delete, new HttpStringUriParameter(requestUri))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Delete, - new HttpStringUriParameter(requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -76,28 +68,19 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? requestUri, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Delete, new HttpRequestMessageParameter(r => r.RequestUri, requestUri))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Delete, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Setup.Get.cs b/Source/Mockolate/Web/HttpClientExtensions.Setup.Get.cs index 58df8d43..e66c16ba 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Setup.Get.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Setup.Get.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Setup; @@ -43,28 +44,19 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? requestUri, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Get, new HttpStringUriParameter(requestUri))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Get, - new HttpStringUriParameter(requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -76,28 +68,19 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? requestUri, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Get, new HttpRequestMessageParameter(r => r.RequestUri, requestUri))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Get, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Setup.Patch.cs b/Source/Mockolate/Web/HttpClientExtensions.Setup.Patch.cs index e04b04da..2d6f7b11 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Setup.Patch.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Setup.Patch.cs @@ -3,6 +3,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Setup; @@ -47,9 +48,8 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? content, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", @@ -57,20 +57,11 @@ httpClientMock.ConstructorParameters[0] is IMockSubject http new HttpStringUriParameter(requestUri), new HttpRequestMessageParameter(r => r.Content, content))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, - new HttpStringUriParameter(requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -83,9 +74,8 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? content, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", @@ -93,20 +83,11 @@ httpClientMock.ConstructorParameters[0] is IMockSubject http new HttpRequestMessageParameter(r => r.RequestUri, requestUri), new HttpRequestMessageParameter(r => r.Content, content))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Setup.Post.cs b/Source/Mockolate/Web/HttpClientExtensions.Setup.Post.cs index 15d9e432..d726a6c0 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Setup.Post.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Setup.Post.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Setup; @@ -46,9 +47,8 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? content, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", @@ -56,20 +56,11 @@ httpClientMock.ConstructorParameters[0] is IMockSubject http new HttpStringUriParameter(requestUri), new HttpRequestMessageParameter(r => r.Content, content))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Post, - new HttpStringUriParameter(requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -82,9 +73,8 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? content, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", @@ -92,20 +82,11 @@ httpClientMock.ConstructorParameters[0] is IMockSubject http new HttpRequestMessageParameter(r => r.RequestUri, requestUri), new HttpRequestMessageParameter(r => r.Content, content))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Post, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Setup.Put.cs b/Source/Mockolate/Web/HttpClientExtensions.Setup.Put.cs index bb478c47..99658d0b 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Setup.Put.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Setup.Put.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Setup; @@ -46,9 +47,8 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? content, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", @@ -56,20 +56,11 @@ httpClientMock.ConstructorParameters[0] is IMockSubject http new HttpStringUriParameter(requestUri), new HttpRequestMessageParameter(r => r.Content, content))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Put, - new HttpStringUriParameter(requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -82,9 +73,8 @@ public IReturnMethodSetup, HttpRequestMessage, Cancell IParameter? content, IParameter cancellationToken) { - if (setup is Mock httpClientMock && - httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock && - httpMessageHandlerMock.Mock is IMockMethodSetup httpMessageHandlerSetup) + if (setup is Mock { ConstructorParameters.Length: > 0, } httpClientMock && + httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = new("System.Net.Http.HttpMessageHandler.SendAsync", @@ -92,20 +82,11 @@ httpClientMock.ConstructorParameters[0] is IMockSubject http new HttpRequestMessageParameter(r => r.RequestUri, requestUri), new HttpRequestMessageParameter(r => r.Content, content))), new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(httpMessageHandlerSetup).SetupMethod(methodSetup); - return methodSetup; - } - else - { - ReturnMethodSetup, HttpRequestMessage, CancellationToken> methodSetup = - new("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Put, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); - CastToMockRegistrationOrThrow(setup).SetupMethod(methodSetup); + httpMessageHandlerMock.Mock.Registrations.SetupMethod(methodSetup); return methodSetup; } + + throw new MockException("Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Verify.Delete.cs b/Source/Mockolate/Web/HttpClientExtensions.Verify.Delete.cs index 1adcabeb..ff57907b 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Verify.Delete.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Verify.Delete.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Verify; @@ -42,7 +43,7 @@ public VerificationResult DeleteAsync( IParameter? requestUri, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -52,10 +53,7 @@ public VerificationResult DeleteAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Delete, - new HttpStringUriParameter(requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -67,7 +65,7 @@ public VerificationResult DeleteAsync( IParameter? requestUri, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -77,10 +75,7 @@ public VerificationResult DeleteAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Delete, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Verify.Get.cs b/Source/Mockolate/Web/HttpClientExtensions.Verify.Get.cs index 7cf18cfa..f3e06baa 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Verify.Get.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Verify.Get.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Verify; @@ -42,7 +43,7 @@ public VerificationResult GetAsync( IParameter? requestUri, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -52,10 +53,7 @@ public VerificationResult GetAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Get, - new HttpStringUriParameter(requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -67,7 +65,7 @@ public VerificationResult GetAsync( IParameter? requestUri, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -77,10 +75,7 @@ public VerificationResult GetAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Get, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Verify.Patch.cs b/Source/Mockolate/Web/HttpClientExtensions.Verify.Patch.cs index 8ae6595f..6560eddf 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Verify.Patch.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Verify.Patch.cs @@ -2,6 +2,7 @@ using System; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Verify; @@ -46,7 +47,7 @@ public VerificationResult PatchAsync( IParameter? content, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -57,11 +58,7 @@ public VerificationResult PatchAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, - new HttpStringUriParameter(requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -74,7 +71,7 @@ public VerificationResult PatchAsync( IParameter? content, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -85,11 +82,7 @@ public VerificationResult PatchAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Patch, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Verify.Post.cs b/Source/Mockolate/Web/HttpClientExtensions.Verify.Post.cs index d382d535..3ff4343c 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Verify.Post.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Verify.Post.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Verify; @@ -45,7 +46,7 @@ public VerificationResult PostAsync( IParameter? content, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -56,11 +57,7 @@ public VerificationResult PostAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Post, - new HttpStringUriParameter(requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -73,7 +70,7 @@ public VerificationResult PostAsync( IParameter? content, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -84,11 +81,7 @@ public VerificationResult PostAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Post, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.Verify.Put.cs b/Source/Mockolate/Web/HttpClientExtensions.Verify.Put.cs index 37d07efa..27b130ce 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.Verify.Put.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.Verify.Put.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Parameters; using Mockolate.Verify; @@ -45,7 +46,7 @@ public VerificationResult PutAsync( IParameter? content, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -56,11 +57,7 @@ public VerificationResult PutAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Put, - new HttpStringUriParameter(requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } /// @@ -73,7 +70,7 @@ public VerificationResult PutAsync( IParameter? content, IParameter cancellationToken) { - if (verifyInvoked is Mock httpClientMock && + if (verifyInvoked is Mock { ConstructorParameters.Length: > 0, } httpClientMock && httpClientMock.ConstructorParameters[0] is IMockSubject httpMessageHandlerMock) { return httpMessageHandlerMock.Mock.Method("System.Net.Http.HttpMessageHandler.SendAsync", @@ -84,11 +81,7 @@ public VerificationResult PutAsync( .Map(httpClientMock.Subject); } - return CastToMockOrThrow(verifyInvoked).Method("System.Net.Http.HttpMessageInvoker.SendAsync", - new NamedParameter("request", new HttpRequestMessageParameters(HttpMethod.Put, - new HttpRequestMessageParameter(r => r.RequestUri, requestUri), - new HttpRequestMessageParameter(r => r.Content, content))), - new NamedParameter("cancellationToken", (IParameter)cancellationToken)); + throw new MockException("Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); } } } diff --git a/Source/Mockolate/Web/HttpClientExtensions.cs b/Source/Mockolate/Web/HttpClientExtensions.cs index f672de51..77e32995 100644 --- a/Source/Mockolate/Web/HttpClientExtensions.cs +++ b/Source/Mockolate/Web/HttpClientExtensions.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Net.Http; -using Mockolate.Exceptions; using Mockolate.Parameters; #if NETSTANDARD2_0 using Mockolate.Internals.Polyfills; @@ -15,33 +13,11 @@ namespace Mockolate.Web; /// public static partial class HttpClientExtensions { - private static MockRegistration CastToMockRegistrationOrThrow(IInteractiveMock subject) - { - if (subject is IHasMockRegistration mock) - { - return mock.Registrations; - } - - throw new MockException("The subject is no mock."); - } - - private static Mock CastToMockOrThrow(IInteractiveMock subject) - { - if (subject is Mock mock) - { - return mock; - } - - throw new MockException("The subject is no mock."); - } - private sealed class HttpRequestMessageParameters( HttpMethod method, params IHttpRequestMessageParameter[] parameters) - : IParameter, IParameter + : IParameter { - private List>? _callbacks; - public bool Matches(object? value) { if (value is HttpRequestMessage requestMessage && @@ -55,40 +31,14 @@ public bool Matches(object? value) public void InvokeCallbacks(object? value) { - if (TryCast(value, out HttpRequestMessage typedValue)) + if (value is HttpRequestMessage typedValue) { - _callbacks?.ForEach(a => a.Invoke(typedValue)); foreach (IHttpRequestMessageParameter parameter in parameters) { parameter.InvokeCallbacks(typedValue); } } } - - public IParameter Do(Action callback) - { - _callbacks ??= []; - _callbacks.Add(callback); - return this; - } - - private static bool TryCast(object? value, out T typedValue) - { - if (value is T castValue) - { - typedValue = castValue; - return true; - } - - if (value is null && default(T) is null) - { - typedValue = default!; - return true; - } - - typedValue = default!; - return false; - } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.DeleteTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.DeleteTests.cs index ee7b8dcd..e55ecee3 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.DeleteTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.DeleteTests.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -69,6 +70,23 @@ await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .DeleteAsync(It.Matches("*aweXpect.com*")) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData(nameof(HttpMethod.Delete), true)] [InlineData(nameof(HttpMethod.Get), false)] @@ -126,6 +144,23 @@ public async Task Uri_WithCancellationToken_ShouldVerifyCancellationToken(bool t await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .DeleteAsync(It.IsUri("*aweXpect.com*")) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.GetTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.GetTests.cs index 449646fb..650bf122 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.GetTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.GetTests.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -69,6 +70,23 @@ await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .GetAsync(It.Matches("*aweXpect.com*")) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData(nameof(HttpMethod.Delete), false)] [InlineData(nameof(HttpMethod.Get), true)] @@ -126,6 +144,23 @@ public async Task Uri_WithCancellationToken_ShouldVerifyCancellationToken(bool t await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .GetAsync(It.IsUri("*aweXpect.com*")) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PatchTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PatchTests.cs index c5003551..6100c5f7 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PatchTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PatchTests.cs @@ -3,6 +3,7 @@ using System.Net.Http; using System.Text; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -94,6 +95,23 @@ await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .PatchAsync(It.Matches("*aweXpect.com*"), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData("application/json", true)] [InlineData("text/plain", false)] @@ -174,6 +192,23 @@ public async Task Uri_WithCancellationToken_ShouldVerifyCancellationToken(bool t await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .PatchAsync(It.IsUri("*aweXpect.com*"), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PostTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PostTests.cs index b3b78ac2..90262f66 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PostTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PostTests.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Text; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -92,6 +93,23 @@ await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .PostAsync(It.Matches("*aweXpect.com*"), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData("application/json", true)] [InlineData("text/plain", false)] @@ -171,6 +189,23 @@ public async Task Uri_WithCancellationToken_ShouldVerifyCancellationToken(bool t await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .PostAsync(It.IsUri("*aweXpect.com*"), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PutTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PutTests.cs index ebdd3a24..be99e82c 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PutTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Setup.PutTests.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Text; using System.Threading; +using Mockolate.Exceptions; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -92,6 +93,23 @@ await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .PutAsync(It.Matches("*aweXpect.com*"), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData("application/json", true)] [InlineData("text/plain", false)] @@ -171,6 +189,23 @@ public async Task Uri_WithCancellationToken_ShouldVerifyCancellationToken(bool t await That(result.StatusCode) .IsEqualTo(tokenMatches ? HttpStatusCode.OK : HttpStatusCode.NotImplemented); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.SetupMock.Method + .PutAsync(It.IsUri("*aweXpect.com*"), It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + } + + await That(Act).Throws() + .WithMessage( + "Cannot setup HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.DeleteTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.DeleteTests.cs index 4c02a8a0..08839f7b 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.DeleteTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.DeleteTests.cs @@ -1,5 +1,7 @@ using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; +using Mockolate.Verify; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -60,6 +62,22 @@ await That(httpClient.VerifyMock.Invoked.DeleteAsync( .Exactly(tokenMatches ? 1 : 0); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .DeleteAsync(It.Matches("*aweXpect.com*")).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData(nameof(HttpMethod.Delete), 1)] [InlineData(nameof(HttpMethod.Get), 0)] @@ -108,6 +126,22 @@ await That(httpClient.VerifyMock.Invoked.DeleteAsync( It.Satisfies(_ => tokenMatches))) .Exactly(tokenMatches ? 1 : 0); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .DeleteAsync(It.IsUri("*aweXpect.com*")).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.GetTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.GetTests.cs index e9aeba3f..e1148114 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.GetTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.GetTests.cs @@ -1,5 +1,7 @@ using System.Net.Http; using System.Threading; +using Mockolate.Exceptions; +using Mockolate.Verify; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -60,6 +62,22 @@ await That(httpClient.VerifyMock.Invoked.GetAsync( .Exactly(tokenMatches ? 1 : 0); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .GetAsync(It.Matches("*aweXpect.com*")).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData(nameof(HttpMethod.Delete), 0)] [InlineData(nameof(HttpMethod.Get), 1)] @@ -108,6 +126,22 @@ await That(httpClient.VerifyMock.Invoked.GetAsync( It.Satisfies(_ => tokenMatches))) .Exactly(tokenMatches ? 1 : 0); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .GetAsync(It.IsUri("*aweXpect.com*")).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PatchTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PatchTests.cs index 6e02e1d7..764f0459 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PatchTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PatchTests.cs @@ -2,6 +2,8 @@ using System.Net.Http; using System.Text; using System.Threading; +using Mockolate.Exceptions; +using Mockolate.Verify; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -84,6 +86,22 @@ await That(httpClient.VerifyMock.Invoked.PatchAsync( .Exactly(tokenMatches ? 1 : 0); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .PatchAsync(It.Matches("*aweXpect.com*"), It.IsAny()).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData("application/json", 1)] [InlineData("text/plain", 0)] @@ -154,6 +172,22 @@ await That(httpClient.VerifyMock.Invoked.PatchAsync( It.Satisfies(_ => tokenMatches))) .Exactly(tokenMatches ? 1 : 0); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .PatchAsync(It.IsUri("*aweXpect.com*"), It.IsAny()).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PostTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PostTests.cs index 81bad973..0a869b9a 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PostTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PostTests.cs @@ -1,6 +1,8 @@ using System.Net.Http; using System.Text; using System.Threading; +using Mockolate.Exceptions; +using Mockolate.Verify; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -82,6 +84,22 @@ await That(httpClient.VerifyMock.Invoked.PostAsync( .Exactly(tokenMatches ? 1 : 0); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .PostAsync(It.Matches("*aweXpect.com*"), It.IsAny()).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData("application/json", 1)] [InlineData("text/plain", 0)] @@ -151,6 +169,22 @@ await That(httpClient.VerifyMock.Invoked.PostAsync( It.Satisfies(_ => tokenMatches))) .Exactly(tokenMatches ? 1 : 0); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .PostAsync(It.IsUri("*aweXpect.com*"), It.IsAny()).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PutTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PutTests.cs index 625b1369..c0fcdbd2 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PutTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.Verify.PutTests.cs @@ -1,6 +1,8 @@ using System.Net.Http; using System.Text; using System.Threading; +using Mockolate.Exceptions; +using Mockolate.Verify; using Mockolate.Web; namespace Mockolate.Tests.Web; @@ -82,6 +84,22 @@ await That(httpClient.VerifyMock.Invoked.PutAsync( .Exactly(tokenMatches ? 1 : 0); } + [Fact] + public async Task StringUri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .PutAsync(It.Matches("*aweXpect.com*"), It.IsAny()).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } + [Theory] [InlineData("application/json", 1)] [InlineData("text/plain", 0)] @@ -151,6 +169,22 @@ await That(httpClient.VerifyMock.Invoked.PutAsync( It.Satisfies(_ => tokenMatches))) .Exactly(tokenMatches ? 1 : 0); } + + [Fact] + public async Task Uri_WithoutMockedHttpMessageHandler_ShouldThrowMockException() + { + HttpClient httpClient = Mock.Create(BaseClass.WithConstructorParameters()); + + void Act() + { + httpClient.VerifyMock.Invoked + .PutAsync(It.IsUri("*aweXpect.com*"), It.IsAny()).Never(); + } + + await That(Act).Throws() + .WithMessage( + "Cannot verify HttpClient when it is not mocked with a mockable HttpMessageHandler."); + } } } } diff --git a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.cs b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.cs index 42f8226a..539667ae 100644 --- a/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.cs +++ b/Tests/Mockolate.Tests/Web/HttpClientExtensionsTests.cs @@ -1,3 +1,50 @@ +using System.Net; +using System.Net.Http; +using System.Threading; +using Mockolate.Interactions; +using Mockolate.Parameters; +using Mockolate.Setup; +using Mockolate.Web; + namespace Mockolate.Tests.Web; -public sealed partial class HttpClientExtensionsTests; +public sealed partial class HttpClientExtensionsTests +{ + [Fact] + public async Task InvalidParameter_ShouldReturnTrue() + { + HttpClient httpClient = Mock.Create(); + IReturnMethodSetup, HttpRequestMessage, CancellationToken> setup = httpClient + .SetupMock.Method + .GetAsync(new InvalidParameter()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + IInteractiveMethodSetup interactiveSetup = (IInteractiveMethodSetup)setup; + + bool result = interactiveSetup.Matches(new MethodInvocation(0, "System.Net.Http.HttpMessageHandler.SendAsync", + [new HttpRequestMessage(), CancellationToken.None,])); + + await That(result).IsTrue(); + } + + [Fact] + public async Task NullUri_ShouldReturnFalse() + { + HttpClient httpClient = Mock.Create(); + IReturnMethodSetup, HttpRequestMessage, CancellationToken> setup = httpClient + .SetupMock.Method + .GetAsync(It.IsAny()) + .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK)); + IInteractiveMethodSetup interactiveSetup = (IInteractiveMethodSetup)setup; + + bool result = interactiveSetup.Matches(new MethodInvocation(0, "System.Net.Http.HttpMessageHandler.SendAsync", + [new HttpRequestMessage(), CancellationToken.None,])); + + await That(result).IsFalse(); + } + + private sealed class InvalidParameter : IParameter + { + public IParameter Do(Action callback) + => throw new NotSupportedException(); + } +}