Summary

Class:WireMock.Owin.Mappers.OwinRequestMapper
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\Mappers\OwinRequestMapper.cs
Covered lines:32
Uncovered lines:7
Coverable lines:39
Total lines:89
Line coverage:82%
Branch coverage:62.5%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ParseRequest(...)0010.5
ShouldParseBody(...)0010
MapAsync()000.750.643

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\Mappers\OwinRequestMapper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using WireMock.Models;
 6using WireMock.Util;
 7#if !USE_ASPNETCORE
 8using IRequest = Microsoft.Owin.IOwinRequest;
 9#else
 10using Microsoft.AspNetCore.Http;
 11using Microsoft.AspNetCore.Http.Extensions;
 12using IRequest = Microsoft.AspNetCore.Http.HttpRequest;
 13#endif
 14
 15namespace WireMock.Owin.Mappers
 16{
 17    /// <summary>
 18    /// OwinRequestMapper
 19    /// </summary>
 20    internal class OwinRequestMapper : IOwinRequestMapper
 21    {
 22        /// <inheritdoc cref="IOwinRequestMapper.MapAsync"/>
 23        public async Task<RequestMessage> MapAsync(IRequest request)
 4624        {
 4625            (UrlDetails urldetails, string clientIP) = ParseRequest(request);
 26
 4627            string method = request.Method;
 28
 4629            Dictionary<string, string[]> headers = null;
 4630            if (request.Headers.Any())
 4531            {
 4532                headers = new Dictionary<string, string[]>();
 26033                foreach (var header in request.Headers)
 6234                {
 6235                    headers.Add(header.Key, header.Value);
 6236                }
 4637            }
 38
 4639            IDictionary<string, string> cookies = null;
 4640            if (request.Cookies.Any())
 041            {
 042                cookies = new Dictionary<string, string>();
 043                foreach (var cookie in request.Cookies)
 044                {
 045                    cookies.Add(cookie.Key, cookie.Value);
 046                }
 047            }
 48
 4649            BodyData body = null;
 4650            if (request.Body != null && ShouldParseBody(method))
 751            {
 752                body = await BodyParser.Parse(request.Body, request.ContentType);
 753            }
 54
 4655            return new RequestMessage(urldetails, method, clientIP, body, headers, cookies) { DateTime = DateTime.UtcNow
 4656        }
 57
 58        private (UrlDetails UrlDetails, string ClientIP) ParseRequest(IRequest request)
 4659        {
 60#if !USE_ASPNETCORE
 61            var urldetails = UrlUtils.Parse(request.Uri, request.PathBase);
 62            string clientIP = request.RemoteIpAddress;
 63#else
 4664            var urldetails = UrlUtils.Parse(new Uri(request.GetEncodedUrl()), request.PathBase);
 4665            var connection = request.HttpContext.Connection;
 4666            string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
 4667                ? connection.RemoteIpAddress.MapToIPv4().ToString()
 4668                : connection.RemoteIpAddress.ToString();
 69#endif
 4570            return (urldetails, clientIP);
 4671        }
 72
 73        private bool ShouldParseBody(string method)
 4674        {
 75            /*
 76                HEAD - No defined body semantics.
 77                GET - No defined body semantics.
 78                PUT - Body supported.
 79                POST - Body supported.
 80                DELETE - No defined body semantics.
 81                TRACE - Body not supported.
 82                OPTIONS - Body supported but no semantics on usage (maybe in the future).
 83                CONNECT - No defined body semantics
 84                PATCH - Body supported.
 85            */
 4686            return new[] { "PUT", "POST", "OPTIONS", "PATCH" }.Contains(method.ToUpper());
 4687        }
 88    }
 89}