Skip to content

Commit

Permalink
Improved ApplicationBuilderMock
Browse files Browse the repository at this point in the history
  • Loading branch information
kalintsenkov committed Jul 9, 2023
1 parent 1d3ec72 commit 543a30c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ private void CheckForEndpointRouting(IServiceProvider serviceProvider)

private void ExtractEndpointRoutes(Func<RequestDelegate, RequestDelegate> 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;
}
Expand All @@ -157,7 +159,7 @@ private void ExtractEndpointRoutes(Func<RequestDelegate, RequestDelegate> middle
endpointDataSource
.Endpoints
.OfType<RouteEndpoint>()
.Where(route => route.Metadata.All(m =>
.Where(route => route.Metadata.All(m =>
Reflection.AreNotAssignable(typeof(IRouteTemplateProvider), m.GetType())))
.OrderBy(route => route.Order)
.ForEach(route =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static FieldInfo GetTargetField<T, TResult>(
string fieldName)
=> func
.Target
.GetType()
?.GetType()
.GetTypeInfo()
.DeclaredFields
.FirstOrDefault(m => m.Name == fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void WithHttpRequestShouldWorkCorrectlyWithDefaultValues()
.WithCookies(new
{
ObjectCookie = "ObjectCookieValue",
AnotherObjectCookie = "AnotherObjectCookieValue"
AnotherObjectCookie = "AnotherObjectCookieValue"
})
.AndAlso()
.WithFormField("Field", "FieldValue")
Expand Down Expand Up @@ -181,7 +181,7 @@ public void WithFormAsObjectShouldWorkCorrectly()
Assert.Equal("AnotherFieldValue", builtRequest.Form["AnotherField"]);
});
}

[Fact]
public void WithFormAsStringDictionaryShouldWorkCorrectly()
{
Expand Down Expand Up @@ -269,7 +269,7 @@ public void WithHeadersAsDictionaryShouldWorkCorrectly()
Assert.Equal("AnotherHeaderValue,SecondHeaderValue", builtRequest.Headers["AnotherHeader"]);
});
}

[Fact]
public void WithHeadersAsObjectDictionaryShouldWorkCorrectly()
{
Expand Down Expand Up @@ -560,7 +560,7 @@ public void WithStringBodyShouldWorkCorrectly()
Assert.Equal(ContentType.TextPlain, builtRequest.ContentType);
Assert.Equal(reader.BaseStream.Length, builtRequest.ContentLength);
}
});
});
}

[Fact]
Expand Down Expand Up @@ -708,7 +708,7 @@ public void WithLocationShouldWorkWithRelativeUri()
.WithHttpRequest(request => request.WithLocation("/Home/Index"))
.ShouldPassForThe<HttpRequest>(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);
Expand All @@ -724,7 +724,7 @@ public void WithLocationShouldWorkWithRelativeUriAndQueryString()
.WithHttpRequest(request => request.WithLocation("/Home/Index?test=text"))
.ShouldPassForThe<HttpRequest>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ShouldInvokeMethodOfTypeVoidWithValidServiceCollection()

testPlugin.DefaultServiceRegistrationDelegate(serviceCollection);

Assert.True(serviceCollection.Count == 165);
Assert.True(serviceCollection.Count == 166);
}
}
}

0 comments on commit 543a30c

Please sign in to comment.