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