2025-09-02 18:30:53 +02:00
|
|
|
|
using Bit.SharedWeb.Swagger;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
2026-02-10 11:11:44 -05:00
|
|
|
|
using Microsoft.OpenApi;
|
2025-09-02 18:30:53 +02:00
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SharedWeb.Test;
|
|
|
|
|
|
|
|
|
|
|
|
public class ActionNameOperationFilterTest
|
|
|
|
|
|
{
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void WithValidActionNameAddsActionNameExtensions()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2026-02-10 11:11:44 -05:00
|
|
|
|
var operation = new OpenApiOperation
|
|
|
|
|
|
{
|
|
|
|
|
|
Extensions = new Dictionary<string, IOpenApiExtension>()
|
|
|
|
|
|
};
|
2025-09-02 18:30:53 +02:00
|
|
|
|
var actionDescriptor = new ActionDescriptor();
|
|
|
|
|
|
actionDescriptor.RouteValues["action"] = "GetUsers";
|
|
|
|
|
|
|
|
|
|
|
|
var apiDescription = new ApiDescription
|
|
|
|
|
|
{
|
|
|
|
|
|
ActionDescriptor = actionDescriptor
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 11:11:44 -05:00
|
|
|
|
var context = new OperationFilterContext(apiDescription, null, null, null, null);
|
2025-09-02 18:30:53 +02:00
|
|
|
|
var filter = new ActionNameOperationFilter();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
filter.Apply(operation, context);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.True(operation.Extensions.ContainsKey("x-action-name"));
|
|
|
|
|
|
Assert.True(operation.Extensions.ContainsKey("x-action-name-snake-case"));
|
|
|
|
|
|
|
2026-02-10 11:11:44 -05:00
|
|
|
|
var actionNameExt = operation.Extensions["x-action-name"] as JsonNodeExtension;
|
|
|
|
|
|
var actionNameSnakeCaseExt = operation.Extensions["x-action-name-snake-case"] as JsonNodeExtension;
|
2025-09-02 18:30:53 +02:00
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(actionNameExt);
|
|
|
|
|
|
Assert.NotNull(actionNameSnakeCaseExt);
|
2026-02-10 11:11:44 -05:00
|
|
|
|
Assert.Equal("GetUsers", actionNameExt.Node.ToString());
|
|
|
|
|
|
Assert.Equal("get_users", actionNameSnakeCaseExt.Node.ToString());
|
2025-09-02 18:30:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
|
public void WithMissingActionRouteValueDoesNotAddExtensions()
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2026-02-10 11:11:44 -05:00
|
|
|
|
var operation = new OpenApiOperation
|
|
|
|
|
|
{
|
|
|
|
|
|
Extensions = new Dictionary<string, IOpenApiExtension>()
|
|
|
|
|
|
};
|
2025-09-02 18:30:53 +02:00
|
|
|
|
var actionDescriptor = new ActionDescriptor();
|
|
|
|
|
|
// Not setting the "action" route value at all
|
|
|
|
|
|
|
|
|
|
|
|
var apiDescription = new ApiDescription
|
|
|
|
|
|
{
|
|
|
|
|
|
ActionDescriptor = actionDescriptor
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-02-10 11:11:44 -05:00
|
|
|
|
var context = new OperationFilterContext(apiDescription, null, null, null, null);
|
2025-09-02 18:30:53 +02:00
|
|
|
|
var filter = new ActionNameOperationFilter();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
filter.Apply(operation, context);
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.False(operation.Extensions.ContainsKey("x-action-name"));
|
|
|
|
|
|
Assert.False(operation.Extensions.ContainsKey("x-action-name-snake-case"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|