# WireMock.Owin.WireMockMiddleware - Coverage Report

# Summary

Class: | WireMock.Owin.WireMockMiddleware
Assembly: | WireMock.Net
File(s): | C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\WireMockMiddleware.cs
Covered lines: | 84
Uncovered lines: | 34
Coverable lines: | 118
Total lines: | 185
Line coverage: | 71.1%
Branch coverage: | 69.2%

# Metrics

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
.ctor(...) | 1 | 0 | 100 | 100
LogRequest(...) | 7 | 64 | 56.67 | 61.54
.cctor() | 1 | 0 | 100 | 100
Invoke() | 52 | 262144 | 74.24 | 72.97

# File(s)

## C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\WireMockMiddleware.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Threading.Tasks;`
|  | `3` |  | `using WireMock.Logging;`
|  | `4` |  | `using WireMock.Matchers.Request;`
|  | `5` |  | `using System.Linq;`
|  | `6` |  | `using WireMock.Matchers;`
|  | `7` |  | `using WireMock.Util;`
|  | `8` |  | `using Newtonsoft.Json;`
|  | `9` |  | `using WireMock.Http;`
|  | `10` |  | `#if !NETSTANDARD`
|  | `11` |  | `using Microsoft.Owin;`
|  | `12` |  | `#else`
|  | `13` |  | `using Microsoft.AspNetCore.Http;`
|  | `14` |  | `#endif`
|  | `15` |  | ``
|  | `16` |  | `namespace WireMock.Owin`
|  | `17` |  | `{`
|  | `18` |  | `#if !NETSTANDARD`
|  | `19` |  | `internal class WireMockMiddleware : OwinMiddleware`
|  | `20` |  | `#else`
|  | `21` |  | `internal class WireMockMiddleware`
|  | `22` |  | `#endif`
|  | `23` |  | `{`
| 1 | `24` |  | `private static readonly Task CompletedTask = Task.FromResult(false);`
|  | `25` |  | `private readonly WireMockMiddlewareOptions _options;`
|  | `26` |  | ``
| 47 | `27` |  | `private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();`
| 47 | `28` |  | `private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();`
|  | `29` |  | ``
|  | `30` |  | `#if !NETSTANDARD`
| 47 | `31` |  | `public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)`
| 47 | `32` |  | `{`
| 47 | `33` |  | `_options = options;`
| 47 | `34` |  | `}`
|  | `35` |  | `#else`
|  | `36` |  | `public WireMockMiddleware(RequestDelegate next, WireMockMiddlewareOptions options)`
|  | `37` |  | `{`
|  | `38` |  | `_options = options;`
|  | `39` |  | `}`
|  | `40` |  | `#endif`
|  | `41` |  | ``
|  | `42` |  | `#if !NETSTANDARD`
|  | `43` |  | `public override async Task Invoke(IOwinContext ctx)`
|  | `44` |  | `#else`
|  | `45` |  | `public async Task Invoke(HttpContext ctx)`
|  | `46` |  | `#endif`
| 58 | `47` |  | `{`
| 58 | `48` |  | `var request = await _requestMapper.MapAsync(ctx.Request);`
|  | `49` |  | ``
| 58 | `50` |  | `bool logRequest = false;`
| 58 | `51` |  | `ResponseMessage response = null;`
| 58 | `52` |  | `Mapping targetMapping = null;`
| 58 | `53` |  | `RequestMatchResult requestMatchResult = null;`
|  | `54` |  | `try`
| 58 | `55` |  | `{`
| 352 | `56` |  | `foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))`
| 14 | `57` |  | `{`
|  | `58` |  | `// Set start`
| 14 | `59` |  | `if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)`
| 2 | `60` |  | `{`
| 2 | `61` |  | `_options.Scenarios.Add(mapping.Scenario, null);`
| 2 | `62` |  | `}`
| 14 | `63` |  | `}`
|  | `64` |  | ``
| 58 | `65` |  | `var mappings = _options.Mappings.Values`
| 208 | `66` |  | `.Select(m => new`
| 208 | `67` |  | `{`
| 208 | `68` |  | `Mapping = m,`
| 208 | `69` |  | `MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.Contains`
| 208 | `70` |  | `})`
| 58 | `71` |  | `.ToList();`
|  | `72` |  | ``
| 58 | `73` |  | `if (_options.AllowPartialMapping)`
| 0 | `74` |  | `{`
| 0 | `75` |  | `var partialMappings = mappings`
| 0 | `76` |  | `.Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdmin`
| 0 | `77` |  | `.OrderBy(m => m.MatchResult)`
| 0 | `78` |  | `.ThenBy(m => m.Mapping.Priority)`
| 0 | `79` |  | `.ToList();`
|  | `80` |  | ``
| 0 | `81` |  | `var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0);`
|  | `82` |  | ``
| 0 | `83` |  | `targetMapping = bestPartialMatch?.Mapping;`
| 0 | `84` |  | `requestMatchResult = bestPartialMatch?.MatchResult;`
| 0 | `85` |  | `}`
|  | `86` |  | `else`
| 58 | `87` |  | `{`
| 58 | `88` |  | `var perfectMatch = mappings`
| 208 | `89` |  | `.OrderBy(m => m.Mapping.Priority)`
| 156 | `90` |  | `.FirstOrDefault(m => m.MatchResult.IsPerfectMatch);`
|  | `91` |  | ``
| 58 | `92` |  | `targetMapping = perfectMatch?.Mapping;`
| 58 | `93` |  | `requestMatchResult = perfectMatch?.MatchResult;`
| 58 | `94` |  | `}`
|  | `95` |  | ``
| 58 | `96` |  | `if (targetMapping == null)`
| 12 | `97` |  | `{`
| 12 | `98` |  | `logRequest = true;`
| 12 | `99` |  | `_options.Logger.Warn("HttpStatusCode set to 404 : No matching mapping found");`
| 12 | `100` |  | `response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" };`
| 12 | `101` |  | `return;`
|  | `102` |  | `}`
|  | `103` |  | ``
| 46 | `104` |  | `logRequest = !targetMapping.IsAdminInterface;`
|  | `105` |  | ``
| 46 | `106` |  | `if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)`
| 0 | `107` |  | `{`
| 0 | `108` |  | `bool present = request.Headers.TryGetValue(HttpKnownHeaderNames.Authorization, out WireMockList<stri`
| 0 | `109` |  | `if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfec`
| 0 | `110` |  | `{`
| 0 | `111` |  | `_options.Logger.Error("HttpStatusCode set to 401");`
| 0 | `112` |  | `response = new ResponseMessage { StatusCode = 401 };`
| 0 | `113` |  | `return;`
|  | `114` |  | `}`
| 0 | `115` |  | `}`
|  | `116` |  | ``
| 46 | `117` |  | `if (!targetMapping.IsAdminInterface && _options.RequestProcessingDelay > TimeSpan.Zero)`
| 1 | `118` |  | `{`
| 1 | `119` |  | `await Task.Delay(_options.RequestProcessingDelay.Value);`
| 1 | `120` |  | `}`
|  | `121` |  | ``
| 46 | `122` |  | `response = await targetMapping.ResponseToAsync(request);`
|  | `123` |  | ``
| 46 | `124` |  | `if (targetMapping.Scenario != null)`
| 5 | `125` |  | `{`
| 5 | `126` |  | `_options.Scenarios[targetMapping.Scenario] = targetMapping.NextState;`
| 5 | `127` |  | `}`
| 46 | `128` |  | `}`
| 0 | `129` |  | `catch (Exception ex)`
| 0 | `130` |  | `{`
| 0 | `131` |  | `_options.Logger.Error("HttpStatusCode set to 500");`
| 0 | `132` |  | `response = new ResponseMessage { StatusCode = 500, Body = JsonConvert.SerializeObject(ex) };`
| 0 | `133` |  | `}`
|  | `134` |  | `finally`
| 58 | `135` |  | `{`
| 58 | `136` |  | `var log = new LogEntry`
| 58 | `137` |  | `{`
| 58 | `138` |  | `Guid = Guid.NewGuid(),`
| 58 | `139` |  | `RequestMessage = request,`
| 58 | `140` |  | `ResponseMessage = response,`
| 58 | `141` |  | `MappingGuid = targetMapping?.Guid,`
| 58 | `142` |  | `MappingTitle = targetMapping?.Title,`
| 58 | `143` |  | `RequestMatchResult = requestMatchResult`
| 58 | `144` |  | `};`
|  | `145` |  | ``
| 58 | `146` |  | `LogRequest(log, logRequest);`
|  | `147` |  | ``
| 58 | `148` |  | `await _responseMapper.MapAsync(response, ctx.Response);`
| 58 | `149` |  | `}`
|  | `150` |  | ``
| 46 | `151` |  | `await CompletedTask;`
| 58 | `152` |  | `}`
|  | `153` |  | ``
|  | `154` |  | `private void LogRequest(LogEntry entry, bool addRequest)`
| 58 | `155` |  | `{`
| 58 | `156` |  | `if (addRequest)`
| 53 | `157` |  | `{`
| 53 | `158` |  | `_options.LogEntries.Add(entry);`
| 53 | `159` |  | `}`
|  | `160` |  | ``
| 58 | `161` |  | `if (_options.MaxRequestLogCount != null)`
| 3 | `162` |  | `{`
| 3 | `163` |  | `var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;`
| 8 | `164` |  | `for (int i = 0; i < amount; i++)`
| 1 | `165` |  | `{`
| 1 | `166` |  | `_options.LogEntries.RemoveAt(0);`
| 1 | `167` |  | `}`
| 3 | `168` |  | `}`
|  | `169` |  | ``
| 58 | `170` |  | `if (_options.RequestLogExpirationDuration != null)`
| 0 | `171` |  | `{`
| 0 | `172` |  | `var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value);`
|  | `173` |  | ``
| 0 | `174` |  | `for (var i = _options.LogEntries.Count - 1; i >= 0; i--)`
| 0 | `175` |  | `{`
| 0 | `176` |  | `var le = _options.LogEntries[i];`
| 0 | `177` |  | `if (le.RequestMessage.DateTime <= checkTime)`
| 0 | `178` |  | `{`
| 0 | `179` |  | `_options.LogEntries.RemoveAt(i);`
| 0 | `180` |  | `}`
| 0 | `181` |  | `}`
| 0 | `182` |  | `}`
| 58 | `183` |  | `}`
|  | `184` |  | `}`
|  | `185` |  | `}`

# Methods/Properties

.cctor()
.ctor(Microsoft.Owin.OwinMiddleware,WireMock.Owin.WireMockMiddlewareOptions)
Invoke()
LogRequest(WireMock.Logging.LogEntry,System.Boolean)