# 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

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
.ctor(...) | 1 | 0 | 100 | 100
LogRequest(...) | 2 | 0 | 100 | 100
.cctor() | 1 | 0 | 100 | 100
Invoke() | 44 | 32768 | 67.39 | 67.74

# File(s)

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

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Collections;`
|  | `3` |  | `using System.Threading.Tasks;`
|  | `4` |  | `using WireMock.Logging;`
|  | `5` |  | `using WireMock.Matchers.Request;`
|  | `6` |  | `using System.Linq;`
|  | `7` |  | `#if NET45`
|  | `8` |  | `using Microsoft.Owin;`
|  | `9` |  | `#else`
|  | `10` |  | `using Microsoft.AspNetCore.Http;`
|  | `11` |  | `#endif`
|  | `12` |  | ``
|  | `13` |  | `namespace WireMock.Owin`
|  | `14` |  | `{`
|  | `15` |  | `#if NET45`
|  | `16` |  | `internal class WireMockMiddleware : OwinMiddleware`
|  | `17` |  | `#else`
|  | `18` |  | `internal class WireMockMiddleware`
|  | `19` |  | `#endif`
|  | `20` |  | `{`
| 1 | `21` |  | `private static readonly Task CompletedTask = Task.FromResult(false);`
|  | `22` |  | `private readonly WireMockMiddlewareOptions _options;`
|  | `23` |  | ``
| 16 | `24` |  | `private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();`
| 16 | `25` |  | `private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();`
|  | `26` |  | ``
|  | `27` |  | `#if NET45`
| 16 | `28` |  | `public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)`
| 16 | `29` |  | `{`
| 16 | `30` |  | `_options = options;`
| 16 | `31` |  | `}`
|  | `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`
| 13 | `44` |  | `{`
| 13 | `45` |  | `if (_options.RequestProcessingDelay > TimeSpan.Zero)`
| 1 | `46` |  | `{`
| 1 | `47` |  | `await Task.Delay(_options.RequestProcessingDelay.Value);`
|  | `48` |  | `// Thread.Sleep(_options.RequestProcessingDelay.Value);`
| 1 | `49` |  | `}`
|  | `50` |  | ``
| 13 | `51` |  | `var request = await _requestMapper.MapAsync(ctx.Request);`
|  | `52` |  | ``
| 13 | `53` |  | `ResponseMessage response = null;`
| 13 | `54` |  | `Mapping targetMapping = null;`
| 13 | `55` |  | `RequestMatchResult requestMatchResult = null;`
|  | `56` |  | `try`
| 13 | `57` |  | `{`
| 13 | `58` |  | `var mappings = _options.Mappings`
| 23 | `59` |  | `.Select(m => new`
| 23 | `60` |  | `{`
| 23 | `61` |  | `Mapping = m,`
| 23 | `62` |  | `MatchResult = m.IsRequestHandled(request)`
| 23 | `63` |  | `})`
| 13 | `64` |  | `.ToList();`
|  | `65` |  | ``
| 13 | `66` |  | `if (_options.AllowPartialMapping)`
| 0 | `67` |  | `{`
| 0 | `68` |  | `var partialMappings = mappings`
| 0 | `69` |  | `.Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdmin`
| 0 | `70` |  | `.OrderBy(m => m.MatchResult)`
| 0 | `71` |  | `.ThenBy(m => m.Mapping.Priority)`
| 0 | `72` |  | `.ToList();`
|  | `73` |  | ``
| 0 | `74` |  | `var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0);`
|  | `75` |  | ``
| 0 | `76` |  | `targetMapping = bestPartialMatch?.Mapping;`
| 0 | `77` |  | `requestMatchResult = bestPartialMatch?.MatchResult;`
| 0 | `78` |  | `}`
|  | `79` |  | `else`
| 13 | `80` |  | `{`
| 13 | `81` |  | `var perfectMatch = mappings`
| 23 | `82` |  | `.OrderBy(m => m.Mapping.Priority)`
| 21 | `83` |  | `.FirstOrDefault(m => m.MatchResult.IsPerfectMatch);`
|  | `84` |  | ``
| 13 | `85` |  | `targetMapping = perfectMatch?.Mapping;`
| 13 | `86` |  | `requestMatchResult = perfectMatch?.MatchResult;`
| 13 | `87` |  | `}`
|  | `88` |  | ``
| 13 | `89` |  | `if (targetMapping == null)`
| 6 | `90` |  | `{`
| 6 | `91` |  | `response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" };`
| 6 | `92` |  | `return;`
|  | `93` |  | `}`
|  | `94` |  | ``
| 7 | `95` |  | `if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)`
| 0 | `96` |  | `{`
|  | `97` |  | `string authorization;`
| 0 | `98` |  | `bool present = request.Headers.TryGetValue("Authorization", out authorization);`
| 0 | `99` |  | `if (!present || _options.AuthorizationMatcher.IsMatch(authorization) < 1.0)`
| 0 | `100` |  | `{`
| 0 | `101` |  | `response = new ResponseMessage { StatusCode = 401 };`
| 0 | `102` |  | `return;`
|  | `103` |  | `}`
| 0 | `104` |  | `}`
|  | `105` |  | ``
| 7 | `106` |  | `response = await targetMapping.ResponseTo(request);`
| 7 | `107` |  | `}`
| 0 | `108` |  | `catch (Exception ex)`
| 0 | `109` |  | `{`
| 0 | `110` |  | `response = new ResponseMessage { StatusCode = 500, Body = ex.ToString() };`
| 0 | `111` |  | `}`
|  | `112` |  | `finally`
| 13 | `113` |  | `{`
| 13 | `114` |  | `var log = new LogEntry`
| 13 | `115` |  | `{`
| 13 | `116` |  | `Guid = Guid.NewGuid(),`
| 13 | `117` |  | `RequestMessage = request,`
| 13 | `118` |  | `ResponseMessage = response,`
| 13 | `119` |  | `MappingGuid = targetMapping?.Guid,`
| 13 | `120` |  | `MappingTitle = targetMapping?.Title,`
| 13 | `121` |  | `RequestMatchResult = requestMatchResult`
| 13 | `122` |  | `};`
|  | `123` |  | ``
| 13 | `124` |  | `LogRequest(log);`
|  | `125` |  | ``
| 13 | `126` |  | `await _responseMapper.MapAsync(response, ctx.Response);`
| 13 | `127` |  | `}`
|  | `128` |  | ``
| 7 | `129` |  | `await CompletedTask;`
| 13 | `130` |  | `}`
|  | `131` |  | ``
|  | `132` |  | `/// <summary>`
|  | `133` |  | `/// The log request.`
|  | `134` |  | `/// </summary>`
|  | `135` |  | `/// <param name="entry">The request.</param>`
|  | `136` |  | `private void LogRequest(LogEntry entry)`
| 13 | `137` |  | `{`
| 13 | `138` |  | `lock (((ICollection)_options.LogEntries).SyncRoot)`
| 13 | `139` |  | `{`
| 13 | `140` |  | `_options.LogEntries.Add(entry);`
| 13 | `141` |  | `}`
| 13 | `142` |  | `}`
|  | `143` |  | `}`
|  | `144` |  | `}`

# Methods/Properties

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