Skip to content
Merged
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
7 changes: 6 additions & 1 deletion src/WireMock.Net.Abstractions/Admin/Mappings/MappingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class MappingModel
/// In case the value is null state will not be changed.
/// </summary>
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>
/// The request model.
Expand Down Expand Up @@ -86,7 +91,7 @@ public class MappingModel
/// Fire and forget for webhooks.
/// </summary>
public bool? UseWebhooksFireAndForget { get; set; }

/// <summary>
/// Data Object which can be used when WithTransformer is used.
/// e.g. lookup a path in this object using
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.Minimal/IMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface IMapping
/// <summary>
/// The number of times this match should be matched before the state will be changed to the next state.
/// </summary>
int? StateTimes { get; }
int? TimesInSameState { get; }

/// <summary>
/// The RequestMatcher.
Expand Down
4 changes: 2 additions & 2 deletions src/WireMock.Net.Minimal/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Mapping : IMapping
public string? NextState { get; }

/// <inheritdoc />
public int? StateTimes { get; }
public int? TimesInSameState { get; }

/// <inheritdoc />
public IRequestMatcher RequestMatcher { get; }
Expand Down Expand Up @@ -137,7 +137,7 @@ public Mapping
Scenario = scenario;
ExecutionConditionState = executionConditionState;
NextState = nextState;
StateTimes = stateTimes;
TimesInSameState = stateTimes;
Webhooks = webhooks;
UseWebhooksFireAndForget = useWebhooksFireAndForget;
TimeSettings = timeSettings;
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private void UpdateScenarioState(IMapping mapping)
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
if (scenario.Counter == (mapping.StateTimes ?? 1))
if (scenario.Counter == (mapping.TimesInSameState ?? 1))
{
scenario.NextState = mapping.NextState;
scenario.Counter = 0;
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net.Minimal/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public MappingModel ToMappingModel(IMapping mapping)
Scenario = mapping.Scenario,
WhenStateIs = mapping.ExecutionConditionState,
SetStateTo = mapping.NextState,
TimesInSameState = !string.IsNullOrWhiteSpace(mapping.NextState) ? mapping.TimesInSameState : null,
Data = mapping.Data,
Probability = mapping.Probability,
Request = new RequestModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private Guid ConvertMappingAndRegisterAsRespondProvider(MappingModel mappingMode

if (!string.IsNullOrEmpty(mappingModel.SetStateTo))
{
respondProvider = respondProvider.WillSetStateTo(mappingModel.SetStateTo!);
respondProvider = respondProvider.WillSetStateTo(mappingModel.SetStateTo!, mappingModel.TimesInSameState);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,30 @@
]
},
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
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ builder
.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")
);

Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ server
.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")
);

25 changes: 25 additions & 0 deletions test/WireMock.Net.Tests/MappingBuilderTests.ToJson.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]
30 changes: 20 additions & 10 deletions test/WireMock.Net.Tests/MappingBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static MappingBuilderTests()
private static readonly DateTime UtcNow = new(2023, 1, 14, 15, 16, 17);

private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
private readonly int _numMappings;

private readonly MappingBuilder _sut;

Expand Down Expand Up @@ -104,11 +105,20 @@ public MappingBuilderTests()
).RespondWith(Response.Create());

_sut.Given(Request.Create()
.WithPath("/regex")
.WithParam("foo", new RegexMatcher(".*"))
.UsingGet()
)
.RespondWith(Response.Create());
.WithPath("/regex")
.WithParam("foo", new RegexMatcher(".*"))
.UsingGet()
).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]
Expand Down Expand Up @@ -197,9 +207,9 @@ public void SaveMappingsToFolder_FolderIsNull()
_sut.SaveMappingsToFolder(null);

// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Exactly(5));
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Exactly(5));
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(5));
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Exactly(_numMappings));
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(mappingFolder), Times.Exactly(_numMappings));
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(_numMappings));
_fileSystemHandlerMock.VerifyNoOtherCalls();
}

Expand All @@ -215,8 +225,8 @@ public void SaveMappingsToFolder_FolderExists_IsTrue()

// Verify
_fileSystemHandlerMock.Verify(fs => fs.GetMappingFolder(), Times.Never);
_fileSystemHandlerMock.Verify(fs => fs.FolderExists(path), Times.Exactly(5));
_fileSystemHandlerMock.Verify(fs => fs.WriteMappingFile(It.IsAny<string>(), It.IsAny<string>()), 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(_numMappings));
_fileSystemHandlerMock.VerifyNoOtherCalls();
}
}
Expand Down
Loading