-
Notifications
You must be signed in to change notification settings - Fork 184
/
FunctionsEndpointDataSourceTests.cs
164 lines (144 loc) · 5.48 KB
/
FunctionsEndpointDataSourceTests.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Routing;
using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata;
using Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.AspNetMiddleware;
using Xunit;
namespace Microsoft.Azure.Functions.Worker.Tests.AspNetCore
{
public class FunctionsEndpointDataSourceTests
{
[Theory]
[InlineData("api")]
[InlineData("customRoutePrefix")]
public void MapHttpFunction(string routePrefix)
{
string rawBinding = """
{
"name": "req",
"direction": "In",
"Type": "httpTrigger",
"authLevel": "Anonymous",
"methods": ["get", "post"],
"properties": { }
}
""";
var metadata = new DefaultFunctionMetadata
{
Name = "TestFunction",
RawBindings = new List<string> { rawBinding },
};
RouteEndpoint endpoint = FunctionsEndpointDataSource.MapHttpFunction(metadata, routePrefix) as RouteEndpoint;
Assert.Equal("TestFunction", endpoint.DisplayName);
Assert.Equal($"{routePrefix}/TestFunction", endpoint.RoutePattern.RawText);
var endpointMetadata = endpoint.Metadata.OfType<HttpMethodMetadata>().Single();
Assert.Equal(new[] { "GET", "POST" }, endpointMetadata.HttpMethods);
}
[Fact]
public void MapHttpFunction_CustomRoute()
{
string rawBinding = """
{
"name": "req",
"direction": "In",
"type": "httpTrigger",
"route": "customRoute/function",
"authLevel": "Anonymous",
"methods": ["get", "post"],
"properties": { }
}
""";
var metadata = new DefaultFunctionMetadata
{
Name = "TestFunction",
RawBindings = new List<string> { rawBinding },
};
RouteEndpoint endpoint = FunctionsEndpointDataSource.MapHttpFunction(metadata, "api") as RouteEndpoint;
Assert.Equal("TestFunction", endpoint.DisplayName);
Assert.Equal($"api/customRoute/function", endpoint.RoutePattern.RawText);
var endpointMetadata = endpoint.Metadata.OfType<HttpMethodMetadata>().Single();
Assert.Equal(new[] { "GET", "POST" }, endpointMetadata.HttpMethods);
}
[Fact]
public void MapHttpFunction_CustomRoute_CaseInsensitive()
{
string rawBinding = """
{
"name": "req",
"direction": "In",
"tyPe": "httpTrigger",
"rOute": "customRoute/function",
"authLevel": "Anonymous",
"metHOds": ["get", "post"],
"properties": { }
}
""";
var metadata = new DefaultFunctionMetadata
{
Name = "TestFunction",
RawBindings = new List<string> { rawBinding },
};
RouteEndpoint endpoint = FunctionsEndpointDataSource.MapHttpFunction(metadata, "api") as RouteEndpoint;
Assert.Equal("TestFunction", endpoint.DisplayName);
Assert.Equal($"api/customRoute/function", endpoint.RoutePattern.RawText);
var endpointMetadata = endpoint.Metadata.OfType<HttpMethodMetadata>().Single();
Assert.Equal(new[] { "GET", "POST" }, endpointMetadata.HttpMethods);
}
[Fact]
public void MapHttpFunction_CustomRoute_NoHttpMethodSpecified()
{
string rawBinding = """
{
"name": "req",
"direction": "In",
"type": "httpTrigger",
"route": "customRoute/function",
"authLevel": "Anonymous",
"properties": { }
}
""";
var metadata = new DefaultFunctionMetadata
{
Name = "TestFunction",
RawBindings = new List<string> { rawBinding },
};
RouteEndpoint endpoint = FunctionsEndpointDataSource.MapHttpFunction(metadata, "api") as RouteEndpoint;
Assert.Equal("TestFunction", endpoint.DisplayName);
Assert.Equal($"api/customRoute/function", endpoint.RoutePattern.RawText);
var endpointMetadata = endpoint.Metadata.OfType<HttpMethodMetadata>().Single();
Assert.Equal([], endpointMetadata.HttpMethods);
}
[Fact]
public void GetRoutePrefix()
{
string hostJson = """
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": "custom"
}
}
}
""";
string prefix = FunctionsEndpointDataSource.GetRoutePrefix(hostJson);
Assert.Equal("custom", prefix);
}
[Fact]
public void GetRoutePrefix_CaseInsensitive()
{
string hostJson = """
{
"version": "2.0",
"ExTEnsions": {
"hTtp": {
"rOUtepREfix": "custom"
}
}
}
""";
string prefix = FunctionsEndpointDataSource.GetRoutePrefix(hostJson);
Assert.Equal("custom", prefix);
}
}
}