|
| 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.ValidationsGenerator.Tests; |
| 5 | + |
| 6 | +public partial class ValidationsGeneratorTests : ValidationsGeneratorTestBase |
| 7 | +{ |
| 8 | + [Fact] |
| 9 | + public async Task CanValidateMultipleNamespaces() |
| 10 | + { |
| 11 | + // Arrange |
| 12 | + var source = """ |
| 13 | +using System; |
| 14 | +using System.ComponentModel.DataAnnotations; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using Microsoft.AspNetCore.Builder; |
| 18 | +using Microsoft.AspNetCore.Http; |
| 19 | +using Microsoft.AspNetCore.Http.Validation; |
| 20 | +using Microsoft.AspNetCore.Routing; |
| 21 | +using Microsoft.Extensions.DependencyInjection; |
| 22 | +
|
| 23 | +var builder = WebApplication.CreateBuilder(); |
| 24 | +
|
| 25 | +builder.Services.AddValidation(); |
| 26 | +
|
| 27 | +var app = builder.Build(); |
| 28 | +
|
| 29 | +app.MapPost("/namespace-one", (NamespaceOne.Type obj) => Results.Ok("Passed")); |
| 30 | +app.MapPost("/namespace-two", (NamespaceTwo.Type obj) => Results.Ok("Passed")); |
| 31 | +
|
| 32 | +app.Run(); |
| 33 | +
|
| 34 | +namespace NamespaceOne { |
| 35 | + public class Type |
| 36 | + { |
| 37 | + [StringLength(10)] |
| 38 | + public string StringWithLength { get; set; } = string.Empty; |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +namespace NamespaceTwo { |
| 43 | + public class Type |
| 44 | + { |
| 45 | + [StringLength(20)] |
| 46 | + public string StringWithLength { get; set; } = string.Empty; |
| 47 | + } |
| 48 | +} |
| 49 | +"""; |
| 50 | + await Verify(source, out var compilation); |
| 51 | + await VerifyEndpoint(compilation, "/namespace-one", async (endpoint, serviceProvider) => |
| 52 | + { |
| 53 | + await InvalidStringWithLengthProducesError(endpoint); |
| 54 | + await ValidInputProducesNoWarnings(endpoint); |
| 55 | + |
| 56 | + async Task InvalidStringWithLengthProducesError(Endpoint endpoint) |
| 57 | + { |
| 58 | + var payload = """ |
| 59 | + { |
| 60 | + "StringWithLength": "abcdefghijk" |
| 61 | + } |
| 62 | + """; |
| 63 | + var context = CreateHttpContextWithPayload(payload, serviceProvider); |
| 64 | + |
| 65 | + await endpoint.RequestDelegate(context); |
| 66 | + |
| 67 | + var problemDetails = await AssertBadRequest(context); |
| 68 | + Assert.Collection(problemDetails.Errors, kvp => |
| 69 | + { |
| 70 | + Assert.Equal("StringWithLength", kvp.Key); |
| 71 | + Assert.Equal("The field StringWithLength must be a string with a maximum length of 10.", kvp.Value.Single()); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + async Task ValidInputProducesNoWarnings(Endpoint endpoint) |
| 76 | + { |
| 77 | + var payload = """ |
| 78 | + { |
| 79 | + "StringWithLength": "abc" |
| 80 | + } |
| 81 | + """; |
| 82 | + var context = CreateHttpContextWithPayload(payload, serviceProvider); |
| 83 | + await endpoint.RequestDelegate(context); |
| 84 | + |
| 85 | + Assert.Equal(200, context.Response.StatusCode); |
| 86 | + } |
| 87 | + }); |
| 88 | + await VerifyEndpoint(compilation, "/namespace-two", async (endpoint, serviceProvider) => |
| 89 | + { |
| 90 | + await InvalidStringWithLengthProducesError(endpoint); |
| 91 | + await ValidInputProducesNoWarnings(endpoint); |
| 92 | + |
| 93 | + async Task InvalidStringWithLengthProducesError(Endpoint endpoint) |
| 94 | + { |
| 95 | + var payload = """ |
| 96 | + { |
| 97 | + "StringWithLength": "abcdefghijklmnopqrstu" |
| 98 | + } |
| 99 | + """; |
| 100 | + var context = CreateHttpContextWithPayload(payload, serviceProvider); |
| 101 | + |
| 102 | + await endpoint.RequestDelegate(context); |
| 103 | + |
| 104 | + var problemDetails = await AssertBadRequest(context); |
| 105 | + Assert.Collection(problemDetails.Errors, kvp => |
| 106 | + { |
| 107 | + Assert.Equal("StringWithLength", kvp.Key); |
| 108 | + Assert.Equal("The field StringWithLength must be a string with a maximum length of 20.", kvp.Value.Single()); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + async Task ValidInputProducesNoWarnings(Endpoint endpoint) |
| 113 | + { |
| 114 | + var payload = """ |
| 115 | + { |
| 116 | + "StringWithLength": "abcdefghijk" |
| 117 | + } |
| 118 | + """; |
| 119 | + var context = CreateHttpContextWithPayload(payload, serviceProvider); |
| 120 | + await endpoint.RequestDelegate(context); |
| 121 | + |
| 122 | + Assert.Equal(200, context.Response.StatusCode); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | +} |
0 commit comments