Summary

Class:WireMock.Owin.Mappers.OwinResponseMapper
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Owin\Mappers\OwinResponseMapper.cs
Covered lines:44
Uncovered lines:3
Coverable lines:47
Total lines:100
Line coverage:93.6%
Branch coverage:76.9%

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Text;
 6using System.Threading.Tasks;
 7using Newtonsoft.Json;
 8using WireMock.Http;
 9using WireMock.Util;
 10#if !USE_ASPNETCORE
 11using System.Net;
 12using Microsoft.Owin;
 13using IResponse = Microsoft.Owin.IOwinResponse;
 14#else
 15using Microsoft.AspNetCore.Http;
 16using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
 17#endif
 18
 19namespace WireMock.Owin.Mappers
 20{
 21    /// <summary>
 22    /// OwinResponseMapper
 23    /// </summary>
 24    public class OwinResponseMapper : IOwinResponseMapper
 25    {
 5426        private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
 27
 28        // https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
 29#if !USE_ASPNETCORE
 30        private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new 
 31#else
 132        private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new 
 133#endif
 2034            { HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }
 135        };
 36
 37        /// <inheritdoc cref="IOwinResponseMapper.MapAsync"/>
 38        public async Task MapAsync(ResponseMessage responseMessage, IResponse response)
 5339        {
 5340            if (responseMessage == null)
 141            {
 142                return;
 43            }
 44
 5245            response.StatusCode = responseMessage.StatusCode;
 46
 5247            byte[] bytes = null;
 5248            if (responseMessage.BodyAsBytes != null)
 149            {
 150                bytes = responseMessage.BodyAsBytes;
 151            }
 5152            else if (responseMessage.BodyAsFile != null)
 053            {
 054                bytes = File.ReadAllBytes(responseMessage.BodyAsFile);
 055            }
 5056            else if (responseMessage.BodyAsJson != null)
 1557            {
 1558                Formatting formatting = responseMessage.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.No
 1559                string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { F
 1560                bytes = (responseMessage.BodyEncoding ?? _utf8NoBom).GetBytes(jsonBody);
 1561            }
 3562            else if (responseMessage.Body != null)
 1963            {
 1864                bytes = (responseMessage.BodyEncoding ?? _utf8NoBom).GetBytes(responseMessage.Body);
 1965            }
 66
 5267            SetResponseHeaders(responseMessage, response);
 68
 5269            if (bytes != null)
 3570            {
 3571                await response.Body.WriteAsync(bytes, 0, bytes.Length);
 3572            }
 5373        }
 74
 75        private void SetResponseHeaders(ResponseMessage responseMessage, IResponse response)
 5176        {
 77            // Set headers
 20178            foreach (var pair in responseMessage.Headers)
 2379            {
 2380                if (ResponseHeadersToFix.ContainsKey(pair.Key))
 1981                {
 1982                    ResponseHeadersToFix[pair.Key]?.Invoke(response, pair.Value);
 1983                }
 84                else
 485                {
 86#if !USE_ASPNETCORE
 87                    // For non-NETSTANDARD, check if this response header can be added (#148)
 88                    if (!WebHeaderCollection.IsRestricted(pair.Key, true))
 89                    {
 90                        response.Headers.AppendValues(pair.Key, pair.Value.ToArray());
 91                    }
 92#else
 93                    // NETSTANDARD can add any header (or so it seems)
 494                    response.Headers.Append(pair.Key, pair.Value.ToArray());
 95#endif
 496                }
 2397            }
 5298        }
 99    }
 100}