mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-22 16:28:27 +02:00
Expose scenario states (#168)
* Scenarios (WIP) * Update ToJson and StatefulBehaviorTests * fix tests * Update comment
This commit is contained in:
@@ -43,11 +43,14 @@ namespace WireMock.Net.Client
|
|||||||
var request = api.GetRequestsAsync().Result;
|
var request = api.GetRequestsAsync().Result;
|
||||||
Console.WriteLine($"request = {JsonConvert.SerializeObject(request)}");
|
Console.WriteLine($"request = {JsonConvert.SerializeObject(request)}");
|
||||||
|
|
||||||
var deleteRequestsAsync = api.DeleteRequestsAsync().Result;
|
//var deleteRequestsAsync = api.DeleteRequestsAsync().Result;
|
||||||
Console.WriteLine($"deleteRequestsAsync = {deleteRequestsAsync.Status}");
|
//Console.WriteLine($"DeleteRequestsAsync = {deleteRequestsAsync.Status}");
|
||||||
|
|
||||||
var resetRequestsAsync = api.ResetRequestsAsync().Result;
|
//var resetRequestsAsync = api.ResetRequestsAsync().Result;
|
||||||
Console.WriteLine($"resetRequestsAsync = {resetRequestsAsync.Status}");
|
//Console.WriteLine($"ResetRequestsAsync = {resetRequestsAsync.Status}");
|
||||||
|
|
||||||
|
var scenarioStates = api.GetScenariosAsync().Result;
|
||||||
|
Console.WriteLine($"GetScenariosAsync = {JsonConvert.SerializeObject(scenarioStates)}");
|
||||||
|
|
||||||
Console.WriteLine("Press any key to quit");
|
Console.WriteLine("Press any key to quit");
|
||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|||||||
@@ -30,13 +30,13 @@ namespace WireMock.Admin.Mappings
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Execution state condition for the current mapping.
|
/// Execution state condition for the current mapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public object WhenStateIs { get; set; }
|
public string WhenStateIs { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The next state which will be signaled after the current mapping execution.
|
/// The next state which will be signaled after the current mapping execution.
|
||||||
/// 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 object SetStateTo { get; set; }
|
public string SetStateTo { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The request.
|
/// The request.
|
||||||
|
|||||||
28
src/WireMock.Net/Admin/Scenarios/ScenarioStateModel.cs
Normal file
28
src/WireMock.Net/Admin/Scenarios/ScenarioStateModel.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
namespace WireMock.Admin.Scenarios
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// ScenarioStateModel
|
||||||
|
/// </summary>
|
||||||
|
public class ScenarioStateModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the name.
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the NextState.
|
||||||
|
/// </summary>
|
||||||
|
public string NextState { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this <see cref="ScenarioStateModel"/> is started.
|
||||||
|
/// </summary>
|
||||||
|
public bool Started { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this <see cref="ScenarioStateModel"/> is finished.
|
||||||
|
/// </summary>
|
||||||
|
public bool Finished { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -145,7 +145,7 @@ namespace WireMock.Client
|
|||||||
/// Get all scenarios
|
/// Get all scenarios
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Get("__admin/scenarios")]
|
[Get("__admin/scenarios")]
|
||||||
Task<string> GetScenariosAsync();
|
Task<IList<ScenarioState>> GetScenariosAsync();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete (reset) all scenarios
|
/// Delete (reset) all scenarios
|
||||||
|
|||||||
@@ -41,14 +41,14 @@ namespace WireMock
|
|||||||
/// Execution state condition for the current mapping.
|
/// Execution state condition for the current mapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
public object ExecutionConditionState { get; }
|
public string ExecutionConditionState { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The next state which will be signaled after the current mapping execution.
|
/// The next state which will be signaled after the current mapping execution.
|
||||||
/// In case the value is null state will not be changed.
|
/// In case the value is null, state will not be changed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
public object NextState { get; }
|
public string NextState { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Request matcher.
|
/// The Request matcher.
|
||||||
@@ -77,7 +77,7 @@ namespace WireMock
|
|||||||
/// <param name="scenario">The scenario. [Optional]</param>
|
/// <param name="scenario">The scenario. [Optional]</param>
|
||||||
/// <param name="executionConditionState">State in which the current mapping can occur. [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>
|
/// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</param>
|
||||||
public Mapping(Guid guid, [CanBeNull] string title, [CanBeNull] string path, IRequestMatcher requestMatcher, IResponseProvider provider, int priority, [CanBeNull] string scenario, [CanBeNull] object executionConditionState, [CanBeNull] object nextState)
|
public Mapping(Guid guid, [CanBeNull] string title, [CanBeNull] string path, IRequestMatcher requestMatcher, IResponseProvider provider, int priority, [CanBeNull] string scenario, [CanBeNull] string executionConditionState, [CanBeNull] string nextState)
|
||||||
{
|
{
|
||||||
Guid = guid;
|
Guid = guid;
|
||||||
Title = title;
|
Title = title;
|
||||||
@@ -106,7 +106,7 @@ namespace WireMock
|
|||||||
/// <param name="requestMessage">The request message.</param>
|
/// <param name="requestMessage">The request message.</param>
|
||||||
/// <param name="nextState">The Next State.</param>
|
/// <param name="nextState">The Next State.</param>
|
||||||
/// <returns>The <see cref="RequestMatchResult"/>.</returns>
|
/// <returns>The <see cref="RequestMatchResult"/>.</returns>
|
||||||
public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] object nextState)
|
public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] string nextState)
|
||||||
{
|
{
|
||||||
var result = new RequestMatchResult();
|
var result = new RequestMatchResult();
|
||||||
|
|
||||||
|
|||||||
@@ -11,21 +11,21 @@ namespace WireMock.Matchers.Request
|
|||||||
/// Execution state condition for the current mapping.
|
/// Execution state condition for the current mapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
private readonly object _executionConditionState;
|
private readonly string _executionConditionState;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The next state which will be signaled after the current mapping execution.
|
/// The next state which will be signaled after the current mapping execution.
|
||||||
/// In case the value is null state will not be changed.
|
/// In case the value is null state will not be changed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[CanBeNull]
|
[CanBeNull]
|
||||||
private readonly object _nextState;
|
private readonly string _nextState;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="RequestMessageScenarioAndStateMatcher"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessageScenarioAndStateMatcher"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="nextState">The next state.</param>
|
/// <param name="nextState">The next state.</param>
|
||||||
/// <param name="executionConditionState">Execution state condition for the current mapping.</param>
|
/// <param name="executionConditionState">Execution state condition for the current mapping.</param>
|
||||||
public RequestMessageScenarioAndStateMatcher([CanBeNull] object nextState, [CanBeNull] object executionConditionState)
|
public RequestMessageScenarioAndStateMatcher([CanBeNull] string nextState, [CanBeNull] string executionConditionState)
|
||||||
{
|
{
|
||||||
_nextState = nextState;
|
_nextState = nextState;
|
||||||
_executionConditionState = executionConditionState;
|
_executionConditionState = executionConditionState;
|
||||||
|
|||||||
@@ -60,7 +60,10 @@ namespace WireMock.Owin
|
|||||||
// Set start
|
// Set start
|
||||||
if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
|
if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
|
||||||
{
|
{
|
||||||
_options.Scenarios.TryAdd(mapping.Scenario, null);
|
_options.Scenarios.TryAdd(mapping.Scenario, new ScenarioState
|
||||||
|
{
|
||||||
|
Name = mapping.Scenario
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +71,7 @@ namespace WireMock.Owin
|
|||||||
.Select(m => new
|
.Select(m => new
|
||||||
{
|
{
|
||||||
Mapping = m,
|
Mapping = m,
|
||||||
MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.ContainsKey(m.Scenario) ? _options.Scenarios[m.Scenario] : null)
|
MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.ContainsKey(m.Scenario) ? _options.Scenarios[m.Scenario].NextState : null)
|
||||||
})
|
})
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
@@ -125,7 +128,9 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
if (targetMapping.Scenario != null)
|
if (targetMapping.Scenario != null)
|
||||||
{
|
{
|
||||||
_options.Scenarios[targetMapping.Scenario] = targetMapping.NextState;
|
_options.Scenarios[targetMapping.Scenario].NextState = targetMapping.NextState;
|
||||||
|
_options.Scenarios[targetMapping.Scenario].Started = true;
|
||||||
|
_options.Scenarios[targetMapping.Scenario].Finished = targetMapping.NextState == null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
public bool AllowPartialMapping { get; set; }
|
public bool AllowPartialMapping { get; set; }
|
||||||
|
|
||||||
public ConcurrentDictionary<Guid, Mapping> Mappings { get; } = new ConcurrentDictionary<Guid, Mapping>(); // Checked
|
public ConcurrentDictionary<Guid, Mapping> Mappings { get; } = new ConcurrentDictionary<Guid, Mapping>();
|
||||||
|
|
||||||
public ConcurrentDictionary<string, object> Scenarios { get; } = new ConcurrentDictionary<string, object>(); // Checked
|
public ConcurrentDictionary<string, ScenarioState> Scenarios { get; } = new ConcurrentDictionary<string, ScenarioState>();
|
||||||
|
|
||||||
public ObservableCollection<LogEntry> LogEntries { get; } = new ConcurentObservableCollection<LogEntry>();
|
public ObservableCollection<LogEntry> LogEntries { get; } = new ConcurentObservableCollection<LogEntry>();
|
||||||
|
|
||||||
|
|||||||
28
src/WireMock.Net/ScenarioState.cs
Normal file
28
src/WireMock.Net/ScenarioState.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
namespace WireMock
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The ScenarioState
|
||||||
|
/// </summary>
|
||||||
|
public class ScenarioState
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the Name (from the Scenario).
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the NextState.
|
||||||
|
/// </summary>
|
||||||
|
public string NextState { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this <see cref="ScenarioState"/> is started.
|
||||||
|
/// </summary>
|
||||||
|
public bool Started { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this <see cref="ScenarioState"/> is finished.
|
||||||
|
/// </summary>
|
||||||
|
public bool Finished { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,19 +37,16 @@ namespace WireMock.Serialization
|
|||||||
ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
|
ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
|
||||||
{
|
{
|
||||||
Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
|
Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
|
||||||
//Funcs = Map(clientIPMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
|
|
||||||
} : null,
|
} : null,
|
||||||
|
|
||||||
Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
|
Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
|
||||||
{
|
{
|
||||||
Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
|
Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
|
||||||
//Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
|
|
||||||
} : null,
|
} : null,
|
||||||
|
|
||||||
Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
|
Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
|
||||||
{
|
{
|
||||||
Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
|
Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers))
|
||||||
//Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
|
|
||||||
} : null,
|
} : null,
|
||||||
|
|
||||||
Methods = methodMatcher?.Methods,
|
Methods = methodMatcher?.Methods,
|
||||||
@@ -58,28 +55,23 @@ namespace WireMock.Serialization
|
|||||||
{
|
{
|
||||||
Name = hm.Name,
|
Name = hm.Name,
|
||||||
Matchers = MatcherMapper.Map(hm.Matchers)
|
Matchers = MatcherMapper.Map(hm.Matchers)
|
||||||
//Funcs = Map(hm.Funcs)
|
|
||||||
}).ToList() : null,
|
}).ToList() : null,
|
||||||
|
|
||||||
Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
|
Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
|
||||||
{
|
{
|
||||||
Name = cm.Name,
|
Name = cm.Name,
|
||||||
Matchers = MatcherMapper.Map(cm.Matchers)
|
Matchers = MatcherMapper.Map(cm.Matchers)
|
||||||
//Funcs = Map(cm.Funcs)
|
|
||||||
}).ToList() : null,
|
}).ToList() : null,
|
||||||
|
|
||||||
Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
|
Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel
|
||||||
{
|
{
|
||||||
Name = pm.Key,
|
Name = pm.Key,
|
||||||
Matchers = MatcherMapper.Map(pm.Matchers)
|
Matchers = MatcherMapper.Map(pm.Matchers)
|
||||||
//Funcs = Map(pm.Funcs)
|
|
||||||
}).ToList() : null,
|
}).ToList() : null,
|
||||||
|
|
||||||
Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
|
Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
|
||||||
{
|
{
|
||||||
Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null
|
Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null
|
||||||
//Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
|
|
||||||
//DataFunc = bodyMatcher != null ? Map(bodyMatcher.DataFunc) : null
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Response = new ResponseModel
|
Response = new ResponseModel
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using JetBrains.Annotations;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
|
using WireMock.Admin.Scenarios;
|
||||||
using WireMock.Admin.Settings;
|
using WireMock.Admin.Settings;
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.Logging;
|
using WireMock.Logging;
|
||||||
@@ -41,7 +42,13 @@ namespace WireMock.Server
|
|||||||
private readonly JsonSerializerSettings _settings = new JsonSerializerSettings
|
private readonly JsonSerializerSettings _settings = new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
NullValueHandling = NullValueHandling.Ignore,
|
NullValueHandling = NullValueHandling.Ignore
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly JsonSerializerSettings _settingsIncludeNullValues = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Formatting = Formatting.Indented,
|
||||||
|
NullValueHandling = NullValueHandling.Include
|
||||||
};
|
};
|
||||||
|
|
||||||
#region InitAdmin
|
#region InitAdmin
|
||||||
@@ -525,13 +532,15 @@ namespace WireMock.Server
|
|||||||
#region Scenarios
|
#region Scenarios
|
||||||
private ResponseMessage ScenariosGet(RequestMessage requestMessage)
|
private ResponseMessage ScenariosGet(RequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
var scenarios = Scenarios.ToArray().Select(s => new
|
var scenariosStates = Scenarios.Values.Select(s => new ScenarioStateModel
|
||||||
{
|
{
|
||||||
Name = s.Key,
|
Name = s.Name,
|
||||||
Started = s.Value != null,
|
NextState = s.NextState,
|
||||||
NextState = s.Value
|
Started = s.Started,
|
||||||
|
Finished = s.Finished
|
||||||
});
|
});
|
||||||
return ToJson(scenarios);
|
|
||||||
|
return ToJson(scenariosStates, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseMessage ScenariosReset(RequestMessage requestMessage)
|
private ResponseMessage ScenariosReset(RequestMessage requestMessage)
|
||||||
@@ -706,11 +715,11 @@ namespace WireMock.Server
|
|||||||
return responseBuilder;
|
return responseBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseMessage ToJson<T>(T result)
|
private ResponseMessage ToJson<T>(T result, bool keepNullValues = false)
|
||||||
{
|
{
|
||||||
return new ResponseMessage
|
return new ResponseMessage
|
||||||
{
|
{
|
||||||
Body = JsonConvert.SerializeObject(result, _settings),
|
Body = JsonConvert.SerializeObject(result, keepNullValues ? _settingsIncludeNullValues : _settings),
|
||||||
StatusCode = 200,
|
StatusCode = 200,
|
||||||
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>("application/json") } }
|
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>("application/json") } }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ namespace WireMock.Server
|
|||||||
/// Gets the scenarios.
|
/// Gets the scenarios.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public ConcurrentDictionary<string, object> Scenarios => new ConcurrentDictionary<string, object>(_options.Scenarios);
|
public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>(_options.Scenarios);
|
||||||
|
|
||||||
#region IDisposable Members
|
#region IDisposable Members
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -66,13 +66,13 @@ namespace WireMock.Server
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="state">Any object which identifies the current state</param>
|
/// <param name="state">Any object which identifies the current state</param>
|
||||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
IRespondWithAProvider WhenStateIs(object state);
|
IRespondWithAProvider WhenStateIs(string state);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Once this mapping is executed the state will be changed to specified one.
|
/// Once this mapping is executed the state will be changed to specified one.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="state">Any object which identifies the new state</param>
|
/// <param name="state">Any object which identifies the new state</param>
|
||||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||||
IRespondWithAProvider WillSetStateTo(object state);
|
IRespondWithAProvider WillSetStateTo(string state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,8 +12,8 @@ namespace WireMock.Server
|
|||||||
private int _priority;
|
private int _priority;
|
||||||
private string _title;
|
private string _title;
|
||||||
private string _path;
|
private string _path;
|
||||||
private object _executionConditionState;
|
private string _executionConditionState;
|
||||||
private object _nextState;
|
private string _nextState;
|
||||||
private string _scenario;
|
private string _scenario;
|
||||||
private readonly RegistrationCallback _registrationCallback;
|
private readonly RegistrationCallback _registrationCallback;
|
||||||
private readonly IRequestMatcher _requestMatcher;
|
private readonly IRequestMatcher _requestMatcher;
|
||||||
@@ -87,25 +87,20 @@ namespace WireMock.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <see cref="IRespondWithAProvider.WhenStateIs"/>
|
/// <see cref="IRespondWithAProvider.WhenStateIs"/>
|
||||||
public IRespondWithAProvider WhenStateIs(object state)
|
public IRespondWithAProvider WhenStateIs(string state)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(_scenario))
|
if (string.IsNullOrEmpty(_scenario))
|
||||||
{
|
{
|
||||||
throw new NotSupportedException("Unable to set state condition when no scenario is defined.");
|
throw new NotSupportedException("Unable to set state condition when no scenario is defined.");
|
||||||
}
|
}
|
||||||
|
|
||||||
//if (_nextState != null)
|
|
||||||
//{
|
|
||||||
// throw new NotSupportedException("Unable to set state condition when next state is defined.");
|
|
||||||
//}
|
|
||||||
|
|
||||||
_executionConditionState = state;
|
_executionConditionState = state;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <see cref="IRespondWithAProvider.WillSetStateTo"/>
|
/// <see cref="IRespondWithAProvider.WillSetStateTo"/>
|
||||||
public IRespondWithAProvider WillSetStateTo(object state)
|
public IRespondWithAProvider WillSetStateTo(string state)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(_scenario))
|
if (string.IsNullOrEmpty(_scenario))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Net;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
@@ -12,7 +13,7 @@ namespace WireMock.Net.Tests
|
|||||||
public class StatefulBehaviorTests
|
public class StatefulBehaviorTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_skip_non_relevant_states()
|
public async Task Scenarios_Should_skip_non_relevant_states()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
@@ -33,7 +34,7 @@ namespace WireMock.Net.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Should_process_request_if_equals_state_and_single_state_defined()
|
public async Task Scenarios_Should_process_request_if_equals_state_and_single_state_defined()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
@@ -62,7 +63,7 @@ namespace WireMock.Net.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Scenario_and_State_TodoList_Example()
|
public async Task Scenarios_TodoList_Example()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
@@ -86,22 +87,39 @@ namespace WireMock.Net.Tests
|
|||||||
.WhenStateIs("Cancel newspaper item added")
|
.WhenStateIs("Cancel newspaper item added")
|
||||||
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
.RespondWith(Response.Create().WithBody("Buy milk;Cancel newspaper subscription"));
|
||||||
|
|
||||||
|
Check.That(server.Scenarios.Any()).IsFalse();
|
||||||
|
|
||||||
// Act and Assert
|
// Act and Assert
|
||||||
string url = "http://localhost:" + server.Ports[0];
|
string url = "http://localhost:" + server.Ports[0];
|
||||||
string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
string getResponse1 = new HttpClient().GetStringAsync(url + "/todo/items").Result;
|
||||||
Check.That(getResponse1).Equals("Buy milk");
|
Check.That(getResponse1).Equals("Buy milk");
|
||||||
|
|
||||||
|
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||||
|
Check.That(server.Scenarios["To do list"].NextState).IsEqualTo("TodoList State Started");
|
||||||
|
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||||
|
Check.That(server.Scenarios["To do list"].Finished).IsFalse();
|
||||||
|
|
||||||
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription"));
|
var postResponse = await new HttpClient().PostAsync(url + "/todo/items", new StringContent("Cancel newspaper subscription"));
|
||||||
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
Check.That(postResponse.StatusCode).Equals(HttpStatusCode.Created);
|
||||||
|
|
||||||
|
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||||
|
Check.That(server.Scenarios["To do list"].NextState).IsEqualTo("Cancel newspaper item added");
|
||||||
|
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||||
|
Check.That(server.Scenarios["To do list"].Finished).IsFalse();
|
||||||
|
|
||||||
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
string getResponse2 = await new HttpClient().GetStringAsync(url + "/todo/items");
|
||||||
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
Check.That(getResponse2).Equals("Buy milk;Cancel newspaper subscription");
|
||||||
|
|
||||||
|
Check.That(server.Scenarios["To do list"].Name).IsEqualTo("To do list");
|
||||||
|
Check.That(server.Scenarios["To do list"].NextState).IsNull();
|
||||||
|
Check.That(server.Scenarios["To do list"].Started).IsTrue();
|
||||||
|
Check.That(server.Scenarios["To do list"].Finished).IsTrue();
|
||||||
|
|
||||||
server.Dispose();
|
server.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
// [Fact]
|
[Fact]
|
||||||
public async Task Should_process_request_if_equals_state_and_multiple_state_defined()
|
public async Task Scenarios_Should_process_request_if_equals_state_and_multiple_state_defined()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
var server = FluentMockServer.Start();
|
var server = FluentMockServer.Start();
|
||||||
@@ -113,7 +131,7 @@ namespace WireMock.Net.Tests
|
|||||||
.RespondWith(Response.Create().WithBody("No state msg 1"));
|
.RespondWith(Response.Create().WithBody("No state msg 1"));
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create().WithPath("/fooX").UsingGet())
|
.Given(Request.Create().WithPath("/foo1X").UsingGet())
|
||||||
.InScenario("s1")
|
.InScenario("s1")
|
||||||
.WhenStateIs("Test state 1")
|
.WhenStateIs("Test state 1")
|
||||||
.RespondWith(Response.Create().WithBody("Test state msg 1"));
|
.RespondWith(Response.Create().WithBody("Test state msg 1"));
|
||||||
@@ -125,7 +143,7 @@ namespace WireMock.Net.Tests
|
|||||||
.RespondWith(Response.Create().WithBody("No state msg 2"));
|
.RespondWith(Response.Create().WithBody("No state msg 2"));
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create().WithPath("/fooX").UsingGet())
|
.Given(Request.Create().WithPath("/foo2X").UsingGet())
|
||||||
.InScenario("s2")
|
.InScenario("s2")
|
||||||
.WhenStateIs("Test state 2")
|
.WhenStateIs("Test state 2")
|
||||||
.RespondWith(Response.Create().WithBody("Test state msg 2"));
|
.RespondWith(Response.Create().WithBody("Test state msg 2"));
|
||||||
@@ -138,10 +156,10 @@ namespace WireMock.Net.Tests
|
|||||||
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2");
|
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2");
|
||||||
Check.That(responseNoState2).Equals("No state msg 2");
|
Check.That(responseNoState2).Equals("No state msg 2");
|
||||||
|
|
||||||
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/fooX");
|
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/foo1X");
|
||||||
Check.That(responseWithState1).Equals("Test state msg 1");
|
Check.That(responseWithState1).Equals("Test state msg 1");
|
||||||
|
|
||||||
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/fooX");
|
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/foo2X");
|
||||||
Check.That(responseWithState2).Equals("Test state msg 2");
|
Check.That(responseWithState2).Equals("Test state msg 2");
|
||||||
|
|
||||||
server.Dispose();
|
server.Dispose();
|
||||||
|
|||||||
Reference in New Issue
Block a user