# 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: | 90
Uncovered lines: | 34
Coverable lines: | 124
Total lines: | 194
Line coverage: | 72.5%
Branch coverage: | 64.5%

# Metrics

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
LogRequest(...) | 0 | 0 | 0.593 | 0.583
.ctor(...) | 0 | 0 | 1 | 0
.cctor() | 0 | 0 | 1 | 0
Invoke() | 0 | 0 | 0.756 | 0.676

# File(s)

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

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

# Methods/Properties

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