Skip to content

Commit

Permalink
scenario and state (mapping json model)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Oct 7, 2017
1 parent 5424b1e commit 8386d93
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
62 changes: 49 additions & 13 deletions examples/WireMock.Net.ConsoleApplication/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void Run()

server.SetBasicAuthentication("a", "b");

server.AllowPartialMapping();
// server.AllowPartialMapping();

server
.Given(Request.Create().WithPath("/oauth2/access").UsingPost().WithBody("grant_type=password;username=u;password=p"))
Expand Down Expand Up @@ -95,19 +95,55 @@ public static void Run()
.Given(Request.Create().WithPath("/partial").UsingPost().WithBody(new SimMetricsMatcher(new[] { "cat", "dog" })))
.RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200"));

http://localhost:8080/any/any?start=1000&stop=1&stop=2
// http://localhost:8080/any/any?start=1000&stop=1&stop=2
//server
// .Given(Request.Create().WithPath("/*").UsingGet())
// .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
// .AtPriority(server.Mappings.Count() + 1)
// .RespondWith(Response.Create()
// .WithStatusCode(200)
// .WithHeader("Content-Type", "application/json")
// .WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
// .WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }")
// .WithTransformer()
// .WithDelay(TimeSpan.FromMilliseconds(100))
// );

server
.Given(Request.Create().WithPath("/*").UsingGet())
.WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05")
.AtPriority(server.Mappings.Count() + 1)
.Given(Request.Create()
.WithPath("/state1")
.UsingGet())
.InScenario("s1")
.WillSetStateTo("Test state 1")
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
.WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }")
.WithTransformer()
.WithDelay(TimeSpan.FromMilliseconds(100))
);
.WithBody("No state msg 1"));

server
.Given(Request.Create()
.WithPath("/foostate1")
.UsingGet())
.InScenario("s1")
.WhenStateIs("Test state 1")
.RespondWith(Response.Create()
.WithBody("Test state msg 1"));

server
.Given(Request.Create()
.WithPath("/state2")
.UsingGet())
.InScenario("s2")
.WillSetStateTo("Test state 2")
.RespondWith(Response.Create()
.WithBody("No state msg 2"));

server
.Given(Request.Create()
.WithPath("/foostate2")
.UsingGet())
.InScenario("s2")
.WhenStateIs("Test state 2")
.RespondWith(Response.Create()
.WithBody("Test state msg 2"));

System.Console.WriteLine("Press any key to stop the server");
System.Console.ReadKey();
Expand All @@ -121,4 +157,4 @@ public static void Run()
System.Console.ReadKey();
}
}
}
}
16 changes: 16 additions & 0 deletions src/WireMock.Net/Admin/Mappings/MappingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public class MappingModel
/// </value>
public int? Priority { get; set; }

/// <summary>
/// Scenario.
/// </summary>
public string Scenario { get; set; }

/// <summary>
/// Execution state condition for the current mapping.
/// </summary>
public object WhenStateIs { get; set; }

/// <summary>
/// The next state which will be signaled after the current mapping execution.
/// In case the value is null state will not be changed.
/// </summary>
public object SetStateTo { get; set; }

/// <summary>
/// Gets or sets the request.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WireMock.Matchers.Request
/// <summary>
/// The scenario and state matcher.
/// </summary>
public class RequestMessageScenarioAndStateMatcher : IRequestMatcher
internal class RequestMessageScenarioAndStateMatcher : IRequestMatcher
{
///// <summary>
///// Scenario.
Expand Down
3 changes: 3 additions & 0 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static MappingModel ToMappingModel(Mapping mapping)
Guid = mapping.Guid,
Title = mapping.Title,
Priority = mapping.Priority,
Scenario = mapping.Scenario,
WhenStateIs = mapping.ExecutionConditionState,
SetStateTo = mapping.NextState,
Request = new RequestModel
{
ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
Expand Down
7 changes: 7 additions & 0 deletions src/WireMock.Net/Server/FluentMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ private void DeserializeAndAddMapping(string json, Guid? guid = null)
if (mappingModel.Priority != null)
respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value);

if (mappingModel.Scenario != null)
{
respondProvider = respondProvider.InScenario(mappingModel.Scenario);
respondProvider = respondProvider.WhenStateIs(mappingModel.WhenStateIs);
respondProvider = respondProvider.WillSetStateTo(mappingModel.SetStateTo);
}

respondProvider.RespondWith(responseBuilder);
}

Expand Down

0 comments on commit 8386d93

Please sign in to comment.