Summary

Class:WireMock.ResponseBuilders.Response
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\ResponseBuilders\Response.cs
Covered lines:143
Uncovered lines:24
Coverable lines:167
Total lines:374
Line coverage:85.6%
Branch coverage:81.8%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
Create(...)20100100
Create(...)10100100
.ctor(...)10100100
WithStatusCode(...)10100100
WithStatusCode(...)10100100
WithSuccess()10100100
WithNotFound()1000
WithHeader(...)10100100
WithHeaders(...)30100100
WithHeaders(...)30100100
WithHeaders(...)10100100
WithBody(...)10100100
WithBody(...)3410080
WithBodyFromFile(...)2200
WithBody(...)48100100
WithBodyAsJson(...)10100100
WithBodyFromBase64(...)2210066.67
WithTransformer()10100100
WithDelay(...)20100100
WithDelay(...)10100100
WithProxy(...)10100100
WithProxy(...)1000
WithCallback(...)10100100
ProvideResponseAsync()1016100100

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\ResponseBuilders\Response.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Linq;
 5using System.Net;
 6using System.Net.Http;
 7using System.Text;
 8using System.Threading.Tasks;
 9using JetBrains.Annotations;
 10using Newtonsoft.Json;
 11using WireMock.Http;
 12using WireMock.Settings;
 13using WireMock.Transformers;
 14using WireMock.Util;
 15using WireMock.Validation;
 16
 17namespace WireMock.ResponseBuilders
 18{
 19    /// <summary>
 20    /// The Response.
 21    /// </summary>
 22    public class Response : IResponseBuilder
 23    {
 24        private HttpClient _httpClientForProxy;
 25
 26        /// <summary>
 27        /// The delay
 28        /// </summary>
 7229        public TimeSpan? Delay { get; private set; }
 30
 31        /// <summary>
 32        /// Gets a value indicating whether [use transformer].
 33        /// </summary>
 34        /// <value>
 35        ///   <c>true</c> if [use transformer]; otherwise, <c>false</c>.
 36        /// </value>
 5937        public bool UseTransformer { get; private set; }
 38
 39        /// <summary>
 40        /// The Proxy URL to use.
 41        /// </summary>
 7142        public string ProxyUrl { get; private set; }
 43
 44        /// <summary>
 45        /// The client X509Certificate2 Thumbprint or SubjectName to use.
 46        /// </summary>
 647        public string ClientX509Certificate2ThumbprintOrSubjectName { get; private set; }
 48
 49        /// <summary>
 50        /// Gets the response message.
 51        /// </summary>
 30952        public ResponseMessage ResponseMessage { get; }
 53
 54        /// <summary>
 55        /// A delegate to execute to generate the response
 56        /// </summary>
 5057        public Func<RequestMessage, ResponseMessage> Callback { get; private set; }
 58
 59        /// <summary>
 60        /// Creates this instance.
 61        /// </summary>
 62        /// <param name="responseMessage">ResponseMessage</param>
 63        /// <returns>A <see cref="IResponseBuilder"/>.</returns>
 64        [PublicAPI]
 65        public static IResponseBuilder Create([CanBeNull] ResponseMessage responseMessage = null)
 6366        {
 6367            var message = responseMessage ?? new ResponseMessage { StatusCode = (int)HttpStatusCode.OK };
 6368            return new Response(message);
 6369        }
 70
 71        /// <summary>
 72        /// Creates this instance with the specified function.
 73        /// </summary>
 74        /// <param name="func">The callback function.</param>
 75        /// <returns>A <see cref="IResponseBuilder"/>.</returns>
 76        [PublicAPI]
 77        public static IResponseBuilder Create([NotNull] Func<ResponseMessage> func)
 178        {
 179            Check.NotNull(func, nameof(func));
 80
 181            return new Response(func());
 182        }
 83
 84        /// <summary>
 85        /// Initializes a new instance of the <see cref="Response"/> class.
 86        /// </summary>
 87        /// <param name="responseMessage">
 88        /// The response.
 89        /// </param>
 6490        private Response(ResponseMessage responseMessage)
 6491        {
 6492            ResponseMessage = responseMessage;
 6493        }
 94
 95        /// <summary>
 96        /// The with status code.
 97        /// </summary>
 98        /// <param name="code">The code.</param>
 99        /// <returns>A <see cref="IResponseBuilder"/>.</returns>\
 100        [PublicAPI]
 101        public IResponseBuilder WithStatusCode(int code)
 19102        {
 19103            ResponseMessage.StatusCode = code;
 19104            return this;
 19105        }
 106
 107        /// <summary>
 108        /// The with status code.
 109        /// </summary>
 110        /// <param name="code">The code.</param>
 111        /// <returns>A <see cref="IResponseBuilder"/>.</returns>
 112        [PublicAPI]
 113        public IResponseBuilder WithStatusCode(HttpStatusCode code)
 1114        {
 1115            return WithStatusCode((int)code);
 1116        }
 117
 118        /// <summary>
 119        /// The with Success status code (200).
 120        /// </summary>
 121        /// <returns>A <see cref="IResponseBuilder"/>.</returns>
 122        [PublicAPI]
 123        public IResponseBuilder WithSuccess()
 1124        {
 1125            return WithStatusCode((int)HttpStatusCode.OK);
 1126        }
 127
 128        /// <summary>
 129        /// The with NotFound status code (404).
 130        /// </summary>
 131        /// <returns>The <see cref="IResponseBuilder"/>.</returns>
 132        [PublicAPI]
 133        public IResponseBuilder WithNotFound()
 0134        {
 0135            return WithStatusCode((int)HttpStatusCode.NotFound);
 0136        }
 137
 138        /// <inheritdoc cref="IHeadersResponseBuilder.WithHeader(string, string[])"/>
 139        public IResponseBuilder WithHeader(string name, params string[] values)
 58140        {
 58141            Check.NotNull(name, nameof(name));
 142
 58143            ResponseMessage.AddHeader(name, values);
 58144            return this;
 58145        }
 146
 147        /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string})"/>
 148        public IResponseBuilder WithHeaders(IDictionary<string, string> headers)
 1149        {
 1150            Check.NotNull(headers, nameof(headers));
 151
 3152            ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(head
 1153            return this;
 1154        }
 155
 156        /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string[]})"/>
 157        public IResponseBuilder WithHeaders(IDictionary<string, string[]> headers)
 1158        {
 1159            Check.NotNull(headers, nameof(headers));
 160
 3161            ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(head
 1162            return this;
 1163        }
 164
 165        /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, WireMockList{string}})"/>
 166        public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> headers)
 1167        {
 1168            ResponseMessage.Headers = headers;
 1169            return this;
 1170        }
 171
 172        /// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, string}, string, Encoding)"/>
 173        public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, string destination = BodyDestinationF
 1174        {
 2175            return WithCallback(req => new ResponseMessage { Body = bodyFactory(req) });
 1176        }
 177
 178        /// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
 179        public IResponseBuilder WithBody(byte[] body, string destination, Encoding encoding = null)
 3180        {
 3181            Check.NotNull(body, nameof(body));
 182
 3183            ResponseMessage.BodyDestination = destination;
 184
 3185             switch (destination)
 186            {
 187                case BodyDestinationFormat.String:
 1188                     var enc = encoding ?? Encoding.UTF8;
 1189                    ResponseMessage.BodyAsBytes = null;
 1190                    ResponseMessage.Body = enc.GetString(body);
 1191                    ResponseMessage.BodyEncoding = enc;
 1192                    break;
 193
 194                default:
 2195                    ResponseMessage.BodyAsBytes = body;
 2196                    ResponseMessage.BodyEncoding = null;
 2197                    break;
 198            }
 199
 3200            return this;
 3201        }
 202
 203        /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/>
 204        public IResponseBuilder WithBodyFromFile(string filename, bool cache = true)
 0205        {
 0206            Check.NotNull(filename, nameof(filename));
 207
 0208            ResponseMessage.BodyEncoding = null;
 0209            ResponseMessage.BodyAsFileIsCached = cache;
 210
 0211             if (cache)
 0212            {
 0213                ResponseMessage.Body = null;
 0214                ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename);
 0215                ResponseMessage.BodyAsFile = null;
 0216            }
 217            else
 0218            {
 0219                ResponseMessage.Body = null;
 0220                ResponseMessage.BodyAsBytes = null;
 0221                ResponseMessage.BodyAsFile = filename;
 0222            }
 223
 0224            return this;
 0225        }
 226
 227        /// <inheritdoc cref="IBodyResponseBuilder.WithBody(string, string, Encoding)"/>
 228        public IResponseBuilder WithBody(string body, string destination = BodyDestinationFormat.SameAsSource, Encoding 
 29229        {
 29230            Check.NotNull(body, nameof(body));
 231
 29232             encoding = encoding ?? Encoding.UTF8;
 233
 29234            ResponseMessage.BodyDestination = destination;
 29235            ResponseMessage.BodyEncoding = encoding;
 236
 29237             switch (destination)
 238            {
 239                case BodyDestinationFormat.Bytes:
 1240                    ResponseMessage.Body = null;
 1241                    ResponseMessage.BodyAsJson = null;
 1242                    ResponseMessage.BodyAsBytes = encoding.GetBytes(body);
 1243                    break;
 244
 245                case BodyDestinationFormat.Json:
 1246                    ResponseMessage.Body = null;
 1247                    ResponseMessage.BodyAsJson = JsonConvert.DeserializeObject(body);
 1248                    ResponseMessage.BodyAsBytes = null;
 1249                    break;
 250
 251                default:
 27252                    ResponseMessage.Body = body;
 27253                    ResponseMessage.BodyAsJson = null;
 27254                    ResponseMessage.BodyAsBytes = null;
 27255                    break;
 256            }
 257
 29258            return this;
 29259        }
 260
 261        /// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson"/>
 262        public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null)
 3263        {
 3264            Check.NotNull(body, nameof(body));
 265
 3266            ResponseMessage.BodyDestination = null;
 3267            ResponseMessage.BodyAsJson = body;
 3268            ResponseMessage.BodyEncoding = encoding;
 269
 3270            return this;
 3271        }
 272
 273        /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromBase64"/>
 274        public IResponseBuilder WithBodyFromBase64(string bodyAsbase64, Encoding encoding = null)
 1275        {
 1276            Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
 277
 1278             encoding = encoding ?? Encoding.UTF8;
 279
 1280            ResponseMessage.BodyDestination = null;
 1281            ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64));
 1282            ResponseMessage.BodyEncoding = encoding;
 283
 1284            return this;
 1285        }
 286
 287        /// <inheritdoc cref="ITransformResponseBuilder.WithTransformer"/>
 288        public IResponseBuilder WithTransformer()
 6289        {
 6290            UseTransformer = true;
 6291            return this;
 6292        }
 293
 294        /// <inheritdoc cref="IDelayResponseBuilder.WithDelay(TimeSpan)"/>
 295        public IResponseBuilder WithDelay(TimeSpan delay)
 2296        {
 4297            Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay));
 298
 2299            Delay = delay;
 2300            return this;
 2301        }
 302
 303        /// <inheritdoc cref="IDelayResponseBuilder.WithDelay(int)"/>
 304        public IResponseBuilder WithDelay(int milliseconds)
 1305        {
 1306            return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
 1307        }
 308
 309        /// <inheritdoc cref="IProxyResponseBuilder.WithProxy(string, string)"/>
 310        public IResponseBuilder WithProxy(string proxyUrl, string clientX509Certificate2ThumbprintOrSubjectName = null)
 6311        {
 6312            Check.NotNullOrEmpty(proxyUrl, nameof(proxyUrl));
 313
 6314            ProxyUrl = proxyUrl;
 6315            ClientX509Certificate2ThumbprintOrSubjectName = clientX509Certificate2ThumbprintOrSubjectName;
 6316            _httpClientForProxy = HttpClientHelper.CreateHttpClient(clientX509Certificate2ThumbprintOrSubjectName);
 6317            return this;
 6318        }
 319
 320        /// <inheritdoc cref="IProxyResponseBuilder.WithProxy(IProxyAndRecordSettings)"/>
 321        public IResponseBuilder WithProxy(IProxyAndRecordSettings settings)
 0322        {
 0323            Check.NotNull(settings, nameof(settings));
 324
 0325            return WithProxy(settings.Url, settings.ClientX509Certificate2ThumbprintOrSubjectName);
 0326        }
 327
 328        /// <inheritdoc cref="ICallbackResponseBuilder.WithCallback"/>
 329        public IResponseBuilder WithCallback(Func<RequestMessage, ResponseMessage> callbackHandler)
 2330        {
 2331            Check.NotNull(callbackHandler, nameof(callbackHandler));
 332
 2333            Callback = callbackHandler;
 334
 2335            return this;
 2336        }
 337
 338        /// <summary>
 339        /// The provide response.
 340        /// </summary>
 341        /// <param name="requestMessage">The request.</param>
 342        /// <returns>The <see cref="ResponseMessage"/>.</returns>
 343        public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
 58344        {
 58345            Check.NotNull(requestMessage, nameof(requestMessage));
 346
 58347             if (Delay != null)
 11348            {
 11349                await Task.Delay(Delay.Value);
 11350            }
 351
 58352             if (ProxyUrl != null && _httpClientForProxy != null)
 6353            {
 6354                var requestUri = new Uri(requestMessage.Url);
 6355                var proxyUri = new Uri(ProxyUrl);
 6356                var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery);
 357
 6358                return await HttpClientHelper.SendAsync(_httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQ
 359            }
 360
 52361             if (UseTransformer)
 6362            {
 6363                return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage);
 364            }
 365
 46366             if (Callback != null)
 2367            {
 2368                return Callback(requestMessage);
 369            }
 370
 44371            return ResponseMessage;
 58372        }
 373    }
 374}

Methods/Properties

Delay()
Delay(System.Nullable`1<System.TimeSpan>)
UseTransformer()
UseTransformer(System.Boolean)
ProxyUrl()
ProxyUrl(System.String)
ClientX509Certificate2ThumbprintOrSubjectName()
ClientX509Certificate2ThumbprintOrSubjectName(System.String)
ResponseMessage()
Callback()
Callback(System.Func`2<WireMock.RequestMessage,WireMock.ResponseMessage>)
Create(WireMock.ResponseMessage)
Create(System.Func`1<WireMock.ResponseMessage>)
.ctor(WireMock.ResponseMessage)
WithStatusCode(System.Int32)
WithStatusCode(System.Net.HttpStatusCode)
WithSuccess()
WithNotFound()
WithHeader(System.String,System.String[])
WithHeaders(System.Collections.Generic.IDictionary`2<System.String,System.String>)
WithHeaders(System.Collections.Generic.IDictionary`2<System.String,System.String[]>)
WithHeaders(System.Collections.Generic.IDictionary`2<System.String,WireMock.Util.WireMockList`1<System.String>>)
WithBody(System.Func`2<WireMock.RequestMessage,System.String>,System.String,System.Text.Encoding)
WithBody(System.Byte[],System.String,System.Text.Encoding)
WithBodyFromFile(System.String,System.Boolean)
WithBody(System.String,System.String,System.Text.Encoding)
WithBodyAsJson(System.Object,System.Text.Encoding)
WithBodyFromBase64(System.String,System.Text.Encoding)
WithTransformer()
WithDelay(System.TimeSpan)
WithDelay(System.Int32)
WithProxy(System.String,System.String)
WithProxy(WireMock.Settings.IProxyAndRecordSettings)
WithCallback(System.Func`2<WireMock.RequestMessage,WireMock.ResponseMessage>)
ProvideResponseAsync()