Summary

Class:WireMock.Owin.WireMockMiddleware
Assembly:WireMock.Net
File(s):C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\WireMockMiddleware.cs
Covered lines:60
Uncovered lines:21
Coverable lines:81
Total lines:144
Line coverage:74%
Branch coverage:66.6%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor(...)10100100
LogRequest(...)20100100
.cctor()10100100
Invoke()443276867.3967.74

File(s)

C:\Users\Stef\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\WireMockMiddleware.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Threading.Tasks;
 4using WireMock.Logging;
 5using WireMock.Matchers.Request;
 6using System.Linq;
 7#if NET45
 8using Microsoft.Owin;
 9#else
 10using Microsoft.AspNetCore.Http;
 11#endif
 12
 13namespace WireMock.Owin
 14{
 15#if NET45
 16    internal class WireMockMiddleware : OwinMiddleware
 17#else
 18    internal class WireMockMiddleware
 19#endif
 20    {
 121        private static readonly Task CompletedTask = Task.FromResult(false);
 22        private readonly WireMockMiddlewareOptions _options;
 23
 1624        private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();
 1625        private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
 26
 27#if NET45
 1628        public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)
 1629        {
 1630            _options = options;
 1631        }
 32#else
 33        public WireMockMiddleware(RequestDelegate next, WireMockMiddlewareOptions options)
 34        {
 35            _options = options;
 36        }
 37#endif
 38
 39#if NET45
 40        public override async Task Invoke(IOwinContext ctx)
 41#else
 42        public async Task Invoke(HttpContext ctx)
 43#endif
 1344        {
 1345             if (_options.RequestProcessingDelay > TimeSpan.Zero)
 146            {
 147                await Task.Delay(_options.RequestProcessingDelay.Value);
 48                // Thread.Sleep(_options.RequestProcessingDelay.Value);
 149            }
 50
 1351            var request = await _requestMapper.MapAsync(ctx.Request);
 52
 1353            ResponseMessage response = null;
 1354            Mapping targetMapping = null;
 1355             RequestMatchResult requestMatchResult = null;
 56            try
 1357            {
 1358                var mappings = _options.Mappings
 2359                    .Select(m => new
 2360                    {
 2361                        Mapping = m,
 2362                        MatchResult = m.IsRequestHandled(request)
 2363                    })
 1364                    .ToList();
 65
 1366                 if (_options.AllowPartialMapping)
 067                {
 068                    var partialMappings = mappings
 069                        .Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdmin
 070                        .OrderBy(m => m.MatchResult)
 071                        .ThenBy(m => m.Mapping.Priority)
 072                        .ToList();
 73
 074                    var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0);
 75
 076                     targetMapping = bestPartialMatch?.Mapping;
 077                     requestMatchResult = bestPartialMatch?.MatchResult;
 078                }
 79                else
 1380                {
 1381                    var perfectMatch = mappings
 2382                        .OrderBy(m => m.Mapping.Priority)
 2183                        .FirstOrDefault(m => m.MatchResult.IsPerfectMatch);
 84
 1385                     targetMapping = perfectMatch?.Mapping;
 1386                     requestMatchResult = perfectMatch?.MatchResult;
 1387                }
 88
 1389                 if (targetMapping == null)
 690                {
 691                    response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" };
 692                    return;
 93                }
 94
 795                 if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
 096                {
 97                    string authorization;
 098                    bool present = request.Headers.TryGetValue("Authorization", out authorization);
 099                     if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < 1.0)
 0100                    {
 0101                        response = new ResponseMessage { StatusCode = 401 };
 0102                        return;
 103                    }
 0104                }
 105
 7106                response = await targetMapping.ResponseTo(request);
 7107            }
 0108            catch (Exception ex)
 0109            {
 0110                response = new ResponseMessage { StatusCode = 500, Body = ex.ToString() };
 0111            }
 112            finally
 13113            {
 13114                 var log = new LogEntry
 13115                {
 13116                    Guid = Guid.NewGuid(),
 13117                    RequestMessage = request,
 13118                    ResponseMessage = response,
 13119                    MappingGuid = targetMapping?.Guid,
 13120                    MappingTitle = targetMapping?.Title,
 13121                    RequestMatchResult = requestMatchResult
 13122                };
 123
 13124                LogRequest(log);
 125
 13126                await _responseMapper.MapAsync(response, ctx.Response);
 13127             }
 128
 7129            await CompletedTask;
 13130        }
 131
 132        /// <summary>
 133        /// The log request.
 134        /// </summary>
 135        /// <param name="entry">The request.</param>
 136        private void LogRequest(LogEntry entry)
 13137        {
 13138            lock (((ICollection)_options.LogEntries).SyncRoot)
 13139            {
 13140                _options.LogEntries.Add(entry);
 13141            }
 13142        }
 143    }
 144}