Files
WireMock.Net-wiremock/test/WireMock.Net.Tests/WireMockServer.Settings.cs
T
Mattias SundströmandMattias 0132ec873a Add ScribanRegistrationCallback to register custom Scriban functions (#1515) (#1516)
- Add WireMockServerSettings.ScribanRegistrationCallback
  (Action<object, IFileSystemHandler>?) so code-configured servers can add
  functions, objects or variables to Scriban and Scriban-DotLiquid templates.
- Pass the settings to ScribanContextFactory, which invokes the callback with
  a new ScriptObject per transformer context.
- Push the custom globals beneath the template model in ScribanContext so
  request, response, mapping and data take precedence; keep the original
  Template.Render call when no callback is configured.
- Add the strongly typed WithScribanRegistration extension method.
- Add a Scriban example to the .NET 8 console app.
- Make the proxy tests use a local target server instead of www.google.com,
  and skip the IPv6 loopback test when the OS has no IPv6 support.

Co-authored-by: Mattias <13809209+Quattordici@users.noreply.github.com>
2026-10-18 09:29:11 +02:00

266 lines
8.9 KiB
C#

// Copyright © WireMock.Net
using System.Net.Http;
using Moq;
using Scriban.Runtime;
using WireMock.Authentication;
using WireMock.Constants;
using WireMock.Logging;
using WireMock.Owin;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;
using WireMock.Types;
namespace WireMock.Net.Tests;
public class WireMockServerSettingsTests
{
private readonly Mock<IWireMockLogger> _loggerMock;
public WireMockServerSettingsTests()
{
_loggerMock = new Mock<IWireMockLogger>();
_loggerMock.Setup(l => l.Info(It.IsAny<string>(), It.IsAny<object[]>()));
}
[Fact]
public void WireMockServer_WireMockServerSettings_StartAdminInterfaceTrue_BasicAuthenticationIsSet()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
AdminUsername = "u",
AdminPassword = "p"
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.AuthenticationMatcher.Should().NotBeNull().And.BeOfType<BasicAuthenticationMatcher>();
}
[Fact]
public void WireMockServer_WireMockServerSettings_StartAdminInterfaceTrue_AzureADAuthenticationIsSet()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
AdminAzureADTenant = "t",
AdminAzureADAudience = "a"
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.AuthenticationMatcher.Should().NotBeNull().And.BeOfType<AzureADAuthenticationMatcher>();
}
[Fact]
public void WireMockServer_WireMockServerSettings_StartAdminInterfaceFalse_BasicAuthenticationIsNotSet()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = false,
AdminUsername = "u",
AdminPassword = "p"
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.AuthenticationMatcher.Should().BeNull();
}
[Fact]
public void WireMockServer_WireMockServerSettings_PriorityFromAllAdminMappingsIsLow_When_StartAdminInterface_IsTrue()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true
});
// Assert
server.Mappings.Should().NotBeNull();
server.Mappings.Should().HaveCount(Constants.NumAdminMappings);
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
}
[Fact]
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_IsMinus2000000_When_StartAdminInterface_IsTrue()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
StartAdminInterface = true,
ProxyAndRecordSettings = new ProxyAndRecordSettings
{
Url = "www.google.com"
}
});
// Assert
server.Mappings.Should().NotBeNull();
server.Mappings.Should().HaveCount(Constants.NumAdminMappings + 1);
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(Constants.NumAdminMappings);
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
}
[Fact]
public void WireMockServer_WireMockServerSettings_ProxyAndRecordSettings_ProxyPriority_Is0_When_StartAdminInterface_IsFalse()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
ProxyAndRecordSettings = new ProxyAndRecordSettings
{
Url = "www.google.com"
}
});
// Assert
var mappings = server.Mappings.ToArray();
mappings.Count().Should().Be(1);
mappings[0].Priority.Should().Be(0);
}
[Fact]
public void WireMockServer_WireMockServerSettings_AllowPartialMapping()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
Logger = _loggerMock.Object,
AllowPartialMapping = true
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.AllowPartialMapping.Should().Be(true);
// Verify
_loggerMock.Verify(l => l.Info(It.IsAny<string>(), It.IsAny<bool>()));
}
[Fact]
public void WireMockServer_WireMockServerSettings_AllowBodyForAllHttpMethods()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
Logger = _loggerMock.Object,
AllowBodyForAllHttpMethods = true
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.AllowBodyForAllHttpMethods.Should().Be(true);
// Verify
_loggerMock.Verify(l => l.Info(It.Is<string>(s => s.Contains("AllowBodyForAllHttpMethods") && s.Contains("True"))));
}
[Fact]
public void WireMockServer_WireMockServerSettings_AllowOnlyDefinedHttpStatusCodeInResponse()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
Logger = _loggerMock.Object,
AllowOnlyDefinedHttpStatusCodeInResponse = true
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.AllowOnlyDefinedHttpStatusCodeInResponse.Should().Be(true);
// Verify
_loggerMock.Verify(l => l.Info(It.Is<string>(s => s.Contains("AllowOnlyDefinedHttpStatusCodeInResponse") && s.Contains("True"))));
}
[Fact]
public void WireMockServer_WireMockServerSettings_RequestLogExpirationDuration()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
Logger = _loggerMock.Object,
RequestLogExpirationDuration = 1
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.RequestLogExpirationDuration.Should().Be(1);
}
#if NET6_0_OR_GREATER
[Fact]
public void WireMockServer_WireMockServerSettings_ActivityTracingOptions_AreMappedToMiddlewareOptions()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings
{
ActivityTracingOptions = new ActivityTracingOptions
{
ExcludeAdminRequests = false,
RecordRequestBody = true,
RecordResponseBody = true,
RecordMatchDetails = false
}
});
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.ActivityTracingOptions.Should().NotBeNull();
options.ActivityTracingOptions!.ExcludeAdminRequests.Should().BeFalse();
options.ActivityTracingOptions.RecordRequestBody.Should().BeTrue();
options.ActivityTracingOptions.RecordResponseBody.Should().BeTrue();
options.ActivityTracingOptions.RecordMatchDetails.Should().BeFalse();
}
[Fact]
public void WireMockServer_WireMockServerSettings_Without_ActivityTracingOptions_ShouldNotSetMiddlewareOptions()
{
// Assign and Act
var server = WireMockServer.Start(new WireMockServerSettings());
// Assert
var options = server.GetPrivateFieldValue<IWireMockMiddlewareOptions>("_options");
options.ActivityTracingOptions.Should().BeNull();
}
#endif
[Theory]
[InlineData(TransformerType.Scriban)]
[InlineData(TransformerType.ScribanDotLiquid)]
public async Task WireMockServer_WireMockServerSettings_ScribanRegistrationCallback_IsUsedInResponseTemplate(TransformerType transformerType)
{
// Arrange
var cancellationToken = TestContext.Current.CancellationToken;
var path = $"/scriban_{Guid.NewGuid()}";
using var server = WireMockServer.Start(new WireMockServerSettings
{
ScribanRegistrationCallback = (scriptObject, _) =>
((ScriptObject)scriptObject).Import("fake_name", new Func<string>(() => "Jane Doe"))
});
server
.Given(Request.Create().WithPath(path).UsingGet())
.RespondWith(Response.Create()
.WithBody("{{ fake_name }} {{ request.Path }}")
.WithTransformer(transformerType)
);
// Act
var response = await new HttpClient().GetStringAsync(server.Url + path, cancellationToken);
// Assert
response.Should().Be($"Jane Doe {path}");
}
}