scenario and state

This commit is contained in:
Stef Heyenrath
2017-10-07 13:19:48 +02:00
parent ab017beaa6
commit 5424b1e2e2
10 changed files with 416 additions and 257 deletions

View File

@@ -1,124 +1,141 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
namespace WireMock namespace WireMock
{ {
/// <summary> /// <summary>
/// The Mapping. /// The Mapping.
/// </summary> /// </summary>
public class Mapping 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> /// <summary>
/// Execution state condition for the current mapping. /// Gets the unique identifier.
/// </summary> /// </summary>
public object ExecutionConditionState { get; } /// <value>
/// The unique identifier.
/// </value>
public Guid Guid { get; }
/// <summary> /// <summary>
/// The next state which will be signaled after the current mapping execution. /// 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. /// In case the value is null state will not be changed.
/// </summary> /// </summary>
public object NextState { get; } [CanBeNull]
public object NextState { get; }
/// <summary>
/// The Request matcher. /// <summary>
/// </summary> /// The Request matcher.
public IRequestMatcher RequestMatcher { get; } /// </summary>
public IRequestMatcher RequestMatcher { get; }
/// <summary>
/// The Provider. /// <summary>
/// </summary> /// The Provider.
public IResponseProvider Provider { get; } /// </summary>
public IResponseProvider Provider { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Mapping"/> class. /// <summary>
/// </summary> /// Is State started ?
/// <param name="guid">The unique identifier.</param> /// </summary>
/// <param name="title">The unique title (can be null_.</param> public bool IsStartState => Scenario == null || Scenario != null && NextState != null && ExecutionConditionState == null;
/// <param name="requestMatcher">The request matcher.</param>
/// <param name="provider">The provider.</param> /// <summary>
/// <param name="priority">The priority for this mapping.</param> /// Initializes a new instance of the <see cref="Mapping"/> class.
public Mapping(Guid guid, [CanBeNull] string title, IRequestMatcher requestMatcher, IResponseProvider provider, int priority) /// </summary>
: this(guid, title, requestMatcher, provider, priority, null, null) /// <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>
/// <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="priority">The priority for this mapping.</param>
/// <param name="executionConditionState">State in which the current mapping can occur. Happens if not null</param> /// <param name="scenario">The scenario. [Optional]</param>
/// <param name="nextState">The next state which will occur after the current mapping execution. Happens if not null</param> /// <param name="executionConditionState">State in which the current mapping can occur. [Optional]</param>
public Mapping(Guid guid, [CanBeNull] string title, IRequestMatcher requestMatcher, IResponseProvider provider, int priority, object executionConditionState, object nextState) /// <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)
Priority = priority; {
ExecutionConditionState = executionConditionState; Guid = guid;
NextState = nextState; Title = title;
Guid = guid; RequestMatcher = requestMatcher;
Title = title; Provider = provider;
RequestMatcher = requestMatcher; Priority = priority;
Provider = provider; Scenario = scenario;
} ExecutionConditionState = executionConditionState;
NextState = nextState;
/// <summary> }
/// The response to.
/// </summary> /// <summary>
/// <param name="requestMessage">The request message.</param> /// The response to.
/// <returns>The <see cref="ResponseMessage"/>.</returns> /// </summary>
public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage) /// <param name="requestMessage">The request message.</param>
{ /// <returns>The <see cref="ResponseMessage"/>.</returns>
return await Provider.ProvideResponseAsync(requestMessage); public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage)
} {
return await Provider.ProvideResponseAsync(requestMessage);
/// <summary> }
/// Determines whether the RequestMessage is handled.
/// </summary> /// <summary>
/// <param name="requestMessage">The request message.</param> /// Gets the RequestMatchResult based on the RequestMessage.
/// <returns>The <see cref="RequestMatchResult"/>.</returns> /// </summary>
public RequestMatchResult IsRequestHandled(RequestMessage requestMessage) /// <param name="requestMessage">The request message.</param>
{ /// <param name="nextState">The Next State.</param>
var result = new RequestMatchResult(); /// <returns>The <see cref="RequestMatchResult"/>.</returns>
public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] object nextState)
RequestMatcher.GetMatchingScore(requestMessage, result); {
var result = new RequestMatchResult();
return result;
} RequestMatcher.GetMatchingScore(requestMessage, result);
/// <summary> // Only check state if Scenario is defined
/// Gets a value indicating whether this mapping is an Admin Interface. if (Scenario != null)
/// </summary> {
/// <value> var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState);
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>. matcher.GetMatchingScore(requestMessage, result);
/// </value> //// If ExecutionConditionState is null, this means that request is the start from a scenario. So just return.
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider; //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;
}
} }

View File

@@ -43,7 +43,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Gets the match details. /// Gets the match details.
/// </summary> /// </summary>
public IList<KeyValuePair<Type, double>> MatchDetails { get; private set; } public IList<KeyValuePair<Type, double>> MatchDetails { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMatchResult"/> class. /// Initializes a new instance of the <see cref="RequestMatchResult"/> class.
@@ -72,7 +72,6 @@ namespace WireMock.Matchers.Request
/// <returns> /// <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. /// 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> /// </returns>
/// <exception cref="System.NotImplementedException"></exception>
public int CompareTo(object obj) public int CompareTo(object obj)
{ {
var compareObj = (RequestMatchResult)obj; var compareObj = (RequestMatchResult)obj;

View File

@@ -0,0 +1,51 @@
using JetBrains.Annotations;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The scenario and state matcher.
/// </summary>
public class RequestMessageScenarioAndStateMatcher : IRequestMatcher
{
///// <summary>
///// Scenario.
///// </summary>
//[CanBeNull] private string _scenario;
/// <summary>
/// Execution state condition for the current mapping.
/// </summary>
[CanBeNull]
private readonly object _executionConditionState;
/// <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]
private readonly object _nextState;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageScenarioAndStateMatcher"/> class.
/// </summary>
/// <param name="nextState">The next state.</param>
/// <param name="executionConditionState">Execution state condition for the current mapping.</param>
public RequestMessageScenarioAndStateMatcher([CanBeNull] object nextState, [CanBeNull] object executionConditionState)
{
_nextState = nextState;
_executionConditionState = executionConditionState;
}
/// <inheritdoc />
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch();
return requestMatchResult.AddScore(GetType(), score);
}
private double IsMatch()
{
return Equals(_executionConditionState, _nextState) ? MatchScores.Perfect : MatchScores.Mismatch;
}
}
}

View File

@@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace WireMock.Owin namespace WireMock.Owin

View File

@@ -1,9 +1,12 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using WireMock.Logging; using WireMock.Logging;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using System.Linq; using System.Linq;
using WireMock.Matchers;
#if !NETSTANDARD #if !NETSTANDARD
using Microsoft.Owin; using Microsoft.Owin;
#else #else
@@ -24,19 +27,17 @@ namespace WireMock.Owin
private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper(); private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper(); private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
public object State { get; private set; } private readonly IDictionary<string, object> _states = new ConcurrentDictionary<string, object>();
#if !NETSTANDARD #if !NETSTANDARD
public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next) public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)
{ {
_options = options; _options = options;
State = null;
} }
#else #else
public WireMockMiddleware(RequestDelegate next, WireMockMiddlewareOptions options) public WireMockMiddleware(RequestDelegate next, WireMockMiddlewareOptions options)
{ {
_options = options; _options = options;
State = null;
} }
#endif #endif
@@ -54,12 +55,20 @@ namespace WireMock.Owin
RequestMatchResult requestMatchResult = null; RequestMatchResult requestMatchResult = null;
try try
{ {
foreach (var mapping in _options.Mappings.Where(m => m.Scenario != null))
{
// Set start
if (!_states.ContainsKey(mapping.Scenario) && mapping.IsStartState)
{
_states.Add(mapping.Scenario, null);
}
}
var mappings = _options.Mappings var mappings = _options.Mappings
.Where(m => object.Equals(m.ExecutionConditionState, State))
.Select(m => new .Select(m => new
{ {
Mapping = m, Mapping = m,
MatchResult = m.IsRequestHandled(request) MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _states.ContainsKey(m.Scenario) ? _states[m.Scenario] : null)
}) })
.ToList(); .ToList();
@@ -92,14 +101,13 @@ namespace WireMock.Owin
response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" }; response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" };
return; return;
} }
logRequest = !targetMapping.IsAdminInterface; logRequest = !targetMapping.IsAdminInterface;
if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null) if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
{ {
string authorization; bool present = request.Headers.TryGetValue("Authorization", out var authorization);
bool present = request.Headers.TryGetValue("Authorization", out authorization); if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < MatchScores.Perfect)
if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < 1.0)
{ {
response = new ResponseMessage { StatusCode = 401 }; response = new ResponseMessage { StatusCode = 401 };
return; return;
@@ -112,7 +120,11 @@ namespace WireMock.Owin
} }
response = await targetMapping.ResponseToAsync(request); response = await targetMapping.ResponseToAsync(request);
State = targetMapping.NextState;
if (targetMapping.Scenario != null)
{
_states[targetMapping.Scenario] = targetMapping.NextState;
}
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -69,9 +69,8 @@ namespace WireMock.Server
Check.NotNull(filename, nameof(filename)); Check.NotNull(filename, nameof(filename));
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
Guid guidFromFilename;
if (Guid.TryParse(filenameWithoutExtension, out guidFromFilename)) if (Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
{ {
DeserializeAndAddMapping(File.ReadAllText(filename), guidFromFilename); DeserializeAndAddMapping(File.ReadAllText(filename), guidFromFilename);
} }
@@ -151,7 +150,7 @@ namespace WireMock.Server
var response = (Response)Response.Create(responseMessage); var response = (Response)Response.Create(responseMessage);
return new Mapping(Guid.NewGuid(), string.Empty, request, response, 0); return new Mapping(Guid.NewGuid(), string.Empty, request, response, 0, null, null, null);
} }
#endregion #endregion

View File

@@ -1,58 +1,65 @@
using System; using System;
namespace WireMock.Server namespace WireMock.Server
{ {
/// <summary> /// <summary>
/// IRespondWithAProvider /// IRespondWithAProvider
/// </summary> /// </summary>
public interface IRespondWithAProvider public interface IRespondWithAProvider
{ {
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define a unique title for this mapping.
/// </summary>
/// <param name="title">The unique title.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithTitle(string title);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(string guid);
/// <summary>
/// Define the priority for this mapping.
/// </summary>
/// <param name="priority">The priority.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider AtPriority(int priority);
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">The provider.</param>
void RespondWith(IResponseProvider provider);
/// <summary>
/// Execute this respond only in case the current state is equal to specified one
/// </summary>
/// <param name="state">Any object which identifies the current state</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WhenStateIs(object state);
/// <summary> /// <summary>
/// Once this mapping is executed the state will be changed to specified one /// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define a unique title for this mapping.
/// </summary>
/// <param name="title">The unique title.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithTitle(string title);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(string guid);
/// <summary>
/// Define the priority for this mapping.
/// </summary>
/// <param name="priority">The priority.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider AtPriority(int priority);
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">The provider.</param>
void RespondWith(IResponseProvider provider);
/// <summary>
/// Sets the the scenario.
/// </summary>
/// <param name="scenario">The scenario.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider InScenario(string scenario);
/// <summary>
/// Execute this respond only in case the current state is equal to specified one.
/// </summary>
/// <param name="state">Any object which identifies the current state</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WhenStateIs(object state);
/// <summary>
/// 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(object state);
} }
} }

View File

@@ -11,9 +11,9 @@ namespace WireMock.Server
private int _priority; private int _priority;
private Guid? _guid; private Guid? _guid;
private string _title; private string _title;
private object _executionConditionState;
private object _executionConditionState = null; private object _nextState;
private object _nextState = null; private string _scenario;
/// <summary> /// <summary>
/// The _registration callback. /// The _registration callback.
@@ -45,19 +45,7 @@ namespace WireMock.Server
public void RespondWith(IResponseProvider provider) public void RespondWith(IResponseProvider provider)
{ {
var mappingGuid = _guid ?? Guid.NewGuid(); var mappingGuid = _guid ?? Guid.NewGuid();
_registrationCallback(new Mapping(mappingGuid, _title, _requestMatcher, provider, _priority, _executionConditionState, _nextState)); _registrationCallback(new Mapping(mappingGuid, _title, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState));
}
public IRespondWithAProvider WhenStateIs(object state)
{
_executionConditionState = state;
return this;
}
public IRespondWithAProvider WillSetStateTo(object state)
{
_nextState = state;
return this;
} }
/// <summary> /// <summary>
@@ -105,5 +93,41 @@ namespace WireMock.Server
return this; return this;
} }
public IRespondWithAProvider InScenario(string scenario)
{
_scenario = scenario;
return this;
}
public IRespondWithAProvider WhenStateIs(object state)
{
if (string.IsNullOrEmpty(_scenario))
{
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;
return this;
}
public IRespondWithAProvider WillSetStateTo(object state)
{
if (string.IsNullOrEmpty(_scenario))
{
throw new NotSupportedException("Unable to set next state when no scenario is defined.");
}
_nextState = state;
return this;
}
} }
} }

View File

@@ -24,10 +24,9 @@ namespace WireMock.Net.Tests
.Given(Request.Create() .Given(Request.Create()
.WithPath("/foo") .WithPath("/foo")
.UsingGet()) .UsingGet())
.InScenario("s")
.WhenStateIs("Test state") .WhenStateIs("Test state")
.RespondWith(Response.Create() .RespondWith(Response.Create());
.WithStatusCode(200)
.WithBody(@"{ msg: ""Hello world!""}"));
// when // when
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo"); var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -37,7 +36,7 @@ namespace WireMock.Net.Tests
} }
[Fact] [Fact]
public async Task Should_process_request_if_equals_state() public async Task Should_process_request_if_equals_state_and_single_state_defined()
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
@@ -46,19 +45,19 @@ namespace WireMock.Net.Tests
.Given(Request.Create() .Given(Request.Create()
.WithPath("/foo") .WithPath("/foo")
.UsingGet()) .UsingGet())
.InScenario("s")
.WillSetStateTo("Test state") .WillSetStateTo("Test state")
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(200) .WithBody("No state msg"));
.WithBody(@"No state msg"));
_server _server
.Given(Request.Create() .Given(Request.Create()
.WithPath("/foo") .WithPath("/foo")
.UsingGet()) .UsingGet())
.InScenario("s")
.WhenStateIs("Test state") .WhenStateIs("Test state")
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(200) .WithBody("Test state msg"));
.WithBody(@"Test state msg"));
// when // when
var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo"); var responseNoState = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
@@ -69,9 +68,66 @@ namespace WireMock.Net.Tests
Check.That(responseWithState).Equals("Test state msg"); Check.That(responseWithState).Equals("Test state msg");
} }
[Fact]
public async Task Should_process_request_if_equals_state_and_multiple_state_defined()
{
// given
_server = FluentMockServer.Start();
_server
.Given(Request.Create()
.WithPath("/state1")
.UsingGet())
.InScenario("s1")
.WillSetStateTo("Test state 1")
.RespondWith(Response.Create()
.WithBody("No state msg 1"));
_server
.Given(Request.Create()
.WithPath("/foo")
.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("/foo")
.UsingGet())
.InScenario("s2")
.WhenStateIs("Test state 2")
.RespondWith(Response.Create()
.WithBody("Test state msg 2"));
// when
string url = "http://localhost:" + _server.Ports[0];
var responseNoState1 = await new HttpClient().GetStringAsync(url + "/state1");
var responseNoState2 = await new HttpClient().GetStringAsync(url + "/state2");
var responseWithState1 = await new HttpClient().GetStringAsync(url + "/foo");
var responseWithState2 = await new HttpClient().GetStringAsync(url + "/foo");
// then
Check.That(responseNoState1).Equals("No state msg 1");
Check.That(responseWithState1).Equals("Test state msg 1");
Check.That(responseNoState2).Equals("No state msg 2");
Check.That(responseWithState2).Equals("Test state msg 2");
}
public void Dispose() public void Dispose()
{ {
_server?.Dispose(); _server?.Dispose();
} }
} }
} }

View File

@@ -1,49 +1,44 @@
using System; //using Microsoft.Owin;
using System.Collections.Generic; //using Moq;
using System.Linq; //using NFluent;
using System.Text; //using WireMock.Owin;
using System.Threading.Tasks; //using Xunit;
using Microsoft.Owin;
using Moq;
using NFluent;
using WireMock.Owin;
using Xunit;
namespace WireMock.Net.Tests //namespace WireMock.Net.Tests
{ //{
public class WireMockMiddlewareTests // public class WireMockMiddlewareTests
{ // {
private readonly ObjectMother _objectMother = new ObjectMother(); // private readonly ObjectMother _objectMother = new ObjectMother();
[Fact] // [Fact]
public void Should_have_default_state_as_null() // public void Should_have_default_state_as_null()
{ // {
// given // // given
// when // // when
var sut = _objectMother.Create(); // var sut = _objectMother.Create();
// then // // then
Check.That(sut.State).IsNull(); // Check.That(sut.States).IsNull();
} // }
internal class ObjectMother // private class ObjectMother
{ // {
public Mock<OwinMiddleware> OwinMiddleware { get; set; } // private Mock<OwinMiddleware> OwinMiddleware { get; }
public Mock<IOwinContext> OwinContext { get; set; } // private Mock<IOwinContext> OwinContext { get; }
public WireMockMiddlewareOptions WireMockMiddlewareOptions { get; set; } // private WireMockMiddlewareOptions WireMockMiddlewareOptions { get; }
public ObjectMother() // public ObjectMother()
{ // {
OwinContext = new Mock<IOwinContext>(); // OwinContext = new Mock<IOwinContext>();
OwinMiddleware = new Mock<OwinMiddleware>(null); // OwinMiddleware = new Mock<OwinMiddleware>(null);
WireMockMiddlewareOptions = new WireMockMiddlewareOptions(); // WireMockMiddlewareOptions = new WireMockMiddlewareOptions();
} // }
public WireMockMiddleware Create() // public WireMockMiddleware Create()
{ // {
return new WireMockMiddleware(OwinMiddleware.Object, WireMockMiddlewareOptions); // return new WireMockMiddleware(OwinMiddleware.Object, WireMockMiddlewareOptions);
} // }
} // }
} // }
} //}