From 9acf2179247db378996447e6286fada8f5fdc9c2 Mon Sep 17 00:00:00 2001 From: Rafal Mielowski Date: Wed, 25 Feb 2026 16:25:38 +0100 Subject: [PATCH] Add emulator tests for SetQueryParameter - Add SetQueryParameterTests with 6 test cases - Tests cover all 4 sections, overwrite behavior, and callback override - Sections: Inbound, Backend, Outbound, OnError Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- docs/EmulatorPolicyChecklist.md | 2 +- .../Policies/SetQueryParameterTests.cs | 109 ++++++++++++++++++ 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 test/Test.Testing/Emulator/Policies/SetQueryParameterTests.cs diff --git a/docs/EmulatorPolicyChecklist.md b/docs/EmulatorPolicyChecklist.md index f9faf9cd..a7efff59 100644 --- a/docs/EmulatorPolicyChecklist.md +++ b/docs/EmulatorPolicyChecklist.md @@ -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` | diff --git a/test/Test.Testing/Emulator/Policies/SetQueryParameterTests.cs b/test/Test.Testing/Emulator/Policies/SetQueryParameterTests.cs new file mode 100644 index 00000000..a8b99335 --- /dev/null +++ b/test/Test.Testing/Emulator/Policies/SetQueryParameterTests.cs @@ -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"); + } +}