using System; using System.Collections.Generic; using System.Net; using System.Text; using System.Threading.Tasks; using JetBrains.Annotations; using Newtonsoft.Json; using WireMock.Validation; using WireMock.Http; using WireMock.Transformers; namespace WireMock.ResponseBuilders { /// /// The Response. /// public class Response : IResponseBuilder { /// /// The delay /// public TimeSpan? Delay { get; private set; } /// /// Gets a value indicating whether [use transformer]. /// /// /// true if [use transformer]; otherwise, false. /// public bool UseTransformer { get; private set; } /// /// The Proxy URL to use. /// public string ProxyUrl { get; private set; } /// /// The client X509Certificate2 Thumbprint or SubjectName to use. /// public string X509Certificate2ThumbprintOrSubjectName { get; private set; } /// /// Gets the response message. /// /// /// The response message. /// public ResponseMessage ResponseMessage { get; } /// /// Creates this instance. /// /// ResponseMessage /// A . [PublicAPI] public static IResponseBuilder Create([CanBeNull] ResponseMessage responseMessage = null) { var message = responseMessage ?? new ResponseMessage { StatusCode = (int)HttpStatusCode.OK }; return new Response(message); } /// /// Creates this instance. /// /// A . [PublicAPI] public static IResponseBuilder Create([NotNull] Func func) { Check.NotNull(func, nameof(func)); return new Response(func()); } /// /// Initializes a new instance of the class. /// /// /// The response. /// private Response(ResponseMessage responseMessage) { ResponseMessage = responseMessage; } /// /// The with status code. /// /// The code. /// A .\ [PublicAPI] public IResponseBuilder WithStatusCode(int code) { ResponseMessage.StatusCode = code; return this; } /// /// The with status code. /// /// The code. /// A . [PublicAPI] public IResponseBuilder WithStatusCode(HttpStatusCode code) { return WithStatusCode((int)code); } /// /// The with Success status code (200). /// /// A . [PublicAPI] public IResponseBuilder WithSuccess() { return WithStatusCode((int)HttpStatusCode.OK); } /// /// The with NotFound status code (404). /// /// The . [PublicAPI] public IResponseBuilder WithNotFound() { return WithStatusCode((int)HttpStatusCode.NotFound); } /// /// The with header. /// /// The name. /// The value. /// The . public IResponseBuilder WithHeader(string name, string value) { Check.NotNull(name, nameof(name)); ResponseMessage.AddHeader(name, value); return this; } /// /// The with headers. /// /// The headers. /// public IResponseBuilder WithHeaders(IDictionary headers) { ResponseMessage.Headers = headers; return this; } /// /// The with body. /// /// The body. /// The body encoding. /// A . public IResponseBuilder WithBody(string body, Encoding encoding = null) { Check.NotNull(body, nameof(body)); ResponseMessage.Body = body; ResponseMessage.BodyEncoding = encoding ?? Encoding.UTF8; return this; } /// /// The with body (AsJson object). /// /// The body. /// The body encoding. /// A . public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null) { Check.NotNull(body, nameof(body)); string jsonBody = JsonConvert.SerializeObject(body, new JsonSerializerSettings { Formatting = Formatting.None, NullValueHandling = NullValueHandling.Ignore }); if (encoding != null && !encoding.Equals(Encoding.UTF8)) { jsonBody = encoding.GetString(Encoding.UTF8.GetBytes(jsonBody)); ResponseMessage.BodyEncoding = encoding; } ResponseMessage.Body = jsonBody; return this; } /// /// The with body as base64. /// /// The body asbase64. /// The Encoding. /// A . public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null) { Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); encoding = encoding ?? Encoding.UTF8; ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64)); ResponseMessage.BodyEncoding = encoding; return this; } /// /// The with transformer. /// /// /// The . /// public IResponseBuilder WithTransformer() { UseTransformer = true; return this; } /// /// The with delay. /// /// The TimeSpan to delay. /// The . public IResponseBuilder WithDelay(TimeSpan delay) { Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay)); Delay = delay; return this; } /// /// The with delay. /// /// The milliseconds to delay. /// The . public IResponseBuilder WithDelay(int milliseconds) { return WithDelay(TimeSpan.FromMilliseconds(milliseconds)); } /// /// With Proxy URL. /// /// The proxy url. /// The X509Certificate2 file to use for client authentication. /// A . public IResponseBuilder WithProxy(string proxyUrl, string clientX509Certificate2ThumbprintOrSubjectName = null) { Check.NotEmpty(proxyUrl, nameof(proxyUrl)); ProxyUrl = proxyUrl; X509Certificate2ThumbprintOrSubjectName = clientX509Certificate2ThumbprintOrSubjectName; return this; } /// /// The provide response. /// /// The request. /// The . public async Task ProvideResponseAsync(RequestMessage requestMessage) { Check.NotNull(requestMessage, nameof(requestMessage)); if (Delay != null) await Task.Delay(Delay.Value); if (ProxyUrl != null) { var requestUri = new Uri(requestMessage.Url); var proxyUri = new Uri(ProxyUrl); var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery); return await HttpClientHelper.SendAsync(requestMessage, proxyUriWithRequestPathAndQuery.AbsoluteUri, X509Certificate2ThumbprintOrSubjectName); } if (UseTransformer) { return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage); } return ResponseMessage; } } }