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