Skip to content

Commit e6fc3d7

Browse files
authored
feat: Scaffold policies emulator (#51)
1 parent d5dadbc commit e6fc3d7

File tree

108 files changed

+3561
-158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+3561
-158
lines changed

example/test/ApiOperationPolicyTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void TestInboundExternalIp()
7474
Request = { IpAddress = "11.0.0.1" }
7575
}
7676
};
77-
testDocument.InInbound().AuthenticationManagedIdentity().ReturnsToken("myToken");
77+
testDocument.SetupInbound().AuthenticationManagedIdentity().ReturnsToken("myToken");
7878

7979
testDocument.RunInbound();
8080

src/Core/Compiling/CompilerUtils.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Azure.ApiManagement.PolicyToolkit.Compiling.Diagnostics;
88

99
using Microsoft.CodeAnalysis;
10-
using Microsoft.CodeAnalysis.CSharp;
1110
using Microsoft.CodeAnalysis.CSharp.Syntax;
1211

1312
namespace Azure.ApiManagement.PolicyToolkit.Compiling;
@@ -20,27 +19,27 @@ public static string ProcessParameter(this ExpressionSyntax expression, ICompila
2019
{
2120
case LiteralExpressionSyntax syntax:
2221
return syntax.Token.ValueText;
23-
case InterpolatedStringExpressionSyntax syntax:
24-
var interpolationParts = syntax.Contents.Select(c => c switch
25-
{
26-
InterpolatedStringTextSyntax text => text.TextToken.ValueText,
27-
InterpolationSyntax interpolation =>
28-
$"{{context.Variables[\"{interpolation.Expression.ToString()}\"]}}",
29-
_ => ""
30-
});
31-
var interpolationExpression = CSharpSyntaxTree
32-
.ParseText($"context => $\"{string.Join("", interpolationParts)}\"").GetRoot();
33-
var lambda = interpolationExpression.DescendantNodesAndSelf().OfType<LambdaExpressionSyntax>()
34-
.FirstOrDefault();
35-
lambda = Normalize(lambda!);
36-
return $"@({lambda.ExpressionBody})";
3722
case InvocationExpressionSyntax syntax:
3823
return FindCode(syntax, context);
24+
// case InterpolatedStringExpressionSyntax syntax:
25+
// var interpolationParts = syntax.Contents.Select(c => c switch
26+
// {
27+
// InterpolatedStringTextSyntax text => text.TextToken.ValueText,
28+
// InterpolationSyntax interpolation =>
29+
// $"{{context.Variables[\"{interpolation.Expression.ToString()}\"]}}",
30+
// _ => ""
31+
// });
32+
// var interpolationExpression = CSharpSyntaxTree
33+
// .ParseText($"context => $\"{string.Join("", interpolationParts)}\"").GetRoot();
34+
// var lambda = interpolationExpression.DescendantNodesAndSelf().OfType<LambdaExpressionSyntax>()
35+
// .FirstOrDefault();
36+
// lambda = Normalize(lambda!);
37+
// return $"@({lambda.ExpressionBody})";
3938
default:
4039
context.Report(Diagnostic.Create(
4140
CompilationErrors.NotSupportedParameter,
4241
expression.GetLocation()
43-
));
42+
));
4443
return "";
4544
}
4645
}
@@ -52,7 +51,7 @@ public static string FindCode(this InvocationExpressionSyntax syntax, ICompilati
5251
context.Report(Diagnostic.Create(
5352
CompilationErrors.InvalidExpression,
5453
syntax.GetLocation()
55-
));
54+
));
5655
return "";
5756
}
5857

@@ -97,7 +96,7 @@ public static InitializerValue Process(
9796
context.Report(Diagnostic.Create(
9897
CompilationErrors.ObjectInitializerContainsNotAnAssigmentExpression,
9998
expression.GetLocation()
100-
));
99+
));
101100
continue;
102101
}
103102

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockAppendHeaderProvider
10+
{
11+
public static Setup AppendHeader(this MockPoliciesProvider<IInboundContext> mock) =>
12+
AppendHeader(mock, (_, _, _) => true);
13+
14+
public static Setup AppendHeader(this MockPoliciesProvider<IOutboundContext> mock) =>
15+
AppendHeader(mock, (_, _, _) => true);
16+
17+
public static Setup AppendHeader(this MockPoliciesProvider<IOnErrorContext> mock) =>
18+
AppendHeader(mock, (_, _, _) => true);
19+
20+
public static Setup AppendHeader(
21+
this MockPoliciesProvider<IInboundContext> mock,
22+
Func<GatewayContext, string, string[], bool> predicate
23+
) => AppendHeader<IInboundContext, AppendHeaderRequestHandler>(mock, predicate);
24+
25+
public static Setup AppendHeader(
26+
this MockPoliciesProvider<IOutboundContext> mock,
27+
Func<GatewayContext, string, string[], bool> predicate
28+
) => AppendHeader<IOutboundContext, AppendHeaderResponseHandler>(mock, predicate);
29+
30+
public static Setup AppendHeader(this MockPoliciesProvider<IOnErrorContext> mock,
31+
Func<GatewayContext, string, string[], bool> predicate
32+
) => AppendHeader<IOnErrorContext, AppendHeaderResponseHandler>(mock, predicate);
33+
34+
private static Setup AppendHeader<TContext, THandler>(
35+
MockPoliciesProvider<TContext> mock,
36+
Func<GatewayContext, string, string[], bool> predicate
37+
)
38+
where TContext : class
39+
where THandler : AppendHeaderHandler
40+
{
41+
var handler = mock.SectionContextProxy.GetHandler<THandler>();
42+
return new Setup(predicate, handler);
43+
}
44+
45+
public class Setup
46+
{
47+
private readonly Func<GatewayContext, string, string[], bool> _predicate;
48+
private readonly AppendHeaderHandler _handler;
49+
50+
internal Setup(
51+
Func<GatewayContext, string, string[], bool> predicate,
52+
AppendHeaderHandler handler)
53+
{
54+
_predicate = predicate;
55+
_handler = handler;
56+
}
57+
58+
public void WithCallback(Action<GatewayContext, string, string[]> callback) =>
59+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
60+
}
61+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockAppendQueryParameterProvider
10+
{
11+
public static Setup AppendQueryParameter(this MockPoliciesProvider<IInboundContext> mock) =>
12+
AppendQueryParameter(mock, (_, _, _) => true);
13+
14+
public static Setup AppendQueryParameter(
15+
this MockPoliciesProvider<IInboundContext> mock,
16+
Func<GatewayContext, string, string[], bool> predicate
17+
)
18+
{
19+
var handler = mock.SectionContextProxy.GetHandler<AppendQueryParameterHandler>();
20+
return new Setup(predicate, handler);
21+
}
22+
23+
public class Setup
24+
{
25+
private readonly Func<GatewayContext, string, string[], bool> _predicate;
26+
private readonly AppendQueryParameterHandler _handler;
27+
28+
internal Setup(
29+
Func<GatewayContext, string, string[], bool> predicate,
30+
AppendQueryParameterHandler handler)
31+
{
32+
_predicate = predicate;
33+
_handler = handler;
34+
}
35+
36+
public void WithCallback(Action<GatewayContext, string, string[]> callback) =>
37+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
38+
}
39+
}

src/Testing/Document/MockAuthenticationBasicProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using Azure.ApiManagement.PolicyToolkit.Authoring;
5-
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator;
65
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
76

87
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
@@ -47,8 +46,6 @@ internal Setup(
4746
}
4847

4948
public void WithCallback(Action<GatewayContext, string, string> callback) =>
50-
_handler.CallbackHooks.Add(
51-
new Tuple<Func<GatewayContext, string, string, bool>, Action<GatewayContext, string, string>>(
52-
_predicate, callback));
49+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
5350
}
5451
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockAuthenticationCertificateProvider
10+
{
11+
public static Setup AuthenticationCertificate(this MockPoliciesProvider<IInboundContext> mock) =>
12+
AuthenticationCertificate(mock, (_, _) => true);
13+
14+
public static Setup AuthenticationCertificate(
15+
this MockPoliciesProvider<IInboundContext> mock,
16+
Func<GatewayContext, CertificateAuthenticationConfig, bool> predicate)
17+
{
18+
var handler = mock.SectionContextProxy.GetHandler<AuthenticationCertificateHandler>();
19+
return new Setup(predicate, handler);
20+
}
21+
22+
public class Setup
23+
{
24+
private readonly Func<GatewayContext, CertificateAuthenticationConfig, bool> _predicate;
25+
private readonly AuthenticationCertificateHandler _handler;
26+
27+
internal Setup(
28+
Func<GatewayContext, CertificateAuthenticationConfig, bool> predicate,
29+
AuthenticationCertificateHandler handler)
30+
{
31+
_predicate = predicate;
32+
_handler = handler;
33+
}
34+
35+
public void WithCallback(Action<GatewayContext, CertificateAuthenticationConfig> callback) =>
36+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
37+
}
38+
}

src/Testing/Document/MockAuthenticationManagedIdentityProvider.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using Azure.ApiManagement.PolicyToolkit.Authoring;
5-
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator;
65
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
76

87
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
@@ -34,17 +33,14 @@ internal Setup(
3433
}
3534

3635
public void WithCallback(Action<GatewayContext, ManagedIdentityAuthenticationConfig> callback) =>
37-
_handler.CallbackHooks.Add(
38-
new Tuple<Func<GatewayContext, ManagedIdentityAuthenticationConfig, bool>,
39-
Action<GatewayContext, ManagedIdentityAuthenticationConfig>>(_predicate, callback));
36+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
4037

41-
public void WithTokenProviderHook(Func<string, string?, string> hook) => _handler.ProvideTokenHooks.Add(
42-
new Tuple<Func<GatewayContext, ManagedIdentityAuthenticationConfig, bool>, Func<string, string?, string>>(
43-
_predicate, hook));
38+
public void WithTokenProviderHook(Func<string, string?, string> hook) =>
39+
_handler.ProvideTokenHooks.Add((_predicate, hook).ToTuple());
4440

4541
public void ReturnsToken(string token) => this.WithTokenProviderHook((_, _) => token);
4642

4743
public void WithError(string error) =>
48-
this.WithTokenProviderHook((_, _) => throw new InvalidOperationException(error));
44+
this.WithTokenProviderHook((_, _) => throw new HttpRequestException(error));
4945
}
5046
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockAzureOpenAiEmitTokenMetricProvider
10+
{
11+
public static Setup AzureOpenAiEmitTokenMetric(
12+
this MockPoliciesProvider<IInboundContext> mock) => AzureOpenAiEmitTokenMetric(mock, (_, _) => true);
13+
14+
public static Setup AzureOpenAiEmitTokenMetric(
15+
this MockPoliciesProvider<IInboundContext> mock,
16+
Func<GatewayContext, EmitTokenMetricConfig, bool> predicate)
17+
{
18+
var handler = mock.SectionContextProxy.GetHandler<AzureOpenAiEmitTokenMetricHandler>();
19+
return new Setup(predicate, handler);
20+
}
21+
22+
public class Setup
23+
{
24+
private readonly Func<GatewayContext, EmitTokenMetricConfig, bool> _predicate;
25+
private readonly AzureOpenAiEmitTokenMetricHandler _handler;
26+
27+
internal Setup(
28+
Func<GatewayContext, EmitTokenMetricConfig, bool> predicate,
29+
AzureOpenAiEmitTokenMetricHandler handler)
30+
{
31+
_predicate = predicate;
32+
_handler = handler;
33+
}
34+
35+
public void WithCallback(Action<GatewayContext, EmitTokenMetricConfig> callback) =>
36+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockAzureOpenAiSemanticCacheLookupProvider
10+
{
11+
public static Setup AzureOpenAiSemanticCacheLookup(
12+
this MockPoliciesProvider<IInboundContext> mock) => AzureOpenAiSemanticCacheLookup(mock, (_, _) => true);
13+
14+
public static Setup AzureOpenAiSemanticCacheLookup(
15+
this MockPoliciesProvider<IInboundContext> mock,
16+
Func<GatewayContext, SemanticCacheLookupConfig, bool> predicate)
17+
{
18+
var handler = mock.SectionContextProxy.GetHandler<AzureOpenAiSemanticCacheLookupHandler>();
19+
return new Setup(predicate, handler);
20+
}
21+
22+
public class Setup
23+
{
24+
private readonly Func<GatewayContext, SemanticCacheLookupConfig, bool> _predicate;
25+
private readonly AzureOpenAiSemanticCacheLookupHandler _handler;
26+
27+
internal Setup(
28+
Func<GatewayContext, SemanticCacheLookupConfig, bool> predicate,
29+
AzureOpenAiSemanticCacheLookupHandler handler)
30+
{
31+
_predicate = predicate;
32+
_handler = handler;
33+
}
34+
35+
public void WithCallback(Action<GatewayContext, SemanticCacheLookupConfig> callback) =>
36+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Azure.ApiManagement.PolicyToolkit.Authoring;
5+
using Azure.ApiManagement.PolicyToolkit.Testing.Emulator.Policies;
6+
7+
namespace Azure.ApiManagement.PolicyToolkit.Testing.Document;
8+
9+
public static class MockAzureOpenAiSemanticCacheStoreProvider
10+
{
11+
public static Setup AzureOpenAiSemanticCacheStore(
12+
this MockPoliciesProvider<IOutboundContext> mock) => AzureOpenAiSemanticCacheStore(mock, (_, _) => true);
13+
14+
public static Setup AzureOpenAiSemanticCacheStore(
15+
this MockPoliciesProvider<IOutboundContext> mock,
16+
Func<GatewayContext, uint, bool> predicate)
17+
{
18+
var handler = mock.SectionContextProxy.GetHandler<AzureOpenAiSemanticCacheStoreHandler>();
19+
return new Setup(predicate, handler);
20+
}
21+
22+
public class Setup
23+
{
24+
private readonly Func<GatewayContext, uint, bool> _predicate;
25+
private readonly AzureOpenAiSemanticCacheStoreHandler _handler;
26+
27+
internal Setup(
28+
Func<GatewayContext, uint, bool> predicate,
29+
AzureOpenAiSemanticCacheStoreHandler handler)
30+
{
31+
_predicate = predicate;
32+
_handler = handler;
33+
}
34+
35+
public void WithCallback(Action<GatewayContext, uint> callback) =>
36+
_handler.CallbackSetup.Add((_predicate, callback).ToTuple());
37+
}
38+
}

0 commit comments

Comments
 (0)