diff options
Diffstat (limited to 'src/Http/Http.Results/test/AcceptedAtRouteResultTests.cs')
-rw-r--r-- | src/Http/Http.Results/test/AcceptedAtRouteResultTests.cs | 141 |
1 files changed, 70 insertions, 71 deletions
diff --git a/src/Http/Http.Results/test/AcceptedAtRouteResultTests.cs b/src/Http/Http.Results/test/AcceptedAtRouteResultTests.cs index c2f1e31339..f9626d7df5 100644 --- a/src/Http/Http.Results/test/AcceptedAtRouteResultTests.cs +++ b/src/Http/Http.Results/test/AcceptedAtRouteResultTests.cs @@ -12,43 +12,43 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using Xunit; -namespace Microsoft.AspNetCore.Http.Result +namespace Microsoft.AspNetCore.Http.Result; + +public class AcceptedAtRouteResultTests { - public class AcceptedAtRouteResultTests + [Fact] + public async Task ExecuteResultAsync_FormatsData() { - [Fact] - public async Task ExecuteResultAsync_FormatsData() - { - // Arrange - var url = "testAction"; - var linkGenerator = new TestLinkGenerator { Url = url }; - var httpContext = GetHttpContext(linkGenerator); - var stream = new MemoryStream(); - httpContext.Response.Body = stream; + // Arrange + var url = "testAction"; + var linkGenerator = new TestLinkGenerator { Url = url }; + var httpContext = GetHttpContext(linkGenerator); + var stream = new MemoryStream(); + httpContext.Response.Body = stream; - var routeValues = new RouteValueDictionary(new Dictionary<string, string>() + var routeValues = new RouteValueDictionary(new Dictionary<string, string>() { { "test", "case" }, { "sample", "route" } }); - // Act - var result = new AcceptedAtRouteResult( - routeName: "sample", - routeValues: routeValues, - value: "Hello world"); - await result.ExecuteAsync(httpContext); + // Act + var result = new AcceptedAtRouteResult( + routeName: "sample", + routeValues: routeValues, + value: "Hello world"); + await result.ExecuteAsync(httpContext); - // Assert - var response = Encoding.UTF8.GetString(stream.ToArray()); - Assert.Equal("\"Hello world\"", response); - } + // Assert + var response = Encoding.UTF8.GetString(stream.ToArray()); + Assert.Equal("\"Hello world\"", response); + } - public static TheoryData<object> AcceptedAtRouteData + public static TheoryData<object> AcceptedAtRouteData + { + get { - get - { - return new TheoryData<object> + return new TheoryData<object> { null, new Dictionary<string, string>() @@ -62,59 +62,58 @@ namespace Microsoft.AspNetCore.Http.Result { "sample", "route" } }), }; - } } + } - [Theory] - [MemberData(nameof(AcceptedAtRouteData))] - public async Task ExecuteResultAsync_SetsStatusCodeAndLocationHeader(object values) - { - // Arrange - var expectedUrl = "testAction"; - var linkGenerator = new TestLinkGenerator { Url = expectedUrl }; - var httpContext = GetHttpContext(linkGenerator); + [Theory] + [MemberData(nameof(AcceptedAtRouteData))] + public async Task ExecuteResultAsync_SetsStatusCodeAndLocationHeader(object values) + { + // Arrange + var expectedUrl = "testAction"; + var linkGenerator = new TestLinkGenerator { Url = expectedUrl }; + var httpContext = GetHttpContext(linkGenerator); - // Act - var result = new AcceptedAtRouteResult(routeValues: values, value: null); - await result.ExecuteAsync(httpContext); + // Act + var result = new AcceptedAtRouteResult(routeValues: values, value: null); + await result.ExecuteAsync(httpContext); - // Assert - Assert.Equal(StatusCodes.Status202Accepted, httpContext.Response.StatusCode); - Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]); - } + // Assert + Assert.Equal(StatusCodes.Status202Accepted, httpContext.Response.StatusCode); + Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]); + } - [Fact] - public async Task ExecuteResultAsync_ThrowsIfRouteUrlIsNull() - { - // Arrange - var linkGenerator = new TestLinkGenerator(); - var httpContext = GetHttpContext(linkGenerator); + [Fact] + public async Task ExecuteResultAsync_ThrowsIfRouteUrlIsNull() + { + // Arrange + var linkGenerator = new TestLinkGenerator(); + var httpContext = GetHttpContext(linkGenerator); - // Act - var result = new AcceptedAtRouteResult( - routeName: null, - routeValues: new Dictionary<string, object>(), - value: null); + // Act + var result = new AcceptedAtRouteResult( + routeName: null, + routeValues: new Dictionary<string, object>(), + value: null); - // Assert - await ExceptionAssert.ThrowsAsync<InvalidOperationException>(() => - result.ExecuteAsync(httpContext), - "No route matches the supplied values."); - } + // Assert + await ExceptionAssert.ThrowsAsync<InvalidOperationException>(() => + result.ExecuteAsync(httpContext), + "No route matches the supplied values."); + } - private static HttpContext GetHttpContext(LinkGenerator linkGenerator) - { - var httpContext = new DefaultHttpContext(); - httpContext.RequestServices = CreateServices(linkGenerator); - return httpContext; - } + private static HttpContext GetHttpContext(LinkGenerator linkGenerator) + { + var httpContext = new DefaultHttpContext(); + httpContext.RequestServices = CreateServices(linkGenerator); + return httpContext; + } - private static IServiceProvider CreateServices(LinkGenerator linkGenerator) - { - var services = new ServiceCollection(); - services.AddLogging(); - services.AddSingleton(linkGenerator); - return services.BuildServiceProvider(); - } + private static IServiceProvider CreateServices(LinkGenerator linkGenerator) + { + var services = new ServiceCollection(); + services.AddLogging(); + services.AddSingleton(linkGenerator); + return services.BuildServiceProvider(); } } |