Summary

Class:WireMock.Owin.OwinRequestMapper
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\OwinRequestMapper.cs
Covered lines:28
Uncovered lines:7
Coverable lines:35
Total lines:90
Line coverage:80%
Branch coverage:62.5%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ShouldParseBody(...)0010
MapAsync()000.7810.625

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using WireMock.Util;
 6#if !USE_ASPNETCORE
 7using Microsoft.Owin;
 8#else
 9using Microsoft.AspNetCore.Http;
 10using Microsoft.AspNetCore.Http.Extensions;
 11#endif
 12
 13namespace WireMock.Owin
 14{
 15    /// <summary>
 16    /// OwinRequestMapper
 17    /// </summary>
 18    internal class OwinRequestMapper
 19    {
 20        /// <summary>
 21        /// MapAsync IOwinRequest to RequestMessage
 22        /// </summary>
 23        /// <param name="request"></param>
 24        /// <returns></returns>
 25        public async Task<RequestMessage> MapAsync(
 26#if !USE_ASPNETCORE
 27            IOwinRequest request
 28#else
 29            HttpRequest request
 30#endif
 31            )
 6532        {
 33#if !USE_ASPNETCORE
 34            var urldetails = UrlUtils.Parse(request.Uri, request.PathBase);
 35            string clientIP = request.RemoteIpAddress;
 36#else
 6537            var urldetails = UrlUtils.Parse(new Uri(request.GetEncodedUrl()), request.PathBase);
 6538            var connection = request.HttpContext.Connection;
 6539            string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
 6540                ? connection.RemoteIpAddress.MapToIPv4().ToString()
 6541                : connection.RemoteIpAddress.ToString();
 42#endif
 6543            string method = request.Method;
 44
 6545            Dictionary<string, string[]> headers = null;
 6546            if (request.Headers.Any())
 6547            {
 6548                headers = new Dictionary<string, string[]>();
 39349                foreach (var header in request.Headers)
 9950                {
 9951                    headers.Add(header.Key, header.Value);
 9952                }
 6553            }
 54
 6555            IDictionary<string, string> cookies = null;
 6556            if (request.Cookies.Any())
 057            {
 058                cookies = new Dictionary<string, string>();
 059                foreach (var cookie in request.Cookies)
 060                {
 061                    cookies.Add(cookie.Key, cookie.Value);
 062                }
 063            }
 64
 6565            BodyData body = null;
 6566            if (request.Body != null && ShouldParseBody(method))
 1667            {
 1668                body = await BodyParser.Parse(request.Body, request.ContentType);
 1669            }
 70
 6571            return new RequestMessage(urldetails, method, clientIP, body, headers, cookies) { DateTime = DateTime.Now };
 6572        }
 73
 74        private bool ShouldParseBody(string method)
 6575        {
 76            /*
 77                HEAD - No defined body semantics.
 78                GET - No defined body semantics.
 79                PUT - Body supported.
 80                POST - Body supported.
 81                DELETE - No defined body semantics.
 82                TRACE - Body not supported.
 83                OPTIONS - Body supported but no semantics on usage (maybe in the future).
 84                CONNECT - No defined body semantics
 85                PATCH - Body supported.
 86            */
 6587            return new[] { "PUT", "POST", "OPTIONS", "PATCH" }.Contains(method.ToUpper());
 6588        }
 89    }
 90}