|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +namespace Microsoft.AspNetCore.Http.HttpResults; |
| 5 | + |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.AspNetCore.Builder; |
| 9 | +using Microsoft.AspNetCore.Http.Metadata; |
| 10 | +using Microsoft.AspNetCore.Mvc; |
| 11 | +using Microsoft.AspNetCore.Routing; |
| 12 | +using Microsoft.AspNetCore.Routing.Patterns; |
| 13 | +using Microsoft.Extensions.DependencyInjection; |
| 14 | +using Microsoft.Extensions.Logging; |
| 15 | +using Microsoft.Extensions.Logging.Abstractions; |
| 16 | +using Newtonsoft.Json.Linq; |
| 17 | + |
| 18 | +public class InternalServerErrorOfTResultTests |
| 19 | +{ |
| 20 | + [Fact] |
| 21 | + public void InternalServerErrorObjectResult_SetsStatusCodeAndValue() |
| 22 | + { |
| 23 | + // Arrange & Act |
| 24 | + var obj = new object(); |
| 25 | + var internalServerErrorObjectResult = new InternalServerError<object>(obj); |
| 26 | + |
| 27 | + // Assert |
| 28 | + Assert.Equal(StatusCodes.Status500InternalServerError, internalServerErrorObjectResult.StatusCode); |
| 29 | + Assert.Equal(obj, internalServerErrorObjectResult.Value); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void InternalServerErrorObjectResult_ProblemDetails_SetsStatusCodeAndValue() |
| 34 | + { |
| 35 | + // Arrange & Act |
| 36 | + var obj = new HttpValidationProblemDetails(); |
| 37 | + var result = new InternalServerError<HttpValidationProblemDetails>(obj); |
| 38 | + |
| 39 | + // Assert |
| 40 | + Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode); |
| 41 | + Assert.Equal(StatusCodes.Status500InternalServerError, obj.Status); |
| 42 | + Assert.Equal(obj, result.Value); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task InternalServerErrorObjectResult_ExecuteAsync_SetsStatusCode() |
| 47 | + { |
| 48 | + // Arrange |
| 49 | + var result = new InternalServerError<string>("Hello"); |
| 50 | + var httpContext = new DefaultHttpContext() |
| 51 | + { |
| 52 | + RequestServices = CreateServices(), |
| 53 | + }; |
| 54 | + |
| 55 | + // Act |
| 56 | + await result.ExecuteAsync(httpContext); |
| 57 | + |
| 58 | + // Assert |
| 59 | + Assert.Equal(StatusCodes.Status500InternalServerError, httpContext.Response.StatusCode); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task InternalServerErrorObjectResult_ExecuteResultAsync_FormatsData() |
| 64 | + { |
| 65 | + // Arrange |
| 66 | + var result = new InternalServerError<string>("Hello"); |
| 67 | + var stream = new MemoryStream(); |
| 68 | + var httpContext = new DefaultHttpContext() |
| 69 | + { |
| 70 | + RequestServices = CreateServices(), |
| 71 | + Response = |
| 72 | + { |
| 73 | + Body = stream, |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + // Act |
| 78 | + await result.ExecuteAsync(httpContext); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assert.Equal("\"Hello\"", Encoding.UTF8.GetString(stream.ToArray())); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task InternalServerErrorObjectResult_ExecuteResultAsync_UsesStatusCodeFromResultTypeForProblemDetails() |
| 86 | + { |
| 87 | + // Arrange |
| 88 | + var details = new ProblemDetails { Status = StatusCodes.Status422UnprocessableEntity, }; |
| 89 | + var result = new InternalServerError<ProblemDetails>(details); |
| 90 | + |
| 91 | + var httpContext = new DefaultHttpContext() |
| 92 | + { |
| 93 | + RequestServices = CreateServices(), |
| 94 | + }; |
| 95 | + |
| 96 | + // Act |
| 97 | + await result.ExecuteAsync(httpContext); |
| 98 | + |
| 99 | + // Assert |
| 100 | + Assert.Equal(StatusCodes.Status422UnprocessableEntity, details.Status.Value); |
| 101 | + Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode); |
| 102 | + Assert.Equal(StatusCodes.Status500InternalServerError, httpContext.Response.StatusCode); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void PopulateMetadata_AddsResponseTypeMetadata() |
| 107 | + { |
| 108 | + // Arrange |
| 109 | + InternalServerError<Todo> MyApi() { throw new NotImplementedException(); } |
| 110 | + var metadata = new List<object>(); |
| 111 | + var builder = new RouteEndpointBuilder(requestDelegate: null, RoutePatternFactory.Parse("/"), order: 0); |
| 112 | + |
| 113 | + // Act |
| 114 | + PopulateMetadata<InternalServerError<Todo>>(((Delegate)MyApi).GetMethodInfo(), builder); |
| 115 | + |
| 116 | + // Assert |
| 117 | + var producesResponseTypeMetadata = builder.Metadata.OfType<ProducesResponseTypeMetadata>().Last(); |
| 118 | + Assert.Equal(StatusCodes.Status500InternalServerError, producesResponseTypeMetadata.StatusCode); |
| 119 | + Assert.Equal(typeof(Todo), producesResponseTypeMetadata.Type); |
| 120 | + Assert.Single(producesResponseTypeMetadata.ContentTypes, "application/json"); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void ExecuteAsync_ThrowsArgumentNullException_WhenHttpContextIsNull() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + var result = new InternalServerError<object>(null); |
| 128 | + HttpContext httpContext = null; |
| 129 | + |
| 130 | + // Act & Assert |
| 131 | + Assert.ThrowsAsync<ArgumentNullException>("httpContext", () => result.ExecuteAsync(httpContext)); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public void PopulateMetadata_ThrowsArgumentNullException_WhenMethodOrBuilderAreNull() |
| 136 | + { |
| 137 | + // Act & Assert |
| 138 | + Assert.Throws<ArgumentNullException>("method", () => PopulateMetadata<InternalServerError<object>>(null, new RouteEndpointBuilder(requestDelegate: null, RoutePatternFactory.Parse("/"), order: 0))); |
| 139 | + Assert.Throws<ArgumentNullException>("builder", () => PopulateMetadata<InternalServerError<object>>(((Delegate)PopulateMetadata_ThrowsArgumentNullException_WhenMethodOrBuilderAreNull).GetMethodInfo(), null)); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void InternalServerErrorObjectResult_Implements_IStatusCodeHttpResult_Correctly() |
| 144 | + { |
| 145 | + // Act & Assert |
| 146 | + var result = Assert.IsAssignableFrom<IStatusCodeHttpResult>(new InternalServerError<string>(null)); |
| 147 | + Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode); |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public void InternalServerErrorObjectResult_Implements_IValueHttpResult_Correctly() |
| 152 | + { |
| 153 | + // Arrange |
| 154 | + var value = "Foo"; |
| 155 | + |
| 156 | + // Act & Assert |
| 157 | + var result = Assert.IsAssignableFrom<IValueHttpResult>(new InternalServerError<string>(value)); |
| 158 | + Assert.IsType<string>(result.Value); |
| 159 | + Assert.Equal(value, result.Value); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void InternalServerErrorObjectResult_Implements_IValueHttpResultOfT_Correctly() |
| 164 | + { |
| 165 | + // Arrange & Act |
| 166 | + var value = "Foo"; |
| 167 | + |
| 168 | + // Assert |
| 169 | + var result = Assert.IsAssignableFrom<IValueHttpResult<string>>(new InternalServerError<string>(value)); |
| 170 | + Assert.IsType<string>(result.Value); |
| 171 | + Assert.Equal(value, result.Value); |
| 172 | + } |
| 173 | + |
| 174 | + private static void PopulateMetadata<TResult>(MethodInfo method, EndpointBuilder builder) |
| 175 | + where TResult : IEndpointMetadataProvider => TResult.PopulateMetadata(method, builder); |
| 176 | + |
| 177 | + private record Todo(int Id, string Title); |
| 178 | + |
| 179 | + private static IServiceProvider CreateServices() |
| 180 | + { |
| 181 | + var services = new ServiceCollection(); |
| 182 | + services.AddSingleton<ILoggerFactory, NullLoggerFactory>(); |
| 183 | + return services.BuildServiceProvider(); |
| 184 | + } |
| 185 | +} |
0 commit comments