| | | 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> |
| | 80 | 29 | | public TimeSpan? Delay { get; private set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets a value indicating whether [use transformer]. |
| | | 33 | | /// </summary> |
| | 108 | 34 | | public bool UseTransformer { get; private set; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The Proxy URL to use. |
| | | 38 | | /// </summary> |
| | 76 | 39 | | public string ProxyUrl { get; private set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The client X509Certificate2 Thumbprint or SubjectName to use. |
| | | 43 | | /// </summary> |
| | 0 | 44 | | public string ClientX509Certificate2ThumbprintOrSubjectName { get; private set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the response message. |
| | | 48 | | /// </summary> |
| | 550 | 49 | | public ResponseMessage ResponseMessage { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// A delegate to execute to generate the response. |
| | | 53 | | /// </summary> |
| | 80 | 54 | | public Func<RequestMessage, ResponseMessage> Callback { get; private set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Defines if the method WithCallback(...) is used. |
| | | 58 | | /// </summary> |
| | 4 | 59 | | public bool WithCallbackUsed { get; private set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Creates this instance. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="responseMessage">ResponseMessage</param> |
| | | 65 | | /// <returns>A <see cref="IResponseBuilder"/>.</returns> |
| | | 66 | | [PublicAPI] |
| | | 67 | | public static IResponseBuilder Create([CanBeNull] ResponseMessage responseMessage = null) |
| | 87 | 68 | | { |
| | 87 | 69 | | var message = responseMessage ?? new ResponseMessage { StatusCode = (int)HttpStatusCode.OK }; |
| | 87 | 70 | | return new Response(message); |
| | 87 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Creates this instance with the specified function. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="func">The callback function.</param> |
| | | 77 | | /// <returns>A <see cref="IResponseBuilder"/>.</returns> |
| | | 78 | | [PublicAPI] |
| | | 79 | | public static IResponseBuilder Create([NotNull] Func<ResponseMessage> func) |
| | 1 | 80 | | { |
| | 1 | 81 | | Check.NotNull(func, nameof(func)); |
| | | 82 | | |
| | 1 | 83 | | return new Response(func()); |
| | 1 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Initializes a new instance of the <see cref="Response"/> class. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="responseMessage"> |
| | | 90 | | /// The response. |
| | | 91 | | /// </param> |
| | 88 | 92 | | private Response(ResponseMessage responseMessage) |
| | 88 | 93 | | { |
| | 88 | 94 | | ResponseMessage = responseMessage; |
| | 88 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// The with status code. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="code">The code.</param> |
| | | 101 | | /// <returns>A <see cref="IResponseBuilder"/>.</returns>\ |
| | | 102 | | [PublicAPI] |
| | | 103 | | public IResponseBuilder WithStatusCode(int code) |
| | 22 | 104 | | { |
| | 22 | 105 | | ResponseMessage.StatusCode = code; |
| | 22 | 106 | | return this; |
| | 22 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// The with status code. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="code">The code.</param> |
| | | 113 | | /// <returns>A <see cref="IResponseBuilder"/>.</returns> |
| | | 114 | | [PublicAPI] |
| | | 115 | | public IResponseBuilder WithStatusCode(HttpStatusCode code) |
| | 0 | 116 | | { |
| | 0 | 117 | | return WithStatusCode((int)code); |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// The with Success status code (200). |
| | | 122 | | /// </summary> |
| | | 123 | | /// <returns>A <see cref="IResponseBuilder"/>.</returns> |
| | | 124 | | [PublicAPI] |
| | | 125 | | public IResponseBuilder WithSuccess() |
| | 1 | 126 | | { |
| | 1 | 127 | | return WithStatusCode((int)HttpStatusCode.OK); |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// The with NotFound status code (404). |
| | | 132 | | /// </summary> |
| | | 133 | | /// <returns>The <see cref="IResponseBuilder"/>.</returns> |
| | | 134 | | [PublicAPI] |
| | | 135 | | public IResponseBuilder WithNotFound() |
| | 0 | 136 | | { |
| | 0 | 137 | | return WithStatusCode((int)HttpStatusCode.NotFound); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeader(string, string[])"/> |
| | | 141 | | public IResponseBuilder WithHeader(string name, params string[] values) |
| | 102 | 142 | | { |
| | 102 | 143 | | Check.NotNull(name, nameof(name)); |
| | | 144 | | |
| | 102 | 145 | | ResponseMessage.AddHeader(name, values); |
| | 102 | 146 | | return this; |
| | 102 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string})"/> |
| | | 150 | | public IResponseBuilder WithHeaders(IDictionary<string, string> headers) |
| | 1 | 151 | | { |
| | 1 | 152 | | Check.NotNull(headers, nameof(headers)); |
| | | 153 | | |
| | 3 | 154 | | ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(head |
| | 1 | 155 | | return this; |
| | 1 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, string[]})"/> |
| | | 159 | | public IResponseBuilder WithHeaders(IDictionary<string, string[]> headers) |
| | 1 | 160 | | { |
| | 1 | 161 | | Check.NotNull(headers, nameof(headers)); |
| | | 162 | | |
| | 3 | 163 | | ResponseMessage.Headers = headers.ToDictionary(header => header.Key, header => new WireMockList<string>(head |
| | 1 | 164 | | return this; |
| | 1 | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <inheritdoc cref="IHeadersResponseBuilder.WithHeaders(IDictionary{string, WireMockList{string}})"/> |
| | | 168 | | public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> headers) |
| | 1 | 169 | | { |
| | 1 | 170 | | ResponseMessage.Headers = headers; |
| | 1 | 171 | | return this; |
| | 1 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, string}, string, Encoding)"/> |
| | | 175 | | public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, string destination = BodyDestinationF |
| | 1 | 176 | | { |
| | 1 | 177 | | Check.NotNull(bodyFactory, nameof(bodyFactory)); |
| | | 178 | | |
| | 2 | 179 | | return WithCallbackInternal(false, req => new ResponseMessage |
| | 2 | 180 | | { |
| | 2 | 181 | | Body = bodyFactory(req), |
| | 2 | 182 | | BodyDestination = destination, |
| | 2 | 183 | | BodyEncoding = encoding ?? Encoding.UTF8 |
| | 2 | 184 | | }); |
| | 1 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/> |
| | | 188 | | public IResponseBuilder WithBody(byte[] body, string destination = BodyDestinationFormat.SameAsSource, Encoding |
| | 2 | 189 | | { |
| | 2 | 190 | | Check.NotNull(body, nameof(body)); |
| | | 191 | | |
| | 2 | 192 | | ResponseMessage.BodyDestination = destination; |
| | | 193 | | |
| | 2 | 194 | | switch (destination) |
| | | 195 | | { |
| | | 196 | | case BodyDestinationFormat.String: |
| | 1 | 197 | | var enc = encoding ?? Encoding.UTF8; |
| | 1 | 198 | | ResponseMessage.BodyAsBytes = null; |
| | 1 | 199 | | ResponseMessage.Body = enc.GetString(body); |
| | 1 | 200 | | ResponseMessage.BodyEncoding = enc; |
| | 1 | 201 | | break; |
| | | 202 | | |
| | | 203 | | default: |
| | 1 | 204 | | ResponseMessage.BodyAsBytes = body; |
| | 1 | 205 | | ResponseMessage.BodyEncoding = null; |
| | 1 | 206 | | break; |
| | | 207 | | } |
| | | 208 | | |
| | 2 | 209 | | return this; |
| | 2 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromFile"/> |
| | | 213 | | public IResponseBuilder WithBodyFromFile(string filename, bool cache = true) |
| | 3 | 214 | | { |
| | 3 | 215 | | Check.NotNull(filename, nameof(filename)); |
| | | 216 | | |
| | 3 | 217 | | ResponseMessage.BodyEncoding = null; |
| | 3 | 218 | | ResponseMessage.BodyAsFileIsCached = cache; |
| | | 219 | | |
| | 3 | 220 | | if (cache) |
| | 3 | 221 | | { |
| | 3 | 222 | | ResponseMessage.Body = null; |
| | 3 | 223 | | ResponseMessage.BodyAsBytes = File.ReadAllBytes(filename); |
| | 3 | 224 | | ResponseMessage.BodyAsFile = null; |
| | 3 | 225 | | } |
| | | 226 | | else |
| | 0 | 227 | | { |
| | 0 | 228 | | ResponseMessage.Body = null; |
| | 0 | 229 | | ResponseMessage.BodyAsBytes = null; |
| | 0 | 230 | | ResponseMessage.BodyAsFile = filename; |
| | 0 | 231 | | } |
| | | 232 | | |
| | 3 | 233 | | return this; |
| | 3 | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBody(string, string, Encoding)"/> |
| | | 237 | | public IResponseBuilder WithBody(string body, string destination = BodyDestinationFormat.SameAsSource, Encoding |
| | 47 | 238 | | { |
| | 47 | 239 | | Check.NotNull(body, nameof(body)); |
| | | 240 | | |
| | 47 | 241 | | encoding = encoding ?? Encoding.UTF8; |
| | | 242 | | |
| | 47 | 243 | | ResponseMessage.BodyDestination = destination; |
| | 47 | 244 | | ResponseMessage.BodyEncoding = encoding; |
| | | 245 | | |
| | 47 | 246 | | switch (destination) |
| | | 247 | | { |
| | | 248 | | case BodyDestinationFormat.Bytes: |
| | 1 | 249 | | ResponseMessage.Body = null; |
| | 1 | 250 | | ResponseMessage.BodyAsJson = null; |
| | 1 | 251 | | ResponseMessage.BodyAsBytes = encoding.GetBytes(body); |
| | 1 | 252 | | break; |
| | | 253 | | |
| | | 254 | | case BodyDestinationFormat.Json: |
| | 1 | 255 | | ResponseMessage.Body = null; |
| | 1 | 256 | | ResponseMessage.BodyAsJson = JsonConvert.DeserializeObject(body); |
| | 1 | 257 | | ResponseMessage.BodyAsBytes = null; |
| | 1 | 258 | | break; |
| | | 259 | | |
| | | 260 | | default: |
| | 45 | 261 | | ResponseMessage.Body = body; |
| | 45 | 262 | | ResponseMessage.BodyAsJson = null; |
| | 45 | 263 | | ResponseMessage.BodyAsBytes = null; |
| | 45 | 264 | | break; |
| | | 265 | | } |
| | | 266 | | |
| | 47 | 267 | | return this; |
| | 47 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, Encoding, bool?)"/> |
| | | 271 | | public IResponseBuilder WithBodyAsJson(object body, Encoding encoding = null, bool? indented = null) |
| | 16 | 272 | | { |
| | 16 | 273 | | Check.NotNull(body, nameof(body)); |
| | | 274 | | |
| | 16 | 275 | | ResponseMessage.BodyDestination = null; |
| | 16 | 276 | | ResponseMessage.BodyAsJson = body; |
| | 16 | 277 | | ResponseMessage.BodyEncoding = encoding; |
| | 16 | 278 | | ResponseMessage.BodyAsJsonIndented = indented; |
| | | 279 | | |
| | 16 | 280 | | return this; |
| | 16 | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyAsJson(object, bool)"/> |
| | | 284 | | public IResponseBuilder WithBodyAsJson(object body, bool indented) |
| | 1 | 285 | | { |
| | 1 | 286 | | return WithBodyAsJson(body, null, indented); |
| | 1 | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <inheritdoc cref="IBodyResponseBuilder.WithBodyFromBase64"/> |
| | | 290 | | public IResponseBuilder WithBodyFromBase64(string bodyAsbase64, Encoding encoding = null) |
| | 1 | 291 | | { |
| | 1 | 292 | | Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); |
| | | 293 | | |
| | 1 | 294 | | encoding = encoding ?? Encoding.UTF8; |
| | | 295 | | |
| | 1 | 296 | | ResponseMessage.BodyDestination = null; |
| | 1 | 297 | | ResponseMessage.Body = encoding.GetString(Convert.FromBase64String(bodyAsbase64)); |
| | 1 | 298 | | ResponseMessage.BodyEncoding = encoding; |
| | | 299 | | |
| | 1 | 300 | | return this; |
| | 1 | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <inheritdoc cref="ITransformResponseBuilder.WithTransformer"/> |
| | | 304 | | public IResponseBuilder WithTransformer() |
| | 32 | 305 | | { |
| | 32 | 306 | | UseTransformer = true; |
| | 32 | 307 | | return this; |
| | 32 | 308 | | } |
| | | 309 | | |
| | | 310 | | /// <inheritdoc cref="IDelayResponseBuilder.WithDelay(TimeSpan)"/> |
| | | 311 | | public IResponseBuilder WithDelay(TimeSpan delay) |
| | 1 | 312 | | { |
| | 2 | 313 | | Check.Condition(delay, d => d > TimeSpan.Zero, nameof(delay)); |
| | | 314 | | |
| | 1 | 315 | | Delay = delay; |
| | 1 | 316 | | return this; |
| | 1 | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <inheritdoc cref="IDelayResponseBuilder.WithDelay(int)"/> |
| | | 320 | | public IResponseBuilder WithDelay(int milliseconds) |
| | 0 | 321 | | { |
| | 0 | 322 | | return WithDelay(TimeSpan.FromMilliseconds(milliseconds)); |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | /// <inheritdoc cref="IProxyResponseBuilder.WithProxy(string, string)"/> |
| | | 326 | | public IResponseBuilder WithProxy(string proxyUrl, string clientX509Certificate2ThumbprintOrSubjectName = null) |
| | 0 | 327 | | { |
| | 0 | 328 | | Check.NotNullOrEmpty(proxyUrl, nameof(proxyUrl)); |
| | | 329 | | |
| | 0 | 330 | | ProxyUrl = proxyUrl; |
| | 0 | 331 | | ClientX509Certificate2ThumbprintOrSubjectName = clientX509Certificate2ThumbprintOrSubjectName; |
| | 0 | 332 | | _httpClientForProxy = HttpClientHelper.CreateHttpClient(clientX509Certificate2ThumbprintOrSubjectName); |
| | 0 | 333 | | return this; |
| | 0 | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <inheritdoc cref="IProxyResponseBuilder.WithProxy(IProxyAndRecordSettings)"/> |
| | | 337 | | public IResponseBuilder WithProxy(IProxyAndRecordSettings settings) |
| | 0 | 338 | | { |
| | 0 | 339 | | Check.NotNull(settings, nameof(settings)); |
| | | 340 | | |
| | 0 | 341 | | return WithProxy(settings.Url, settings.ClientX509Certificate2ThumbprintOrSubjectName); |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | /// <inheritdoc cref="ICallbackResponseBuilder.WithCallback"/> |
| | | 345 | | public IResponseBuilder WithCallback(Func<RequestMessage, ResponseMessage> callbackHandler) |
| | 1 | 346 | | { |
| | 1 | 347 | | Check.NotNull(callbackHandler, nameof(callbackHandler)); |
| | | 348 | | |
| | 1 | 349 | | return WithCallbackInternal(true, callbackHandler); |
| | 1 | 350 | | } |
| | | 351 | | |
| | | 352 | | /// <inheritdoc cref="ICallbackResponseBuilder.WithCallback"/> |
| | | 353 | | private IResponseBuilder WithCallbackInternal(bool withCallbackUsed, Func<RequestMessage, ResponseMessage> callb |
| | 2 | 354 | | { |
| | 2 | 355 | | Check.NotNull(callbackHandler, nameof(callbackHandler)); |
| | | 356 | | |
| | 2 | 357 | | WithCallbackUsed = withCallbackUsed; |
| | 2 | 358 | | Callback = callbackHandler; |
| | | 359 | | |
| | 2 | 360 | | return this; |
| | 2 | 361 | | } |
| | | 362 | | |
| | | 363 | | /// <summary> |
| | | 364 | | /// The provide response. |
| | | 365 | | /// </summary> |
| | | 366 | | /// <param name="requestMessage">The request.</param> |
| | | 367 | | /// <returns>The <see cref="ResponseMessage"/>.</returns> |
| | | 368 | | public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage) |
| | 76 | 369 | | { |
| | 76 | 370 | | Check.NotNull(requestMessage, nameof(requestMessage)); |
| | | 371 | | |
| | 76 | 372 | | if (Delay != null) |
| | 1 | 373 | | { |
| | 1 | 374 | | await Task.Delay(Delay.Value); |
| | 1 | 375 | | } |
| | | 376 | | |
| | 76 | 377 | | if (Callback != null) |
| | 2 | 378 | | { |
| | 2 | 379 | | var callbackResponseMessage = Callback(requestMessage); |
| | | 380 | | |
| | 2 | 381 | | if (!WithCallbackUsed) |
| | 1 | 382 | | { |
| | | 383 | | // Copy StatusCode from ResponseMessage |
| | 1 | 384 | | callbackResponseMessage.StatusCode = ResponseMessage.StatusCode; |
| | | 385 | | |
| | | 386 | | // Copy Headers from ResponseMessage (if defined) |
| | 1 | 387 | | if (ResponseMessage.Headers != null) |
| | 1 | 388 | | { |
| | 1 | 389 | | callbackResponseMessage.Headers = ResponseMessage.Headers; |
| | 1 | 390 | | } |
| | 1 | 391 | | } |
| | | 392 | | |
| | 2 | 393 | | return callbackResponseMessage; |
| | | 394 | | } |
| | | 395 | | |
| | 74 | 396 | | if (ProxyUrl != null && _httpClientForProxy != null) |
| | 0 | 397 | | { |
| | 0 | 398 | | var requestUri = new Uri(requestMessage.Url); |
| | 0 | 399 | | var proxyUri = new Uri(ProxyUrl); |
| | 0 | 400 | | var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery); |
| | | 401 | | |
| | 0 | 402 | | return await HttpClientHelper.SendAsync(_httpClientForProxy, requestMessage, proxyUriWithRequestPathAndQ |
| | | 403 | | } |
| | | 404 | | |
| | 74 | 405 | | if (UseTransformer) |
| | 32 | 406 | | { |
| | 32 | 407 | | return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage); |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | // Just return normal defined ResponseMessage |
| | 42 | 411 | | return ResponseMessage; |
| | 71 | 412 | | } |
| | | 413 | | } |
| | | 414 | | } |