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:32
Uncovered lines:0
Coverable lines:32
Total lines:92
Line coverage:100%
Branch coverage:83.3%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
ShouldParseBody(...)10100100
MapAsync()13810085.71

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3// using System.IO;
 4using System.Linq;
 5// using System.Text;
 6using System.Threading.Tasks;
 7using WireMock.Util;
 8#if !NETSTANDARD
 9using Microsoft.Owin;
 10#else
 11using Microsoft.AspNetCore.Http;
 12using Microsoft.AspNetCore.Http.Extensions;
 13#endif
 14
 15namespace WireMock.Owin
 16{
 17    /// <summary>
 18    /// OwinRequestMapper
 19    /// </summary>
 20    internal class OwinRequestMapper
 21    {
 22        /// <summary>
 23        /// MapAsync IOwinRequest to RequestMessage
 24        /// </summary>
 25        /// <param name="request"></param>
 26        /// <returns></returns>
 27        public async Task<RequestMessage> MapAsync(
 28#if !NETSTANDARD
 29            IOwinRequest request
 30#else
 31            HttpRequest request
 32#endif
 33            )
 5834        {
 35#if !NETSTANDARD
 5836            Uri url = request.Uri;
 5837            string clientIP = request.RemoteIpAddress;
 38#else
 39            Uri url = new Uri(request.GetEncodedUrl());
 40            var connection = request.HttpContext.Connection;
 41            string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
 42                ? connection.RemoteIpAddress.MapToIPv4().ToString()
 43                : connection.RemoteIpAddress.ToString();
 44#endif
 5845            string method = request.Method;
 46
 5847            Dictionary<string, string[]> headers = null;
 5848             if (request.Headers.Any())
 5849            {
 5850                headers = new Dictionary<string, string[]>();
 45651                foreach (var header in request.Headers)
 14152                {
 14153                    headers.Add(header.Key, header.Value);
 14154                }
 5855            }
 56
 5857            IDictionary<string, string> cookies = null;
 5858             if (request.Cookies.Any())
 259            {
 260                cookies = new Dictionary<string, string>();
 1061                foreach (var cookie in request.Cookies)
 262                {
 263                    cookies.Add(cookie.Key, cookie.Value);
 264                }
 265            }
 66
 5867            BodyData body = null;
 5868             if (request.Body != null && ShouldParseBody(method))
 1069            {
 1070                body = await BodyParser.Parse(request.Body, request.ContentType);
 1071            }
 72
 5873            return new RequestMessage(url, method, clientIP, body, headers, cookies) { DateTime = DateTime.Now };
 5874        }
 75
 76        private bool ShouldParseBody(string method)
 5877        {
 78            /*
 79                HEAD - No defined body semantics.
 80                GET - No defined body semantics.
 81                PUT - Body supported.
 82                POST - Body supported.
 83                DELETE - No defined body semantics.
 84                TRACE - Body not supported.
 85                OPTIONS - Body supported but no semantics on usage (maybe in the future).
 86                CONNECT - No defined body semantics
 87                PATCH - Body supported.
 88            */
 5889            return new[] { "PUT", "POST", "OPTIONS", "PATCH" }.Contains(method.ToUpper());
 5890        }
 91    }
 92}