-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathApiDescriptionExtensionsTests.cs
More file actions
108 lines (94 loc) · 3.62 KB
/
ApiDescriptionExtensionsTests.cs
File metadata and controls
108 lines (94 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net.Http;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Routing.Patterns;
public class ApiDescriptionExtensionsTests
{
[Theory]
[InlineData("api/todos", "/api/todos")]
[InlineData("api/todos/{id}", "/api/todos/{id}")]
[InlineData("api/todos/{id:int:min(10)}", "/api/todos/{id}")]
[InlineData("{a}/{b}/{c=19}", "/{a}/{b}/{c}")]
[InlineData("{a}/{b}/{c?}", "/{a}/{b}/{c}")]
[InlineData("{a:int}/{b}/{c:int}", "/{a}/{b}/{c}")]
[InlineData("", "/")]
[InlineData("api", "/api")]
[InlineData("{p1}/{p2}.{p3?}", "/{p1}/{p2}.{p3}")]
public void MapRelativePathToItemPath_ReturnsItemPathForApiDescription(string relativePath, string expectedItemPath)
{
// Arrange
var apiDescription = new ApiDescription
{
RelativePath = relativePath
};
// Act
var itemPath = apiDescription.MapRelativePathToItemPath();
// Assert
Assert.Equal(expectedItemPath, itemPath);
}
[Theory]
[InlineData("/~health", "~health", "/~health")]
[InlineData("/~api/todos", "~api/todos", "/~api/todos")]
[InlineData("/~api/todos/{id}", "~api/todos/{id}", "/~api/todos/{id}")]
[InlineData("~/health", "health", "/health")]
[InlineData("~/api/todos", "api/todos", "/api/todos")]
[InlineData("~/api/todos/{id}", "api/todos/{id}", "/api/todos/{id}")]
public void MapRelativePathToItemPath_WithRoutePattern_HandlesRoutesThatStartWithTilde(string rawPattern, string relativePath, string expectedItemPath)
{
// Arrange
var routePattern = RoutePatternFactory.Parse(rawPattern);
var apiDescription = new ApiDescription
{
RelativePath = relativePath,
RoutePattern = routePattern
};
// Act
var itemPath = apiDescription.MapRelativePathToItemPath();
// Assert
Assert.Equal(expectedItemPath, itemPath);
}
public static class HttpMethodTestData
{
public static IEnumerable<object[]> TestCases => new List<object[]>
{
new object[] { "GET", HttpMethod.Get },
new object[] { "POST", HttpMethod.Post },
new object[] { "PUT", HttpMethod.Put },
new object[] { "DELETE", HttpMethod.Delete },
new object[] { "PATCH", HttpMethod.Patch },
new object[] { "HEAD", HttpMethod.Head },
new object[] { "OPTIONS", HttpMethod.Options },
new object[] { "TRACE", HttpMethod.Trace },
new object[] { "QUERY", HttpMethod.Query },
new object[] { "gEt", HttpMethod.Get }, // Test case-insensitivity
};
}
[Theory]
[MemberData(nameof(HttpMethodTestData.TestCases), MemberType = typeof(HttpMethodTestData))]
public void GetHttpMethod_ReturnsHttpMethodForApiDescription(string httpMethod, HttpMethod expectedHttpMethod)
{
// Arrange
var apiDescription = new ApiDescription
{
HttpMethod = httpMethod
};
// Act
var result = apiDescription.GetHttpMethod();
// Assert
Assert.Equal(expectedHttpMethod, result);
}
[Fact]
public void GetHttpMethod_ReturnsNullForUnsupportedMethod()
{
// Arrange - Test that unsupported HTTP methods return null
var apiDescription = new ApiDescription
{
HttpMethod = "UNSUPPORTED"
};
// Act
var result = apiDescription.GetHttpMethod();
// Assert
Assert.Null(result);
}
}