Skip to content

Commit

Permalink
Merge branch 'stateful-behavior'
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Oct 7, 2017
2 parents 8827531 + 8386d93 commit 9c55ff5
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 166 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
235 changes: 140 additions & 95 deletions src/WireMock.Net/Mapping.cs
Original file line number Diff line number Diff line change
@@ -1,96 +1,141 @@
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Matchers.Request;

namespace WireMock
{
/// <summary>
/// The Mapping.
/// </summary>
public class Mapping
{
/// <summary>
/// Gets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
public Guid Guid { get; }

/// <summary>
/// Gets the unique title.
/// </summary>
/// <value>
/// The unique title.
/// </value>
public string Title { get; }

/// <summary>
/// Gets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int Priority { get; }

/// <summary>
/// The Request matcher.
/// </summary>
public IRequestMatcher RequestMatcher { get; }

/// <summary>
/// The Provider.
/// </summary>
public IResponseProvider Provider { get; }

/// <summary>
/// Initializes a new instance of the <see cref="Mapping"/> class.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <param name="title">The unique title (can be null_.</param>
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="provider">The provider.</param>
/// <param name="priority">The priority for this mapping.</param>
public Mapping(Guid guid, [CanBeNull] string title, IRequestMatcher requestMatcher, IResponseProvider provider, int priority)
{
Priority = priority;
Guid = guid;
Title = title;
RequestMatcher = requestMatcher;
Provider = provider;
}

/// <summary>
/// The response to.
/// </summary>
/// <param name="requestMessage">The request message.</param>
/// <returns>The <see cref="ResponseMessage"/>.</returns>
public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage)
{
return await Provider.ProvideResponseAsync(requestMessage);
}

/// <summary>
/// Determines whether the RequestMessage is handled.
/// </summary>
/// <param name="requestMessage">The request message.</param>
/// <returns>The <see cref="RequestMatchResult"/>.</returns>
public RequestMatchResult IsRequestHandled(RequestMessage requestMessage)
{
var result = new RequestMatchResult();

RequestMatcher.GetMatchingScore(requestMessage, result);

return result;
}

/// <summary>
/// Gets a value indicating whether this mapping is an Admin Interface.
/// </summary>
/// <value>
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>.
/// </value>
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
}
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Matchers.Request;

namespace WireMock
{
/// <summary>
/// The Mapping.
/// </summary>
public class Mapping
{
/// <summary>
/// Gets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
public Guid Guid { get; }

/// <summary>
/// Gets the unique title.
/// </summary>
/// <value>
/// The unique title.
/// </value>
public string Title { get; }

/// <summary>
/// Gets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int Priority { get; }

/// <summary>
/// Scenario.
/// </summary>
[CanBeNull]
public string Scenario { get; }

/// <summary>
/// Execution state condition for the current mapping.
/// </summary>
[CanBeNull]
public object ExecutionConditionState { get; }

/// <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>
[CanBeNull]
public object NextState { get; }

/// <summary>
/// The Request matcher.
/// </summary>
public IRequestMatcher RequestMatcher { get; }

/// <summary>
/// The Provider.
/// </summary>
public IResponseProvider Provider { get; }

/// <summary>
/// Is State started ?
/// </summary>
public bool IsStartState => Scenario == null || Scenario != null && NextState != null && ExecutionConditionState == null;

/// <summary>
/// Initializes a new instance of the <see cref="Mapping"/> class.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <param name="title">The unique title (can be null_.</param>
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="provider">The provider.</param>
/// <param name="priority">The priority for this mapping.</param>
/// <param name="scenario">The scenario. [Optional]</param>
/// <param name="executionConditionState">State in which the current mapping can occur. [Optional]</param>
/// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</param>
public Mapping(Guid guid, [CanBeNull] string title, IRequestMatcher requestMatcher, IResponseProvider provider, int priority, [CanBeNull] string scenario, [CanBeNull] object executionConditionState, [CanBeNull] object nextState)
{
Guid = guid;
Title = title;
RequestMatcher = requestMatcher;
Provider = provider;
Priority = priority;
Scenario = scenario;
ExecutionConditionState = executionConditionState;
NextState = nextState;
}

/// <summary>
/// The response to.
/// </summary>
/// <param name="requestMessage">The request message.</param>
/// <returns>The <see cref="ResponseMessage"/>.</returns>
public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage)
{
return await Provider.ProvideResponseAsync(requestMessage);
}

/// <summary>
/// Gets the RequestMatchResult based on the RequestMessage.
/// </summary>
/// <param name="requestMessage">The request message.</param>
/// <param name="nextState">The Next State.</param>
/// <returns>The <see cref="RequestMatchResult"/>.</returns>
public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] object nextState)
{
var result = new RequestMatchResult();

RequestMatcher.GetMatchingScore(requestMessage, result);

// Only check state if Scenario is defined
if (Scenario != null)
{
var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
matcher.GetMatchingScore(requestMessage, result);
//// If ExecutionConditionState is null, this means that request is the start from a scenario. So just return.
//if (ExecutionConditionState != null)
//{
// // ExecutionConditionState is not null, so get score for matching with the nextState.
// var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
// matcher.GetMatchingScore(requestMessage, result);
//}
}

return result;
}

/// <summary>
/// Gets a value indicating whether this mapping is an Admin Interface.
/// </summary>
/// <value>
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>.
/// </value>
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
}
}
3 changes: 1 addition & 2 deletions src/WireMock.Net/Matchers/Request/RequestMatchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class RequestMatchResult : IComparable
/// <summary>
/// Gets the match details.
/// </summary>
public IList<KeyValuePair<Type, double>> MatchDetails { get; private set; }
public IList<KeyValuePair<Type, double>> MatchDetails { get; }

/// <summary>
/// Initializes a new instance of the <see cref="RequestMatchResult"/> class.
Expand Down Expand Up @@ -72,7 +72,6 @@ public double AddScore(Type matcherType, double score)
/// <returns>
/// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj" /> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj" />. Greater than zero This instance follows <paramref name="obj" /> in the sort order.
/// </returns>
/// <exception cref="System.NotImplementedException"></exception>
public int CompareTo(object obj)
{
var compareObj = (RequestMatchResult)obj;
Expand Down
Loading

0 comments on commit 9c55ff5

Please sign in to comment.