mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 01:50:36 +01:00
Add TimesInSameState to MappingModel (#1345)
* Add TimesInSameState to MappingModel * fix tests
This commit is contained in:
@@ -56,6 +56,11 @@ public class MappingModel
|
|||||||
/// In case the value is null state will not be changed.
|
/// In case the value is null state will not be changed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? SetStateTo { get; set; }
|
public string? SetStateTo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The number of times this match should be matched before the state will be changed to the specified one.
|
||||||
|
/// </summary>
|
||||||
|
public int? TimesInSameState { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The request model.
|
/// The request model.
|
||||||
@@ -86,7 +91,7 @@ public class MappingModel
|
|||||||
/// Fire and forget for webhooks.
|
/// Fire and forget for webhooks.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool? UseWebhooksFireAndForget { get; set; }
|
public bool? UseWebhooksFireAndForget { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Data Object which can be used when WithTransformer is used.
|
/// Data Object which can be used when WithTransformer is used.
|
||||||
/// e.g. lookup a path in this object using
|
/// e.g. lookup a path in this object using
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ public interface IMapping
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The number of times this match should be matched before the state will be changed to the next state.
|
/// The number of times this match should be matched before the state will be changed to the next state.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
int? StateTimes { get; }
|
int? TimesInSameState { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The RequestMatcher.
|
/// The RequestMatcher.
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class Mapping : IMapping
|
|||||||
public string? NextState { get; }
|
public string? NextState { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public int? StateTimes { get; }
|
public int? TimesInSameState { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IRequestMatcher RequestMatcher { get; }
|
public IRequestMatcher RequestMatcher { get; }
|
||||||
@@ -137,7 +137,7 @@ public class Mapping : IMapping
|
|||||||
Scenario = scenario;
|
Scenario = scenario;
|
||||||
ExecutionConditionState = executionConditionState;
|
ExecutionConditionState = executionConditionState;
|
||||||
NextState = nextState;
|
NextState = nextState;
|
||||||
StateTimes = stateTimes;
|
TimesInSameState = stateTimes;
|
||||||
Webhooks = webhooks;
|
Webhooks = webhooks;
|
||||||
UseWebhooksFireAndForget = useWebhooksFireAndForget;
|
UseWebhooksFireAndForget = useWebhooksFireAndForget;
|
||||||
TimeSettings = timeSettings;
|
TimeSettings = timeSettings;
|
||||||
|
|||||||
@@ -299,7 +299,7 @@ namespace WireMock.Owin
|
|||||||
scenario.Counter++;
|
scenario.Counter++;
|
||||||
|
|
||||||
// Only if the number of times this state is executed equals the required StateTimes, proceed to next state and reset the counter to 0
|
// Only if the number of times this state is executed equals the required StateTimes, proceed to next state and reset the counter to 0
|
||||||
if (scenario.Counter == (mapping.StateTimes ?? 1))
|
if (scenario.Counter == (mapping.TimesInSameState ?? 1))
|
||||||
{
|
{
|
||||||
scenario.NextState = mapping.NextState;
|
scenario.NextState = mapping.NextState;
|
||||||
scenario.Counter = 0;
|
scenario.Counter = 0;
|
||||||
|
|||||||
@@ -267,6 +267,7 @@ internal class MappingConverter(MatcherMapper mapper)
|
|||||||
Scenario = mapping.Scenario,
|
Scenario = mapping.Scenario,
|
||||||
WhenStateIs = mapping.ExecutionConditionState,
|
WhenStateIs = mapping.ExecutionConditionState,
|
||||||
SetStateTo = mapping.NextState,
|
SetStateTo = mapping.NextState,
|
||||||
|
TimesInSameState = !string.IsNullOrWhiteSpace(mapping.NextState) ? mapping.TimesInSameState : null,
|
||||||
Data = mapping.Data,
|
Data = mapping.Data,
|
||||||
Probability = mapping.Probability,
|
Probability = mapping.Probability,
|
||||||
Request = new RequestModel
|
Request = new RequestModel
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
if (!string.IsNullOrEmpty(mappingModel.SetStateTo))
|
if (!string.IsNullOrEmpty(mappingModel.SetStateTo))
|
||||||
{
|
{
|
||||||
respondProvider = respondProvider.WillSetStateTo(mappingModel.SetStateTo!);
|
respondProvider = respondProvider.WillSetStateTo(mappingModel.SetStateTo!, mappingModel.TimesInSameState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -165,5 +165,30 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
Response: {}
|
Response: {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Guid: 98fae52e-76df-47d9-876f-2ee32e931005,
|
||||||
|
UpdatedAt: 2023-01-14 15:16:17,
|
||||||
|
Scenario: To do list,
|
||||||
|
SetStateTo: TodoList State Started,
|
||||||
|
TimesInSameState: 2,
|
||||||
|
Request: {
|
||||||
|
Path: {
|
||||||
|
Matchers: [
|
||||||
|
{
|
||||||
|
Name: WildcardMatcher,
|
||||||
|
Pattern: /todo/items,
|
||||||
|
IgnoreCase: false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Methods: [
|
||||||
|
GET
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Response: {
|
||||||
|
BodyDestination: SameAsSource,
|
||||||
|
Body: Buy milk
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -66,3 +66,13 @@ builder
|
|||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
builder
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingMethod("GET")
|
||||||
|
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/todo/items", false, WireMock.Matchers.MatchOperator.Or))
|
||||||
|
)
|
||||||
|
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931005")
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody("Buy milk")
|
||||||
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -66,3 +66,13 @@ server
|
|||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingMethod("GET")
|
||||||
|
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/todo/items", false, WireMock.Matchers.MatchOperator.Or))
|
||||||
|
)
|
||||||
|
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931005")
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody("Buy milk")
|
||||||
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -161,5 +161,30 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Guid: 98fae52e-76df-47d9-876f-2ee32e931005,
|
||||||
|
UpdatedAt: 2023-01-14T15:16:17,
|
||||||
|
Scenario: To do list,
|
||||||
|
SetStateTo: TodoList State Started,
|
||||||
|
TimesInSameState: 2,
|
||||||
|
Request: {
|
||||||
|
Path: {
|
||||||
|
Matchers: [
|
||||||
|
{
|
||||||
|
Name: WildcardMatcher,
|
||||||
|
Pattern: /todo/items,
|
||||||
|
IgnoreCase: false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Methods: [
|
||||||
|
GET
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Response: {
|
||||||
|
BodyDestination: SameAsSource,
|
||||||
|
Body: Buy milk
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -34,6 +34,7 @@ public class MappingBuilderTests
|
|||||||
private static readonly DateTime UtcNow = new(2023, 1, 14, 15, 16, 17);
|
private static readonly DateTime UtcNow = new(2023, 1, 14, 15, 16, 17);
|
||||||
|
|
||||||
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
|
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
|
||||||
|
private readonly int _numMappings;
|
||||||
|
|
||||||
private readonly MappingBuilder _sut;
|
private readonly MappingBuilder _sut;
|
||||||
|
|
||||||
@@ -104,11 +105,20 @@ public class MappingBuilderTests
|
|||||||
).RespondWith(Response.Create());
|
).RespondWith(Response.Create());
|
||||||
|
|
||||||
_sut.Given(Request.Create()
|
_sut.Given(Request.Create()
|
||||||
.WithPath("/regex")
|
.WithPath("/regex")
|
||||||
.WithParam("foo", new RegexMatcher(".*"))
|
.WithParam("foo", new RegexMatcher(".*"))
|
||||||
.UsingGet()
|
.UsingGet()
|
||||||
)
|
).RespondWith(Response.Create());
|
||||||
.RespondWith(Response.Create());
|
|
||||||
|
_sut.Given(Request.Create()
|
||||||
|
.WithPath("/todo/items")
|
||||||
|
.UsingGet())
|
||||||
|
.InScenario("To do list")
|
||||||
|
.WillSetStateTo("TodoList State Started", 2)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithBody("Buy milk"));
|
||||||
|
|
||||||
|
_numMappings = _sut.GetMappings().Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -197,9 +207,9 @@ public class MappingBuilderTests
|
|||||||
_sut.SaveMappingsToFolder(null);
|
_sut.SaveMappingsToFolder(null);
|
||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Exactly(5));
|
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Exactly(_numMappings));
|
||||||
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Exactly(5));
|
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Exactly(_numMappings));
|
||||||
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(5));
|
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(_numMappings));
|
||||||
_fileSystemHandlerMock.VerifyNoOtherCalls();
|
_fileSystemHandlerMock.VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,8 +225,8 @@ public class MappingBuilderTests
|
|||||||
|
|
||||||
// Verify
|
// Verify
|
||||||
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
|
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
|
||||||
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Exactly(5));
|
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Exactly(_numMappings));
|
||||||
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(5));
|
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(_numMappings));
|
||||||
_fileSystemHandlerMock.VerifyNoOtherCalls();
|
_fileSystemHandlerMock.VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user