| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Net.Http; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | using Newtonsoft.Json; |
| | | 11 | | using WireMock.Http; |
| | | 12 | | using WireMock.Settings; |
| | | 13 | | using WireMock.Transformers; |
| | | 14 | | using WireMock.Util; |
| | | 15 | | using WireMock.Validation; |
| | | 16 | | |
| | | 17 | | namespace 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> |
| | 72 | 29 | | 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> |
| | 59 | 37 | | public bool UseTransformer { get; private set; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The Proxy URL to use. |
| | | 41 | | /// </summary> |
| | 71 | 42 | | public string ProxyUrl { get; private set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The client X509Certificate2 Thumbprint or SubjectName to use. |
| | | 46 | | /// </summary> |
| | 6 | 47 | | public string ClientX509Certificate2ThumbprintOrSubjectName { get; private set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the response message. |
| | | 51 | | /// </summary> |
| | 309 | 52 | | public ResponseMessage ResponseMessage { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// A delegate to execute to generate the response |
| | | 56 | | /// </summary> |
| | 50 | 57 | | 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) |
| | 63 | 66 | | { |
| | 63 | 67 | | var message = responseMessage ?? new ResponseMessage { StatusCode = (int)HttpStatusCode.OK }; |
| | 63 | 68 | | return new Response(message); |
| | 63 | 69 | | } |
| | | 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) |
| | 1 | 78 | | { |
| | 1 | 79 | | Check.NotNull(func, nameof(func)); |
| | | 80 | | |
| | 1 | 81 | | return new Response(func()); |
| | 1 | 82 | | } |
| | | 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> |
| | 64 | 90 | | private Response(ResponseMessage responseMessage) |
| | 64 | 91 | | { |
| | 64 | 92 | | ResponseMessage = responseMessage; |
| | 64 | 93 | | } |
| | | 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) |
| | 19 | 102 | | { |
| | 19 | 103 | | ResponseMessage.StatusCode = code; |
| | 19 | 104 | | return this; |
| | 19 | 105 | | } |
| | | 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) |
| | 1 | 114 | | { |
| | 1 | 115 | | return WithStatusCode((int)code); |
| | 1 | 116 | | } |
| | | 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() |
| | 1 | 124 | | { |
| | 1 | 125 | | return WithStatusCode((int)HttpStatusCode.OK); |
| | 1 | 126 | | } |
| | | 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() |
| | 0 | 134 | | { |
| | 0 | 135 | | return WithStatusCode((int)HttpStatusCode.NotFound); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeader(string, string[])"/> |
| | | 139 | | public IResponseBuilder WithHeader(string name, params string[] values) |
| | 58 | 140 | | { |
| | 58 | 141 | | Check.NotNull(name, nameof(name)); |
| | | 142 | | |
| | 58 | 143 | | ResponseMessage.AddHeader(name, values); |
| | 58 | 144 | | return this; |
| | 58 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string})"/> |
| | | 148 | | public IResponseBuilder WithHeaders(IDictionary<string, string> headers) |
| | 1 | 149 | | { |
| | 1 | 150 | | Check.NotNull(headers, nameof(headers)); |
| | | 151 | | |
| | 3 | 152 | | ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(head |
| | 1 | 153 | | return this; |
| | 1 | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string[]})"/> |
| | | 157 | | public IResponseBuilder WithHeaders(IDictionary<string, string[]> headers) |
| | 1 | 158 | | { |
| | 1 | 159 | | Check.NotNull(headers, nameof(headers)); |
| | | 160 | | |
| | 3 | 161 | | ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(head |
| | 1 | 162 | | return this; |
| | 1 | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, WireMockList{string}})"/> |
| | | 166 | | public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> headers) |
| | 1 | 167 | | { |
| | 1 | 168 | | ResponseMessage.Headers = headers; |
| | 1 | 169 | | return this; |
| | 1 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, string}, string, Encoding)"/> |
| | | 173 | | public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, string destination = BodyDestinationF |
| | 1 | 174 | | { |
| | 2 | 175 | | return WithCallback(req => new ResponseMessage { Body = bodyFactory(req) }); |
| | 1 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/> |
| | | 179 | | public IResponseBuilder WithBody(byte[] body, string destination, Encoding encoding = null) |
| | 3 | 180 | | { |
| | 3 | 181 | | Check.NotNull(body, nameof(body)); |
| | | 182 | | |
| | 3 | 183 | | ResponseMessage.BodyDestination = destination; |
| | | 184 | | |
| | 3 | 185 | | switch (destination) |
| | | 186 | | { |
| | | 187 | | case BodyDestinationFormat.String: |
| | 1 | 188 | | var enc = encoding ?? Encoding.UTF8; |
| | 1 | 189 | | ResponseMessage.BodyAsBytes = null; |
| | 1 | 190 | | ResponseMessage.Body = enc.GetString(body); |
| | 1 | 191 | | ResponseMessage.BodyEncoding = enc; |
| | 1 | 192 | | break; |
| | | 193 | | |
| | | 194 | | default: |
| | 2 | 195 | | ResponseMessage.BodyAsBytes = body; |
| | 2 | 196 | | ResponseMessage.BodyEncoding = null; |
| | 2 | 197 | | break; |
| | | 198 | | } |
| | | 199 | | |
| | 3 | 200 | | return this; |
| | 3 | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/> |
| | | 204 | | public IResponseBuilder WithBodyFromFile(string filename, bool cache = true) |
| | 0 | 205 | | { |
| | 0 | 206 | | Check.NotNull(filename, nameof(filename)); |
| | | 207 | | |
| | 0 | 208 | | ResponseMessage.BodyEncoding = null; |
| | 0 | 209 | | ResponseMessage.BodyAsFileIsCached = cache; |
| | | 210 | | |
| | 0 | 211 | | if (cache) |
| | 0 | 212 | | { |
| | 0 | 213 | | ResponseMessage.Body = null; |
| | 0 | 214 | | ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename); |
| | 0 | 215 | | ResponseMessage.BodyAsFile = null; |
| | 0 | 216 | | } |
| | | 217 | | else |
| | 0 | 218 | | { |
| | 0 | 219 | | ResponseMessage.Body = null; |
| | 0 | 220 | | ResponseMessage.BodyAsBytes = null; |
| | 0 | 221 | | ResponseMessage.BodyAsFile = filename; |
| | 0 | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | return this; |
| | 0 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBody(string, string, Encoding)"/> |
| | | 228 | | public IResponseBuilder WithBody(string body, string destination = BodyDestinationFormat.SameAsSource, Encoding |
| | 29 | 229 | | { |
| | 29 | 230 | | Check.NotNull(body, nameof(body)); |
| | | 231 | | |
| | 29 | 232 | | encoding = encoding ?? Encoding.UTF8; |
| | | 233 | | |
| | 29 | 234 | | ResponseMessage.BodyDestination = destination; |
| | 29 | 235 | | ResponseMessage.BodyEncoding = encoding; |
| | | 236 | | |
| | 29 | 237 | | switch (destination) |
| | | 238 | | { |
| | | 239 | | case BodyDestinationFormat.Bytes: |
| | 1 | 240 | | ResponseMessage.Body = null; |
| | 1 | 241 | | ResponseMessage.BodyAsJson = null; |
| | 1 | 242 | | ResponseMessage.BodyAsBytes = encoding.GetBytes(body); |
| | 1 | 243 | | break; |
| | | 244 | | |
| | | 245 | | case BodyDestinationFormat.Json: |
| | 1 | 246 | | ResponseMessage.Body = null; |
| | 1 | 247 | | ResponseMessage.BodyAsJson = JsonConvert.DeserializeObject(body); |
| | 1 | 248 | | ResponseMessage.BodyAsBytes = null; |
| | 1 | 249 | | break; |
| | | 250 | | |
| | | 251 | | default: |
| | 27 | 252 | | ResponseMessage.Body = body; |
| | 27 | 253 | | ResponseMessage.BodyAsJson = null; |
| | 27 | 254 | | ResponseMessage.BodyAsBytes = null; |
| | 27 | 255 | | break; |
| | | 256 | | } |
| | | 257 | | |
| | 29 | 258 | | return this; |
| | 29 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson"/> |
| | | 262 | | public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null) |
| | 3 | 263 | | { |
| | 3 | 264 | | Check.NotNull(body, nameof(body)); |
| | | 265 | | |
| | 3 | 266 | | ResponseMessage.BodyDestination = null; |
| | 3 | 267 | | ResponseMessage.BodyAsJson = body; |
| | 3 | 268 | | ResponseMessage.BodyEncoding = encoding; |
| | | 269 | | |
| | 3 | 270 | | return this; |
| | 3 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromBase64"/> |
| | | 274 | | public IResponseBuilder WithBodyFromBase64(string bodyAsbase64, Encoding encoding = null) |
| | 1 | 275 | | { |
| | 1 | 276 | | Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); |
| | | 277 | | |
| | 1 | 278 | | encoding = encoding ?? Encoding.UTF8; |
| | | 279 | | |
| | 1 | 280 | | ResponseMessage.BodyDestination = null; |
| | 1 | 281 | | ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64)); |
| | 1 | 282 | | ResponseMessage.BodyEncoding = encoding; |
| | | 283 | | |
| | 1 | 284 | | return this; |
| | 1 | 285 | | } |
| | | 286 | | |
| | | 287 | | /// <inheritdoc cref="ITransformResponseBuilder.WithTransformer"/> |
| | | 288 | | public IResponseBuilder WithTransformer() |
| | 6 | 289 | | { |
| | 6 | 290 | | UseTransformer = true; |
| | 6 | 291 | | return this; |
| | 6 | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <inheritdoc cref="IDelayResponseBuilder.WithDelay(TimeSpan)"/> |
| | | 295 | | public IResponseBuilder WithDelay(TimeSpan delay) |
| | 2 | 296 | | { |
| | 4 | 297 | | Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay)); |
| | | 298 | | |
| | 2 | 299 | | Delay = delay; |
| | 2 | 300 | | return this; |
| | 2 | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <inheritdoc cref="IDelayResponseBuilder.WithDelay(int)"/> |
| | | 304 | | public IResponseBuilder WithDelay(int milliseconds) |
| | 1 | 305 | | { |
| | 1 | 306 | | return WithDelay(TimeSpan.FromMilliseconds(milliseconds)); |
| | 1 | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <inheritdoc cref="IProxyResponseBuilder.WithProxy(string, string)"/> |
| | | 310 | | public IResponseBuilder WithProxy(string proxyUrl, string clientX509Certificate2ThumbprintOrSubjectName = null) |
| | 6 | 311 | | { |
| | 6 | 312 | | Check.NotNullOrEmpty(proxyUrl, nameof(proxyUrl)); |
| | | 313 | | |
| | 6 | 314 | | ProxyUrl = proxyUrl; |
| | 6 | 315 | | ClientX509Certificate2ThumbprintOrSubjectName = clientX509Certificate2ThumbprintOrSubjectName; |
| | 6 | 316 | | _httpClientForProxy = HttpClientHelper.CreateHttpClient(clientX509Certificate2ThumbprintOrSubjectName); |
| | 6 | 317 | | return this; |
| | 6 | 318 | | } |
| | | 319 | | |
| | | 320 | | /// <inheritdoc cref="IProxyResponseBuilder.WithProxy(IProxyAndRecordSettings)"/> |
| | | 321 | | public IResponseBuilder WithProxy(IProxyAndRecordSettings settings) |
| | 0 | 322 | | { |
| | 0 | 323 | | Check.NotNull(settings, nameof(settings)); |
| | | 324 | | |
| | 0 | 325 | | return WithProxy(settings.Url, settings.ClientX509Certificate2ThumbprintOrSubjectName); |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <inheritdoc cref="ICallbackResponseBuilder.WithCallback"/> |
| | | 329 | | public IResponseBuilder WithCallback(Func<RequestMessage, ResponseMessage> callbackHandler) |
| | 2 | 330 | | { |
| | 2 | 331 | | Check.NotNull(callbackHandler, nameof(callbackHandler)); |
| | | 332 | | |
| | 2 | 333 | | Callback = callbackHandler; |
| | | 334 | | |
| | 2 | 335 | | return this; |
| | 2 | 336 | | } |
| | | 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) |
| | 58 | 344 | | { |
| | 58 | 345 | | Check.NotNull(requestMessage, nameof(requestMessage)); |
| | | 346 | | |
| | 58 | 347 | | if (Delay != null) |
| | 11 | 348 | | { |
| | 11 | 349 | | await Task.Delay(Delay.Value); |
| | 11 | 350 | | } |
| | | 351 | | |
| | 58 | 352 | | if (ProxyUrl != null && _httpClientForProxy != null) |
| | 6 | 353 | | { |
| | 6 | 354 | | var requestUri = new Uri(requestMessage.Url); |
| | 6 | 355 | | var proxyUri = new Uri(ProxyUrl); |
| | 6 | 356 | | var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery); |
| | | 357 | | |
| | 6 | 358 | | return await HttpClientHelper.SendAsync(_httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQ |
| | | 359 | | } |
| | | 360 | | |
| | 52 | 361 | | if (UseTransformer) |
| | 6 | 362 | | { |
| | 6 | 363 | | return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage); |
| | | 364 | | } |
| | | 365 | | |
| | 46 | 366 | | if (Callback != null) |
| | 2 | 367 | | { |
| | 2 | 368 | | return Callback(requestMessage); |
| | | 369 | | } |
| | | 370 | | |
| | 44 | 371 | | return ResponseMessage; |
| | 58 | 372 | | } |
| | | 373 | | } |
| | | 374 | | } |