From 543a30cdbc56dc0bd9e2ba80b8f8ab9985080169 Mon Sep 17 00:00:00 2001 From: Kalin Tsenkov Date: Sun, 9 Jul 2023 11:58:19 +0300 Subject: [PATCH] Improved ApplicationBuilderMock --- .../Internal/Application/ApplicationBuilderMock.cs | 10 ++++++---- .../Utilities/Extensions/FuncExtensions.cs | 2 +- .../HttpTests/HttpRequestBuilderTests.cs | 12 ++++++------ .../PluginsTests/ViewDataTestPluginTests.cs | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/ApplicationBuilderMock.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/ApplicationBuilderMock.cs index 4e8d363a1..a10d4893c 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/ApplicationBuilderMock.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Internal/Application/ApplicationBuilderMock.cs @@ -128,10 +128,12 @@ private void CheckForEndpointRouting(IServiceProvider serviceProvider) private void ExtractEndpointRoutes(Func middleware) { - var middlewareTypeField = middleware.GetTargetField("middleware"); + var stateTypeField = middleware.GetTargetField("state"); + var stateType = stateTypeField?.GetValue(middleware.Target); - if (!(middlewareTypeField?.GetValue(middleware.Target) is Type middlewareType) - || middlewareType.Name != "EndpointMiddleware") + var middlewareTypeProperty = stateType?.GetType().GetProperty("Middleware"); + + if (middlewareTypeProperty?.GetValue(stateType) is not Type { Name: "EndpointMiddleware" }) { return; } @@ -157,7 +159,7 @@ private void ExtractEndpointRoutes(Func middle endpointDataSource .Endpoints .OfType() - .Where(route => route.Metadata.All(m => + .Where(route => route.Metadata.All(m => Reflection.AreNotAssignable(typeof(IRouteTemplateProvider), m.GetType()))) .OrderBy(route => route.Order) .ForEach(route => diff --git a/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Extensions/FuncExtensions.cs b/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Extensions/FuncExtensions.cs index a4626b396..cd54037d9 100644 --- a/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Extensions/FuncExtensions.cs +++ b/src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Extensions/FuncExtensions.cs @@ -11,7 +11,7 @@ public static FieldInfo GetTargetField( string fieldName) => func .Target - .GetType() + ?.GetType() .GetTypeInfo() .DeclaredFields .FirstOrDefault(m => m.Name == fieldName); diff --git a/test/MyTested.AspNetCore.Mvc.Http.Test/BuildersTests/HttpTests/HttpRequestBuilderTests.cs b/test/MyTested.AspNetCore.Mvc.Http.Test/BuildersTests/HttpTests/HttpRequestBuilderTests.cs index daff9ebca..cb79999d5 100644 --- a/test/MyTested.AspNetCore.Mvc.Http.Test/BuildersTests/HttpTests/HttpRequestBuilderTests.cs +++ b/test/MyTested.AspNetCore.Mvc.Http.Test/BuildersTests/HttpTests/HttpRequestBuilderTests.cs @@ -47,7 +47,7 @@ public void WithHttpRequestShouldWorkCorrectlyWithDefaultValues() .WithCookies(new { ObjectCookie = "ObjectCookieValue", - AnotherObjectCookie = "AnotherObjectCookieValue" + AnotherObjectCookie = "AnotherObjectCookieValue" }) .AndAlso() .WithFormField("Field", "FieldValue") @@ -181,7 +181,7 @@ public void WithFormAsObjectShouldWorkCorrectly() Assert.Equal("AnotherFieldValue", builtRequest.Form["AnotherField"]); }); } - + [Fact] public void WithFormAsStringDictionaryShouldWorkCorrectly() { @@ -269,7 +269,7 @@ public void WithHeadersAsDictionaryShouldWorkCorrectly() Assert.Equal("AnotherHeaderValue,SecondHeaderValue", builtRequest.Headers["AnotherHeader"]); }); } - + [Fact] public void WithHeadersAsObjectDictionaryShouldWorkCorrectly() { @@ -560,7 +560,7 @@ public void WithStringBodyShouldWorkCorrectly() Assert.Equal(ContentType.TextPlain, builtRequest.ContentType); Assert.Equal(reader.BaseStream.Length, builtRequest.ContentLength); } - }); + }); } [Fact] @@ -708,7 +708,7 @@ public void WithLocationShouldWorkWithRelativeUri() .WithHttpRequest(request => request.WithLocation("/Home/Index")) .ShouldPassForThe(builtRequest => { - Assert.Null(builtRequest.Host.Value); + Assert.Equal(string.Empty, builtRequest.Host.Value); Assert.Equal("/Home/Index", builtRequest.PathBase); Assert.Equal("http", builtRequest.Scheme); Assert.Equal("/Home/Index", builtRequest.Path); @@ -724,7 +724,7 @@ public void WithLocationShouldWorkWithRelativeUriAndQueryString() .WithHttpRequest(request => request.WithLocation("/Home/Index?test=text")) .ShouldPassForThe(builtRequest => { - Assert.Null(builtRequest.Host.Value); + Assert.Equal(string.Empty, builtRequest.Host.Value); Assert.Equal("/Home/Index", builtRequest.PathBase); Assert.Equal("http", builtRequest.Scheme); Assert.Equal("/Home/Index", builtRequest.Path); diff --git a/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs b/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs index 2bf1f7f26..f43851e95 100644 --- a/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs +++ b/test/MyTested.AspNetCore.Mvc.ViewData.Test/PluginsTests/ViewDataTestPluginTests.cs @@ -33,7 +33,7 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection() testPlugin.DefaultServiceRegistrationDelegate(serviceCollection); - Assert.True(serviceCollection.Count == 165); + Assert.True(serviceCollection.Count == 166); } } }