Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/EmulatorPolicyChecklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ Track progress of emulator policy handler implementation. Each policy needs a ha
| ✅ | SetBody | SetBodyHandler.cs | `emulator/tests-set-body` |
| ⬜ | SetHeaderIfNotExist | SetHeaderIfNotExistHandler.cs | `emulator/tests-set-header-if-not-exist` |
| ✅ | SetMethod | SetMethodHandler.cs | `emulator/tests-set-method` |
| | SetQueryParameter | SetQueryParameterHandler.cs | `emulator/tests-set-query-parameter` |
| | SetQueryParameter | SetQueryParameterHandler.cs | `emulator/tests-set-query-parameter` |
| ⬜ | SetQueryParameterIfNotExist | SetQueryParameterIfNotExistHandler.cs | `emulator/tests-set-query-parameter-if-not-exist` |
| ✅ | SetVariable | SetVariableHandler.cs | `emulator/tests-set-variable` |
109 changes: 109 additions & 0 deletions test/Test.Testing/Emulator/Policies/SetQueryParameterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring;
using Microsoft.Azure.ApiManagement.PolicyToolkit.Testing;
using Microsoft.Azure.ApiManagement.PolicyToolkit.Testing.Document;

namespace Test.Emulator.Emulator.Policies;

[TestClass]
public class SetQueryParameterTests
{
class SimpleSetQueryParameter : IDocument
{
public void Inbound(IInboundContext context)
{
context.SetQueryParameter("param1", "value-1", "value-2");
}

public void Backend(IBackendContext context)
{
context.SetQueryParameter("backend-param", "b-value");
}

public void Outbound(IOutboundContext context)
{
context.SetQueryParameter("outbound-param", "o-value");
}

public void OnError(IOnErrorContext context)
{
context.SetQueryParameter("error-param", "e-value");
}
}

[TestMethod]
public void SetQueryParameter_Inbound()
{
var test = new SimpleSetQueryParameter().AsTestDocument();

test.RunInbound();

test.Context.Request.Url.Query.Should().ContainKey("param1")
.WhoseValue.Should().ContainInOrder("value-1", "value-2");
}

[TestMethod]
public void SetQueryParameter_Inbound_Overwrites()
{
var test = new TestDocument(new SimpleSetQueryParameter())
{
Context = { Request = { Url = { Query = { { "param1", ["old-value"] } } } } }
};

test.RunInbound();

test.Context.Request.Url.Query.Should().ContainKey("param1")
.WhoseValue.Should().ContainInOrder("value-1", "value-2");
}

[TestMethod]
public void SetQueryParameter_Backend()
{
var test = new SimpleSetQueryParameter().AsTestDocument();

test.RunBackend();

test.Context.Request.Url.Query.Should().ContainKey("backend-param")
.WhoseValue.Should().ContainInOrder("b-value");
}

[TestMethod]
public void SetQueryParameter_Outbound()
{
var test = new SimpleSetQueryParameter().AsTestDocument();

test.RunOutbound();

test.Context.Request.Url.Query.Should().ContainKey("outbound-param")
.WhoseValue.Should().ContainInOrder("o-value");
}

[TestMethod]
public void SetQueryParameter_OnError()
{
var test = new SimpleSetQueryParameter().AsTestDocument();

test.RunOnError();

test.Context.Request.Url.Query.Should().ContainKey("error-param")
.WhoseValue.Should().ContainInOrder("e-value");
}

[TestMethod]
public void SetQueryParameter_Callback()
{
var test = new SimpleSetQueryParameter().AsTestDocument();
var callbackExecuted = false;
test.SetupInbound().SetQueryParameter().WithCallback((_, _, _) =>
{
callbackExecuted = true;
});

test.RunInbound();

callbackExecuted.Should().BeTrue();
test.Context.Request.Url.Query.Should().NotContainKey("param1");
}
}
Loading