-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Xml.Linq; | ||
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
using Mielek.Azure.ApiManagement.PolicyToolkit.Authoring; | ||
|
||
namespace Mielek.Azure.ApiManagement.PolicyToolkit.Compilation.Policy; | ||
|
||
public class JsonPCompiler : IMethodPolicyHandler | ||
{ | ||
public string MethodName => nameof(IOutboundContext.JsonP); | ||
|
||
public void Handle(ICompilationContext context, InvocationExpressionSyntax node) | ||
{ | ||
var arguments = node.ArgumentList.Arguments; | ||
if (arguments.Count != 1) | ||
{ | ||
context.ReportError($"Wrong argument count for jsonp policy. {node.GetLocation()}"); | ||
return; | ||
} | ||
|
||
var value = node.ArgumentList.Arguments[0].Expression.ProcessParameter(context); | ||
context.AddPolicy(new XElement("jsonp", new XAttribute("callback-parameter-name", value))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace Mielek.Azure.ApiManagement.PolicyToolkit.Compilation; | ||
|
||
[TestClass] | ||
public class JsonPTests | ||
{ | ||
[TestMethod] | ||
[DataRow( | ||
""" | ||
[Document] | ||
public class PolicyDocument : IDocument | ||
{ | ||
public void Outbound(IOutboundContext context) { | ||
context.JsonP("cb"); | ||
} | ||
} | ||
""", | ||
""" | ||
<policies> | ||
<outbound> | ||
<jsonp callback-parameter-name="cb" /> | ||
</outbound> | ||
</policies> | ||
""", | ||
DisplayName = "Should compile jsonp policy" | ||
)] | ||
[DataRow( | ||
""" | ||
[Document] | ||
public class PolicyDocument : IDocument | ||
{ | ||
public void Outbound(IOutboundContext context) { | ||
context.JsonP(Exp(context.ExpressionContext)); | ||
} | ||
public string Exp(IExpressionContext context) => context.User.Name; | ||
} | ||
""", | ||
""" | ||
<policies> | ||
<outbound> | ||
<jsonp callback-parameter-name="@(context.User.Name)" /> | ||
</outbound> | ||
</policies> | ||
""", | ||
DisplayName = "Should compile jsonp policy with expression" | ||
)] | ||
public void ShouldCompileJsonPPolicy(string code, string expectedXml) | ||
{ | ||
code.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml); | ||
} | ||
|
||
} |