mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 22:19:39 +02:00
Throw exception in case WithTransformer is used after WithBodyFromFile (#1185)
* Fix .WithBodyFromFile + .WithTransformer combination * Ex
This commit is contained in:
@@ -13,6 +13,8 @@ namespace WireMock.ResponseBuilders;
|
||||
|
||||
public partial class Response
|
||||
{
|
||||
private bool _bodyFromFileSet;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithBody(Func<IRequestMessage, string> bodyFactory, string? destination = BodyDestinationFormat.SameAsSource, Encoding? encoding = null)
|
||||
{
|
||||
@@ -78,6 +80,8 @@ public partial class Response
|
||||
{
|
||||
Guard.NotNull(filename);
|
||||
|
||||
_bodyFromFileSet = true;
|
||||
|
||||
ResponseMessage.BodyData = new BodyData
|
||||
{
|
||||
BodyAsFileIsCached = cache,
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.ResponseBuilders;
|
||||
|
||||
public partial class Response
|
||||
{
|
||||
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer(bool)"/>
|
||||
public IResponseBuilder WithTransformer(bool transformContentFromBodyAsFile)
|
||||
{
|
||||
return WithTransformer(TransformerType.Handlebars, transformContentFromBodyAsFile);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer(ReplaceNodeOptions)"/>
|
||||
public IResponseBuilder WithTransformer(ReplaceNodeOptions options)
|
||||
{
|
||||
return WithTransformer(TransformerType.Handlebars, false, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithTransformer(TransformerType transformerType, bool transformContentFromBodyAsFile = false, ReplaceNodeOptions options = ReplaceNodeOptions.EvaluateAndTryToConvert)
|
||||
{
|
||||
if (_bodyFromFileSet)
|
||||
{
|
||||
throw new InvalidOperationException("WithTransformer should be used before WithBodyFromFile.");
|
||||
}
|
||||
|
||||
UseTransformer = true;
|
||||
TransformerType = transformerType;
|
||||
UseTransformerForBodyAsFile = transformContentFromBodyAsFile;
|
||||
TransformerReplaceNodeOptions = options;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -164,28 +164,6 @@ public partial class Response : IResponseBuilder
|
||||
return WithStatusCode((int)HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer(bool)"/>
|
||||
public IResponseBuilder WithTransformer(bool transformContentFromBodyAsFile)
|
||||
{
|
||||
return WithTransformer(TransformerType.Handlebars, transformContentFromBodyAsFile);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ITransformResponseBuilder.WithTransformer(ReplaceNodeOptions)"/>
|
||||
public IResponseBuilder WithTransformer(ReplaceNodeOptions options)
|
||||
{
|
||||
return WithTransformer(TransformerType.Handlebars, false, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithTransformer(TransformerType transformerType, bool transformContentFromBodyAsFile = false, ReplaceNodeOptions options = ReplaceNodeOptions.EvaluateAndTryToConvert)
|
||||
{
|
||||
UseTransformer = true;
|
||||
TransformerType = transformerType;
|
||||
UseTransformerForBodyAsFile = transformContentFromBodyAsFile;
|
||||
TransformerReplaceNodeOptions = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IResponseBuilder WithDelay(TimeSpan delay)
|
||||
{
|
||||
@@ -286,7 +264,7 @@ public partial class Response : IResponseBuilder
|
||||
|
||||
if (UseTransformer)
|
||||
{
|
||||
// Check if the body matcher is a RequestMessageProtoBufMatcher and try to to decode the byte-array to a BodyAsJson.
|
||||
// Check if the body matcher is a RequestMessageProtoBufMatcher and try to decode the byte-array to a BodyAsJson.
|
||||
if (mapping.RequestMatcher is Request requestMatcher && requestMessage is RequestMessage request)
|
||||
{
|
||||
var protoBufMatcher = requestMatcher.GetRequestMessageMatcher<RequestMessageProtoBufMatcher>()?.Matcher;
|
||||
|
||||
Reference in New Issue
Block a user