67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
using System.Net;
|
|
using System.Text.Json;
|
|
using Asp.Versioning;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.TestHost;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Store.Users.Tests;
|
|
|
|
public sealed class VersionedOpenApiTests
|
|
{
|
|
[Fact]
|
|
public async Task MajorAndMinorVersions_GenerateSeparateDocuments()
|
|
{
|
|
var builder = WebApplication.CreateBuilder();
|
|
builder.WebHost.UseTestServer();
|
|
builder.Logging.ClearProviders();
|
|
builder.Services.AddControllers().AddApplicationPart(typeof(OpenApiMinorVersionTestController).Assembly);
|
|
builder.Services.AddApiVersioning(options =>
|
|
{
|
|
options.DefaultApiVersion = new ApiVersion(1, 0);
|
|
options.AssumeDefaultVersionWhenUnspecified = true;
|
|
options.ApiVersionReader = new UrlSegmentApiVersionReader();
|
|
}).AddMvc().AddApiExplorer(options =>
|
|
{
|
|
options.GroupNameFormat = "'v'VVV";
|
|
options.SubstituteApiVersionInUrl = true;
|
|
}).AddOpenApi();
|
|
|
|
await using var app = builder.Build();
|
|
app.MapControllers();
|
|
app.MapOpenApi().WithDocumentPerVersion();
|
|
await app.StartAsync();
|
|
using var client = app.GetTestClient();
|
|
|
|
var groups = app.DescribeApiVersions().Select(version => version.GroupName).ToArray();
|
|
Assert.Contains("v1", groups);
|
|
Assert.Contains("v1.1", groups);
|
|
Assert.Equal(groups.Length, groups.Distinct(StringComparer.OrdinalIgnoreCase).Count());
|
|
|
|
foreach (var group in new[] { "v1", "v1.1" })
|
|
{
|
|
using var response = await client.GetAsync($"/openapi/{group}.json");
|
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
|
using var document = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
|
|
var paths = document.RootElement.GetProperty("paths");
|
|
var ownPath = group == "v1" ? "/default-get-tests/Read" : "/openapi-minor-tests/Read";
|
|
Assert.True(paths.TryGetProperty(ownPath, out _));
|
|
}
|
|
}
|
|
}
|
|
|
|
[ApiController]
|
|
[ApiVersion("1.1")]
|
|
[Route("openapi-minor-tests/[action]")]
|
|
public sealed class OpenApiMinorVersionTestController : ControllerBase
|
|
{
|
|
[HttpPost]
|
|
public string Read()
|
|
{
|
|
return "ok";
|
|
}
|
|
}
|