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:44
Uncovered lines:4
Coverable lines:48
Total lines:105
Line coverage:91.6%
Branch coverage:81.2%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
.ctor()10100100
.cctor()10100100
MapAsync()302569082.35

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.Text;
 6using System.Threading.Tasks;
 7using Newtonsoft.Json;
 8using WireMock.Util;
 9#if !NETSTANDARD
 10using Microsoft.Owin;
 11#else
 12using Microsoft.AspNetCore.Http;
 13#endif
 14
 15namespace WireMock.Owin
 16{
 17    /// <summary>
 18    /// OwinResponseMapper
 19    /// </summary>
 20    public class OwinResponseMapper
 21    {
 9422        private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
 23
 24        // https://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest
 25#if !NETSTANDARD
 126        private static readonly IDictionary<string, Action<IOwinResponse, WireMockList<string>>> RestrictedResponseHeade
 127#else
 128        private static readonly IDictionary<string, Action<HttpResponse, WireMockList<string>>> RestrictedResponseHeader
 129#endif
 130            { "Content-Length", null },
 831            { "Content-Type", (r, v) => r.ContentType = v.FirstOrDefault() },
 132            { "Keep-Alive", null },
 133            { "Transfer-Encoding", null },
 134            { "WWW-Authenticate", null }
 135        };
 36
 37        /// <summary>
 38        /// MapAsync ResponseMessage to OwinResponse
 39        /// </summary>
 40        /// <param name="responseMessage"></param>
 41        /// <param name="response"></param>
 42        public async Task MapAsync(ResponseMessage responseMessage
 43#if !NETSTANDARD
 44            , IOwinResponse response
 45#else
 46            , HttpResponse response
 47#endif
 48            )
 5849        {
 5850            response.StatusCode = responseMessage.StatusCode;
 51
 52            // Set headers
 25453            foreach (var pair in responseMessage.Headers)
 4054            {
 4055                 if (RestrictedResponseHeaders.ContainsKey(pair.Key))
 1556                {
 1557                     RestrictedResponseHeaders[pair.Key]?.Invoke(response, pair.Value);
 1558                }
 59                else
 2560                {
 61#if !NETSTANDARD
 2562                    response.Headers.AppendValues(pair.Key, pair.Value.ToArray());
 63#else
 64                    response.Headers.Append(pair.Key, pair.Value.ToArray());
 65#endif
 2566                }
 4067            }
 68
 5869             if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == nul
 1870            {
 1871                return;
 72            }
 73
 4074             if (responseMessage.BodyAsBytes != null)
 775            {
 776                await response.Body.WriteAsync(responseMessage.BodyAsBytes, 0, responseMessage.BodyAsBytes.Length);
 777                return;
 78            }
 79
 3380             if (responseMessage.BodyAsFile != null)
 081            {
 082                byte[] bytes = File.ReadAllBytes(responseMessage.BodyAsFile);
 83
 084                await response.Body.WriteAsync(bytes, 0, bytes.Length);
 085                return;
 86            }
 87
 3388             if (responseMessage.BodyAsJson != null)
 289            {
 290                string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { F
 291                 using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom))
 292                {
 293                    await writer.WriteAsync(jsonBody);
 294                }
 95
 296                return;
 97            }
 98
 3199             using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom))
 31100            {
 31101                await writer.WriteAsync(responseMessage.Body);
 31102            }
 58103        }
 104    }
 105}

Methods/Properties

.ctor()
.cctor()
MapAsync()