Summary

Class:WireMock.Http.HttpResponseMessageHelper
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Http\HttpResponseMessageHelper.cs
Covered lines:31
Uncovered lines:0
Coverable lines:31
Total lines:53
Line coverage:100%
Branch coverage:75%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
Create()186410076.92

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Http\HttpResponseMessageHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Net.Http;
 5using System.Threading.Tasks;
 6using WireMock.Util;
 7
 8namespace WireMock.Http
 9{
 10    internal static class HttpResponseMessageHelper
 11    {
 12        public static async Task<ResponseMessage> Create(HttpResponseMessage httpResponseMessage, Uri requiredUri, Uri o
 813        {
 814            var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode };
 15
 16            // Set both content and response headers, replacing URLs in values
 817             var headers = (httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers) ?? Enumerable.Empty<K
 818             if (httpResponseMessage.Content != null)
 819            {
 820                var stream = await httpResponseMessage.Content.ReadAsStreamAsync();
 821                IEnumerable<string> contentTypeHeader = null;
 2822                 if (headers.Any(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.O
 323                {
 724                    contentTypeHeader = headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentTy
 325                }
 26
 827                 var body = await BodyParser.Parse(stream, contentTypeHeader?.FirstOrDefault());
 828                responseMessage.Body = body.BodyAsString;
 829                responseMessage.BodyAsJson = body.BodyAsJson;
 830                responseMessage.BodyAsBytes = body.BodyAsBytes;
 831            }
 32
 9233            foreach (var header in headers)
 3434            {
 35                // If Location header contains absolute redirect URL, and base URL is one that we proxy to,
 36                // we need to replace it to original one.
 3437                 if (string.Equals(header.Key, HttpKnownHeaderNames.Location, StringComparison.OrdinalIgnoreCase)
 3438                    && Uri.TryCreate(header.Value.First(), UriKind.Absolute, out Uri absoluteLocationUri)
 3439                    && string.Equals(absoluteLocationUri.Host, requiredUri.Host, StringComparison.OrdinalIgnoreCase))
 140                {
 141                    var replacedLocationUri = new Uri(originalUri, absoluteLocationUri.PathAndQuery);
 142                    responseMessage.AddHeader(header.Key, replacedLocationUri.ToString());
 143                }
 44                else
 3345                {
 3346                    responseMessage.AddHeader(header.Key, header.Value.ToArray());
 3347                }
 3448            }
 49
 850            return responseMessage;
 851        }
 52    }
 53}

Methods/Properties

Create()