-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathApiAggregationServiceTests.cs
More file actions
173 lines (145 loc) · 7.18 KB
/
Copy pathApiAggregationServiceTests.cs
File metadata and controls
173 lines (145 loc) · 7.18 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
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
165
166
167
168
169
170
171
172
173
using System.Net;
using System.Text;
using AppGateway.ApiAggregation;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Primitives;
using SharedKernel.Configuration;
using Xunit;
using Yarp.ReverseProxy.Configuration;
namespace AppGateway.Tests;
public sealed class ApiAggregationServiceTests
{
private const string AccountOpenApiJson = """
{
"openapi": "3.0.0",
"info": { "title": "PlatformPlatform Account API", "version": "v1" },
"paths": {
"/api/account/users": { "get": { "responses": { "200": { "description": "OK" } } } },
"/internal-api/account/probe": { "get": { "responses": { "200": { "description": "OK" } } } }
}
}
""";
private const string MainOpenApiJson = """
{
"openapi": "3.0.0",
"info": { "title": "PlatformPlatform Main API", "version": "v1" },
"paths": {
"/api/main/health": { "get": { "responses": { "200": { "description": "OK" } } } }
}
}
""";
// Simulates a future endpoint that accidentally leaks into the account document with a
// /api/back-office prefix; the aggregator's belt-and-braces filter must drop it.
private const string AccountOpenApiJsonWithLeakedBackOfficePath = """
{
"openapi": "3.0.0",
"info": { "title": "PlatformPlatform Account API", "version": "v1" },
"paths": {
"/api/account/users": { "get": { "responses": { "200": { "description": "OK" } } } },
"/api/back-office/leaked": { "get": { "responses": { "200": { "description": "OK" } } } }
}
}
""";
[Fact]
public async Task GetAggregatedOpenApiJson_ShouldIncludeAccountApiPaths()
{
// Arrange
var service = CreateService(AccountOpenApiJson, MainOpenApiJson);
// Act
var aggregated = await service.GetAggregatedOpenApiJson();
// Assert
aggregated.Should().Contain("\"/api/account/users\"");
}
[Fact]
public async Task GetAggregatedOpenApiJson_ShouldIncludeMainApiPaths()
{
// Arrange
var service = CreateService(AccountOpenApiJson, MainOpenApiJson);
// Act
var aggregated = await service.GetAggregatedOpenApiJson();
// Assert
aggregated.Should().Contain("\"/api/main/health\"");
}
[Fact]
public async Task GetAggregatedOpenApiJson_ShouldExcludeBackOfficePaths()
{
// Arrange
var service = CreateService(AccountOpenApiJsonWithLeakedBackOfficePath, MainOpenApiJson);
// Act
var aggregated = await service.GetAggregatedOpenApiJson();
// Assert
aggregated.Should().NotContain("/api/back-office/");
aggregated.Should().Contain("\"/api/account/users\"");
}
[Fact]
public async Task GetAggregatedOpenApiJson_ShouldExcludeInternalApiPaths()
{
// Arrange
var service = CreateService(AccountOpenApiJson, MainOpenApiJson);
// Act
var aggregated = await service.GetAggregatedOpenApiJson();
// Assert
aggregated.Should().NotContain("/internal-api/");
}
private static ApiAggregationService CreateService(string accountDocumentJson, string mainDocumentJson)
{
var handler = new StubHttpMessageHandler(request =>
{
var requestUrl = request.RequestUri!.ToString();
if (requestUrl.EndsWith("/openapi/account.json", StringComparison.OrdinalIgnoreCase))
{
return BuildJsonResponse(accountDocumentJson);
}
if (requestUrl.EndsWith("/openapi/v1.json", StringComparison.OrdinalIgnoreCase))
{
return BuildJsonResponse(mainDocumentJson);
}
return new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = $"Unstubbed URL: {requestUrl}" };
}
);
var clusters = new ClusterConfig[]
{
new() { ClusterId = "account-api", Destinations = new Dictionary<string, DestinationConfig> { ["destination"] = new() { Address = "https://placeholder.invalid" } } },
new() { ClusterId = "main-api", Destinations = new Dictionary<string, DestinationConfig> { ["destination"] = new() { Address = "https://placeholder.invalid" } } }
};
var proxyConfigProvider = new StubProxyConfigProvider(new StubProxyConfig(clusters));
var httpClientFactory = new StubHttpClientFactory(handler);
var ports = new PortAllocation(9000);
return new ApiAggregationService(NullLogger<ApiAggregationService>.Instance, proxyConfigProvider, httpClientFactory, ports);
}
private static HttpResponseMessage BuildJsonResponse(string json)
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
}
private sealed class StubHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> responder) : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.FromResult(responder(request));
}
}
private sealed class StubHttpClientFactory(HttpMessageHandler handler) : IHttpClientFactory
{
public HttpClient CreateClient(string name)
{
return new HttpClient(handler, false);
}
}
private sealed class StubProxyConfigProvider(IProxyConfig config) : IProxyConfigProvider
{
public IProxyConfig GetConfig()
{
return config;
}
}
private sealed class StubProxyConfig(IReadOnlyList<ClusterConfig> clusters) : IProxyConfig
{
public IReadOnlyList<RouteConfig> Routes => Array.Empty<RouteConfig>();
public IReadOnlyList<ClusterConfig> Clusters => clusters;
public IChangeToken ChangeToken => new CancellationChangeToken(CancellationToken.None);
}
}