Summary

Class:WireMock.Owin.OwinResponseMapper
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Owin\OwinResponseMapper.cs
Covered lines:42
Uncovered lines:5
Coverable lines:47
Total lines:114
Line coverage:89.3%
Branch coverage:82.1%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
SetResponseHeaders(...)0010.833
.ctor()0010
.cctor()0010
MapAsync()000.8330.818

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Net;
 6using System.Text;
 7using System.Threading.Tasks;
 8using Newtonsoft.Json;
 9using WireMock.Http;
 10using WireMock.Util;
 11#if !USE_ASPNETCORE
 12using Microsoft.Owin;
 13#else
 14using Microsoft.AspNetCore.Http;
 15#endif
 16
 17namespace WireMock.Owin
 18{
 19    /// <summary>
 20    /// OwinResponseMapper
 21    /// </summary>
 22    public class OwinResponseMapper
 23    {
 11024        private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
 25
 26        // https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
 27#if !USE_ASPNETCORE
 28        private static readonly IDictionary<string, Action<IOwinResponse, WireMockList<string>>> ResponseHeadersToFix = 
 29#else
 130        private static readonly IDictionary<string, Action<HttpResponse, WireMockList<string>>> ResponseHeadersToFix = n
 131#endif
 2332            { HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }
 133        };
 34
 35        private void SetResponseHeaders(ResponseMessage responseMessage
 36#if !USE_ASPNETCORE
 37            , IOwinResponse response
 38#else
 39            , HttpResponse response
 40#endif
 41        )
 6542        {
 43            // Set headers
 24744            foreach (var pair in responseMessage.Headers)
 2645            {
 2646                if (ResponseHeadersToFix.ContainsKey(pair.Key))
 2247                {
 2248                    ResponseHeadersToFix[pair.Key]?.Invoke(response, pair.Value);
 2249                }
 50                else
 451                {
 52#if !USE_ASPNETCORE
 53                    // For non-NETSTANDARD, check if this response header can be added (#148)
 54                    if (!WebHeaderCollection.IsRestricted(pair.Key, true))
 55                    {
 56                        response.Headers.AppendValues(pair.Key, pair.Value.ToArray());
 57                    }
 58#else
 59                    // NETSTANDARD can add any header (or so it seems)
 460                    response.Headers.Append(pair.Key, pair.Value.ToArray());
 61#endif
 462                }
 2663            }
 6564        }
 65
 66        /// <summary>
 67        /// Map ResponseMessage to OwinResponse/HttpResponse
 68        /// </summary>
 69        /// <param name="responseMessage"></param>
 70        /// <param name="response"></param>
 71        public async Task MapAsync(ResponseMessage responseMessage
 72#if !USE_ASPNETCORE
 73            , IOwinResponse response
 74#else
 75            , HttpResponse response
 76#endif
 77            )
 6578        {
 6579            if (responseMessage == null)
 080            {
 081                return;
 82            }
 83
 6584            response.StatusCode = responseMessage.StatusCode;
 85
 6586            byte[] bytes = null;
 6587            if (responseMessage.BodyAsBytes != null)
 288            {
 289                bytes = responseMessage.BodyAsBytes;
 290            }
 6391            else if (responseMessage.BodyAsFile != null)
 092            {
 093                bytes = File.ReadAllBytes(responseMessage.BodyAsFile);
 094            }
 6395            else if (responseMessage.BodyAsJson != null)
 1996            {
 1997                Formatting formatting = responseMessage.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.No
 1998                string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { F
 1999                bytes = (responseMessage.BodyEncoding ?? _utf8NoBom).GetBytes(jsonBody);
 19100            }
 44101            else if (responseMessage.Body != null)
 30102            {
 30103                bytes = (responseMessage.BodyEncoding ?? _utf8NoBom).GetBytes(responseMessage.Body);
 30104            }
 105
 65106            SetResponseHeaders(responseMessage, response);
 107
 65108            if (bytes != null)
 51109            {
 51110                await response.Body.WriteAsync(bytes, 0, bytes.Length);
 51111            }
 65112        }
 113    }
 114}