@@ -896,447 +896,448 @@
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Threading; |
-| | | 8 | | using Newtonsoft.Json; |
-| | | 9 | | using WireMock.Handlers; |
-| | | 10 | | using WireMock.Logging; |
-| | | 11 | | using WireMock.Matchers; |
-| | | 12 | | using WireMock.Matchers.Request; |
-| | | 13 | | using WireMock.Owin; |
-| | | 14 | | using WireMock.RequestBuilders; |
-| | | 15 | | using WireMock.ResponseProviders; |
-| | | 16 | | using WireMock.Settings; |
-| | | 17 | | using WireMock.Util; |
-| | | 18 | | using WireMock.Validation; |
-| | | 19 | | |
-| | | 20 | | namespace WireMock.Server |
-| | | 21 | | { |
-| | | 22 | | /// <summary> |
-| | | 23 | | /// The fluent mock server. |
-| | | 24 | | /// </summary> |
-| | | 25 | | public partial class FluentMockServer : IDisposable |
-| | | 26 | | { |
-| | | 27 | | private readonly IWireMockLogger _logger; |
-| | | 28 | | private readonly IFileSystemHandler _fileSystemHandler; |
+| | | 8 | | using System.Threading.Tasks; |
+| | | 9 | | using Newtonsoft.Json; |
+| | | 10 | | using WireMock.Handlers; |
+| | | 11 | | using WireMock.Logging; |
+| | | 12 | | using WireMock.Matchers; |
+| | | 13 | | using WireMock.Matchers.Request; |
+| | | 14 | | using WireMock.Owin; |
+| | | 15 | | using WireMock.RequestBuilders; |
+| | | 16 | | using WireMock.ResponseProviders; |
+| | | 17 | | using WireMock.Settings; |
+| | | 18 | | using WireMock.Util; |
+| | | 19 | | using WireMock.Validation; |
+| | | 20 | | |
+| | | 21 | | namespace WireMock.Server |
+| | | 22 | | { |
+| | | 23 | | /// <summary> |
+| | | 24 | | /// The fluent mock server. |
+| | | 25 | | /// </summary> |
+| | | 26 | | public partial class FluentMockServer : IDisposable |
+| | | 27 | | { |
+| | | 28 | | private const int ServerStartDelayInMs = 100; |
| | | 29 | | |
-| | | 30 | | private const int ServerStartDelay = 100; |
-| | | 31 | | private readonly IOwinSelfHost _httpServer; |
-| | 47 | 32 | | private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions(); |
-| | | 33 | | |
-| | | 34 | | /// <summary> |
-| | | 35 | | /// Gets a value indicating whether this server is started. |
-| | | 36 | | /// </summary> |
-| | | 37 | | [PublicAPI] |
-| | 0 | 38 | | public bool IsStarted => _httpServer != null && _httpServer.IsStarted; |
-| | | 39 | | |
-| | | 40 | | /// <summary> |
-| | | 41 | | /// Gets the ports. |
-| | | 42 | | /// </summary> |
-| | | 43 | | [PublicAPI] |
-| | 20 | 44 | | public List<int> Ports { get; } |
-| | | 45 | | |
-| | | 46 | | /// <summary> |
-| | | 47 | | /// Gets the urls. |
-| | | 48 | | /// </summary> |
-| | | 49 | | [PublicAPI] |
-| | 64 | 50 | | public string[] Urls { get; } |
-| | | 51 | | |
-| | | 52 | | /// <summary> |
-| | | 53 | | /// Gets the mappings. |
-| | | 54 | | /// </summary> |
-| | | 55 | | [PublicAPI] |
-| | 18 | 56 | | public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray(); |
-| | | 57 | | |
-| | | 58 | | /// <summary> |
-| | | 59 | | /// Gets the scenarios. |
-| | | 60 | | /// </summary> |
-| | | 61 | | [PublicAPI] |
-| | 13 | 62 | | public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>( |
-| | | 63 | | |
-| | | 64 | | #region IDisposable Members |
-| | | 65 | | /// <summary> |
-| | | 66 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
-| | | 67 | | /// </summary> |
-| | | 68 | | public void Dispose() |
-| | 0 | 69 | | { |
-| | 0 | 70 | | Dispose(true); |
-| | 0 | 71 | | GC.SuppressFinalize(this); |
-| | 0 | 72 | | } |
-| | | 73 | | |
-| | | 74 | | /// <summary> |
-| | | 75 | | /// Releases unmanaged and - optionally - managed resources. |
-| | | 76 | | /// </summary> |
-| | | 77 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release |
-| | | 78 | | protected virtual void Dispose(bool disposing) |
-| | 0 | 79 | | { |
-| | 0 | 80 | | if (_httpServer != null) |
-| | 0 | 81 | | { |
-| | 0 | 82 | | _httpServer.StopAsync(); |
-| | 0 | 83 | | } |
-| | 0 | 84 | | } |
-| | | 85 | | #endregion |
-| | | 86 | | |
-| | | 87 | | #region Start/Stop |
-| | | 88 | | /// <summary> |
-| | | 89 | | /// Starts the specified settings. |
-| | | 90 | | /// </summary> |
-| | | 91 | | /// <param name="settings">The FluentMockServerSettings.</param> |
-| | | 92 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
-| | | 93 | | [PublicAPI] |
-| | | 94 | | public static FluentMockServer Start(IFluentMockServerSettings settings) |
-| | 16 | 95 | | { |
-| | 16 | 96 | | Check.NotNull(settings, nameof(settings)); |
-| | | 97 | | |
-| | 16 | 98 | | return new FluentMockServer(settings); |
-| | 16 | 99 | | } |
-| | | 100 | | |
-| | | 101 | | /// <summary> |
-| | | 102 | | /// Start this FluentMockServer. |
-| | | 103 | | /// </summary> |
-| | | 104 | | /// <param name="port">The port.</param> |
-| | | 105 | | /// <param name="ssl">The SSL support.</param> |
-| | | 106 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
-| | | 107 | | [PublicAPI] |
-| | | 108 | | public static FluentMockServer Start([CanBeNull] int? port = 0, bool ssl = false) |
-| | 26 | 109 | | { |
-| | 26 | 110 | | return new FluentMockServer(new FluentMockServerSettings |
-| | 26 | 111 | | { |
-| | 26 | 112 | | Port = port, |
-| | 26 | 113 | | UseSSL = ssl |
-| | 26 | 114 | | }); |
-| | 26 | 115 | | } |
-| | | 116 | | |
-| | | 117 | | /// <summary> |
-| | | 118 | | /// Start this FluentMockServer. |
-| | | 119 | | /// </summary> |
-| | | 120 | | /// <param name="urls">The urls to listen on.</param> |
-| | | 121 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
-| | | 122 | | [PublicAPI] |
-| | | 123 | | public static FluentMockServer Start(params string[] urls) |
-| | 1 | 124 | | { |
-| | 1 | 125 | | Check.NotNullOrEmpty(urls, nameof(urls)); |
-| | | 126 | | |
-| | 1 | 127 | | return new FluentMockServer(new FluentMockServerSettings |
-| | 1 | 128 | | { |
-| | 1 | 129 | | Urls = urls |
-| | 1 | 130 | | }); |
-| | 1 | 131 | | } |
-| | | 132 | | |
-| | | 133 | | /// <summary> |
-| | | 134 | | /// Start this FluentMockServer with the admin interface. |
-| | | 135 | | /// </summary> |
-| | | 136 | | /// <param name="port">The port.</param> |
-| | | 137 | | /// <param name="ssl">The SSL support.</param> |
-| | | 138 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
-| | | 139 | | [PublicAPI] |
-| | | 140 | | public static FluentMockServer StartWithAdminInterface(int? port = 0, bool ssl = false) |
-| | 4 | 141 | | { |
-| | 4 | 142 | | return new FluentMockServer(new FluentMockServerSettings |
-| | 4 | 143 | | { |
-| | 4 | 144 | | Port = port, |
-| | 4 | 145 | | UseSSL = ssl, |
-| | 4 | 146 | | StartAdminInterface = true |
-| | 4 | 147 | | }); |
-| | 4 | 148 | | } |
-| | | 149 | | |
-| | | 150 | | /// <summary> |
-| | | 151 | | /// Start this FluentMockServer with the admin interface. |
-| | | 152 | | /// </summary> |
-| | | 153 | | /// <param name="urls">The urls.</param> |
-| | | 154 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
-| | | 155 | | [PublicAPI] |
-| | | 156 | | public static FluentMockServer StartWithAdminInterface(params string[] urls) |
-| | 0 | 157 | | { |
-| | 0 | 158 | | Check.NotNullOrEmpty(urls, nameof(urls)); |
-| | | 159 | | |
-| | 0 | 160 | | return new FluentMockServer(new FluentMockServerSettings |
-| | 0 | 161 | | { |
-| | 0 | 162 | | Urls = urls, |
-| | 0 | 163 | | StartAdminInterface = true |
-| | 0 | 164 | | }); |
-| | 0 | 165 | | } |
-| | | 166 | | |
-| | | 167 | | /// <summary> |
-| | | 168 | | /// Start this FluentMockServer with the admin interface and read static mappings. |
-| | | 169 | | /// </summary> |
-| | | 170 | | /// <param name="urls">The urls.</param> |
-| | | 171 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
-| | | 172 | | [PublicAPI] |
-| | | 173 | | public static FluentMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls) |
-| | 0 | 174 | | { |
-| | 0 | 175 | | Check.NotNullOrEmpty(urls, nameof(urls)); |
-| | | 176 | | |
-| | 0 | 177 | | return new FluentMockServer(new FluentMockServerSettings |
-| | 0 | 178 | | { |
-| | 0 | 179 | | Urls = urls, |
-| | 0 | 180 | | StartAdminInterface = true, |
-| | 0 | 181 | | ReadStaticMappings = true |
-| | 0 | 182 | | }); |
-| | 0 | 183 | | } |
-| | | 184 | | |
-| | 47 | 185 | | private FluentMockServer(IFluentMockServerSettings settings) |
-| | 47 | 186 | | { |
-| | 47 | 187 | | settings.Logger = settings.Logger ?? new WireMockConsoleLogger(); |
-| | | 188 | | |
-| | 47 | 189 | | _logger = settings.Logger; |
-| | 47 | 190 | | _fileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler(); |
-| | | 191 | | |
-| | 47 | 192 | | _logger.Info("WireMock.Net by Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)"); |
-| | 47 | 193 | | _logger.Debug("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented) |
-| | | 194 | | |
-| | 47 | 195 | | if (settings.Urls != null) |
-| | 1 | 196 | | { |
-| | 1 | 197 | | Urls = settings.Urls.ToArray(); |
-| | 1 | 198 | | } |
-| | | 199 | | else |
-| | 46 | 200 | | { |
-| | 46 | 201 | | int port = settings.Port > 0 ? settings.Port.Value : PortUtils.FindFreeTcpPort(); |
-| | 46 | 202 | | Urls = new[] { $"{(settings.UseSSL == true ? "https" : "http")}://localhost:{port}" }; |
-| | 46 | 203 | | } |
-| | | 204 | | |
-| | 47 | 205 | | _options.PreWireMockMiddlewareInit = settings.PreWireMockMiddlewareInit; |
-| | 47 | 206 | | _options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit; |
-| | 47 | 207 | | _options.Logger = _logger; |
-| | | 208 | | |
-| | | 209 | | #if USE_ASPNETCORE |
-| | 47 | 210 | | _httpServer = new AspNetCoreSelfHost(_options, Urls); |
-| | | 211 | | #else |
-| | | 212 | | _httpServer = new OwinSelfHost(_options, Urls); |
-| | | 213 | | #endif |
-| | 47 | 214 | | Ports = _httpServer.Ports; |
-| | | 215 | | |
-| | 47 | 216 | | _httpServer.StartAsync(); |
-| | | 217 | | |
-| | 47 | 218 | | using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout)) |
-| | 47 | 219 | | { |
-| | 480 | 220 | | while (!_httpServer.IsStarted) |
-| | 433 | 221 | | { |
-| | | 222 | | // Throw out exception if service start fails |
-| | 433 | 223 | | if (_httpServer.RunningException != null) |
-| | 0 | 224 | | { |
-| | 0 | 225 | | throw new Exception($"Service start failed with error: {_httpServer.RunningException.Message}", |
-| | | 226 | | } |
-| | | 227 | | |
-| | | 228 | | // Respect start timeout setting by throwing TimeoutException |
-| | 433 | 229 | | if (ctsStartTimeout.IsCancellationRequested) |
-| | 0 | 230 | | { |
-| | 0 | 231 | | throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.S |
-| | | 232 | | } |
-| | | 233 | | |
-| | 433 | 234 | | ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelay); |
-| | 433 | 235 | | } |
-| | 47 | 236 | | } |
-| | | 237 | | |
-| | 47 | 238 | | if (settings.AllowPartialMapping == true) |
-| | 1 | 239 | | { |
-| | 1 | 240 | | AllowPartialMapping(); |
-| | 1 | 241 | | } |
-| | | 242 | | |
-| | 47 | 243 | | if (settings.StartAdminInterface == true) |
-| | 11 | 244 | | { |
-| | 11 | 245 | | if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword)) |
-| | 1 | 246 | | { |
-| | 1 | 247 | | SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword); |
-| | 1 | 248 | | } |
-| | | 249 | | |
-| | 11 | 250 | | InitAdmin(); |
-| | 11 | 251 | | } |
-| | | 252 | | |
-| | 47 | 253 | | if (settings.ReadStaticMappings == true) |
-| | 0 | 254 | | { |
-| | 0 | 255 | | ReadStaticMappings(); |
-| | 0 | 256 | | } |
-| | | 257 | | |
-| | 47 | 258 | | if (settings.WatchStaticMappings == true) |
-| | 0 | 259 | | { |
-| | 0 | 260 | | WatchStaticMappings(); |
-| | 0 | 261 | | } |
-| | | 262 | | |
-| | 47 | 263 | | if (settings.ProxyAndRecordSettings != null) |
-| | 2 | 264 | | { |
-| | 2 | 265 | | InitProxyAndRecord(settings); |
-| | 2 | 266 | | } |
-| | | 267 | | |
-| | 47 | 268 | | if (settings.RequestLogExpirationDuration != null) |
-| | 1 | 269 | | { |
-| | 1 | 270 | | SetRequestLogExpirationDuration(settings.RequestLogExpirationDuration); |
-| | 1 | 271 | | } |
-| | | 272 | | |
-| | 47 | 273 | | if (settings.MaxRequestLogCount != null) |
-| | 0 | 274 | | { |
-| | 0 | 275 | | SetMaxRequestLogCount(settings.MaxRequestLogCount); |
-| | 0 | 276 | | } |
-| | 47 | 277 | | } |
-| | | 278 | | |
-| | | 279 | | /// <summary> |
-| | | 280 | | /// Stop this server. |
-| | | 281 | | /// </summary> |
-| | | 282 | | [PublicAPI] |
-| | | 283 | | public void Stop() |
-| | 2 | 284 | | { |
-| | 2 | 285 | | var result = _httpServer?.StopAsync(); |
-| | 2 | 286 | | result?.Wait(); // wait for stop to actually happen |
-| | 2 | 287 | | } |
-| | | 288 | | #endregion |
-| | | 289 | | |
-| | | 290 | | /// <summary> |
-| | | 291 | | /// Adds the catch all mapping. |
-| | | 292 | | /// </summary> |
-| | | 293 | | [PublicAPI] |
-| | | 294 | | public void AddCatchAllMapping() |
-| | 0 | 295 | | { |
-| | 0 | 296 | | Given(Request.Create().WithPath("/*").UsingAnyMethod()) |
-| | 0 | 297 | | .WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05")) |
-| | 0 | 298 | | .AtPriority(1000) |
-| | 0 | 299 | | .RespondWith(new DynamicResponseProvider(request => ResponseMessageBuilder.Create("No matching mapping f |
-| | 0 | 300 | | } |
-| | | 301 | | |
-| | | 302 | | /// <summary> |
-| | | 303 | | /// Resets LogEntries and Mappings. |
-| | | 304 | | /// </summary> |
-| | | 305 | | [PublicAPI] |
-| | | 306 | | public void Reset() |
-| | 0 | 307 | | { |
-| | 0 | 308 | | ResetLogEntries(); |
-| | | 309 | | |
-| | 0 | 310 | | ResetMappings(); |
-| | 0 | 311 | | } |
-| | | 312 | | |
-| | | 313 | | /// <summary> |
-| | | 314 | | /// Resets the Mappings. |
-| | | 315 | | /// </summary> |
-| | | 316 | | [PublicAPI] |
-| | | 317 | | public void ResetMappings() |
-| | 2 | 318 | | { |
-| | 18 | 319 | | foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface)) |
-| | 4 | 320 | | { |
-| | 4 | 321 | | _options.Mappings.TryRemove(nonAdmin.Key, out _); |
-| | 4 | 322 | | } |
-| | 2 | 323 | | } |
-| | | 324 | | |
-| | | 325 | | /// <summary> |
-| | | 326 | | /// Deletes the mapping. |
-| | | 327 | | /// </summary> |
-| | | 328 | | /// <param name="guid">The unique identifier.</param> |
-| | | 329 | | [PublicAPI] |
-| | | 330 | | public bool DeleteMapping(Guid guid) |
-| | 0 | 331 | | { |
-| | | 332 | | // Check a mapping exists with the same GUID, if so, remove it. |
-| | 0 | 333 | | if (_options.Mappings.ContainsKey(guid)) |
-| | 0 | 334 | | { |
-| | 0 | 335 | | return _options.Mappings.TryRemove(guid, out _); |
-| | | 336 | | } |
-| | | 337 | | |
-| | 0 | 338 | | return false; |
-| | 0 | 339 | | } |
-| | | 340 | | |
-| | | 341 | | private bool DeleteMapping(string path) |
-| | 0 | 342 | | { |
-| | | 343 | | // Check a mapping exists with the same path, if so, remove it. |
-| | 0 | 344 | | var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, Stri |
-| | 0 | 345 | | return DeleteMapping(mapping.Key); |
-| | 0 | 346 | | } |
-| | | 347 | | |
-| | | 348 | | /// <summary> |
-| | | 349 | | /// The add request processing delay. |
-| | | 350 | | /// </summary> |
-| | | 351 | | /// <param name="delay">The delay.</param> |
-| | | 352 | | [PublicAPI] |
-| | | 353 | | public void AddGlobalProcessingDelay(TimeSpan delay) |
-| | 1 | 354 | | { |
-| | 1 | 355 | | _options.RequestProcessingDelay = delay; |
-| | 1 | 356 | | } |
-| | | 357 | | |
-| | | 358 | | /// <summary> |
-| | | 359 | | /// Allows the partial mapping. |
-| | | 360 | | /// </summary> |
-| | | 361 | | [PublicAPI] |
-| | | 362 | | public void AllowPartialMapping(bool allow = true) |
-| | 1 | 363 | | { |
-| | 1 | 364 | | _logger.Info("AllowPartialMapping is set to {0}", allow); |
-| | 1 | 365 | | _options.AllowPartialMapping = allow; |
-| | 1 | 366 | | } |
-| | | 367 | | |
-| | | 368 | | /// <summary> |
-| | | 369 | | /// Sets the basic authentication. |
-| | | 370 | | /// </summary> |
-| | | 371 | | /// <param name="username">The username.</param> |
-| | | 372 | | /// <param name="password">The password.</param> |
-| | | 373 | | [PublicAPI] |
-| | | 374 | | public void SetBasicAuthentication([NotNull] string username, [NotNull] string password) |
-| | 3 | 375 | | { |
-| | 3 | 376 | | Check.NotNull(username, nameof(username)); |
-| | 3 | 377 | | Check.NotNull(password, nameof(password)); |
-| | | 378 | | |
-| | 3 | 379 | | string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + p |
-| | 3 | 380 | | _options.AuthorizationMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, "^(?i)BASIC " + authorization |
-| | 3 | 381 | | } |
-| | | 382 | | |
-| | | 383 | | /// <summary> |
-| | | 384 | | /// Removes the basic authentication. |
-| | | 385 | | /// </summary> |
-| | | 386 | | [PublicAPI] |
-| | | 387 | | public void RemoveBasicAuthentication() |
-| | 1 | 388 | | { |
-| | 1 | 389 | | _options.AuthorizationMatcher = null; |
-| | 1 | 390 | | } |
-| | | 391 | | |
-| | | 392 | | /// <summary> |
-| | | 393 | | /// Sets the maximum RequestLog count. |
-| | | 394 | | /// </summary> |
-| | | 395 | | /// <param name="maxRequestLogCount">The maximum RequestLog count.</param> |
-| | | 396 | | [PublicAPI] |
-| | | 397 | | public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount) |
-| | 1 | 398 | | { |
-| | 1 | 399 | | _options.MaxRequestLogCount = maxRequestLogCount; |
-| | 1 | 400 | | } |
-| | | 401 | | |
-| | | 402 | | /// <summary> |
-| | | 403 | | /// Sets RequestLog expiration in hours. |
-| | | 404 | | /// </summary> |
-| | | 405 | | /// <param name="requestLogExpirationDuration">The RequestLog expiration in hours.</param> |
-| | | 406 | | [PublicAPI] |
-| | | 407 | | public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration) |
-| | 1 | 408 | | { |
-| | 1 | 409 | | _options.RequestLogExpirationDuration = requestLogExpirationDuration; |
-| | 1 | 410 | | } |
-| | | 411 | | |
-| | | 412 | | /// <summary> |
-| | | 413 | | /// Resets the Scenarios. |
-| | | 414 | | /// </summary> |
-| | | 415 | | [PublicAPI] |
-| | | 416 | | public void ResetScenarios() |
-| | 0 | 417 | | { |
-| | 0 | 418 | | _options.Scenarios.Clear(); |
-| | 0 | 419 | | } |
-| | | 420 | | |
-| | | 421 | | /// <summary> |
-| | | 422 | | /// The given. |
-| | | 423 | | /// </summary> |
-| | | 424 | | /// <param name="requestMatcher">The request matcher.</param> |
-| | | 425 | | /// <returns>The <see cref="IRespondWithAProvider"/>.</returns> |
-| | | 426 | | [PublicAPI] |
-| | | 427 | | public IRespondWithAProvider Given(IRequestMatcher requestMatcher) |
-| | 249 | 428 | | { |
-| | 249 | 429 | | return new RespondWithAProvider(RegisterMapping, requestMatcher); |
-| | 249 | 430 | | } |
-| | | 431 | | |
-| | | 432 | | private void RegisterMapping(IMapping mapping) |
-| | 249 | 433 | | { |
-| | | 434 | | // Check a mapping exists with the same Guid, if so, replace it. |
-| | 249 | 435 | | if (_options.Mappings.ContainsKey(mapping.Guid)) |
-| | 1 | 436 | | { |
-| | 1 | 437 | | _options.Mappings[mapping.Guid] = mapping; |
-| | 1 | 438 | | } |
-| | | 439 | | else |
-| | 248 | 440 | | { |
-| | 248 | 441 | | _options.Mappings.TryAdd(mapping.Guid, mapping); |
-| | 248 | 442 | | } |
-| | 249 | 443 | | } |
-| | | 444 | | } |
-| | | 445 | | } |
+| | | 30 | | private readonly IWireMockLogger _logger; |
+| | | 31 | | private readonly IFileSystemHandler _fileSystemHandler; |
+| | | 32 | | private readonly IOwinSelfHost _httpServer; |
+| | 47 | 33 | | private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions(); |
+| | | 34 | | |
+| | | 35 | | /// <summary> |
+| | | 36 | | /// Gets a value indicating whether this server is started. |
+| | | 37 | | /// </summary> |
+| | | 38 | | [PublicAPI] |
+| | 0 | 39 | | public bool IsStarted => _httpServer != null && _httpServer.IsStarted; |
+| | | 40 | | |
+| | | 41 | | /// <summary> |
+| | | 42 | | /// Gets the ports. |
+| | | 43 | | /// </summary> |
+| | | 44 | | [PublicAPI] |
+| | 20 | 45 | | public List<int> Ports { get; } |
+| | | 46 | | |
+| | | 47 | | /// <summary> |
+| | | 48 | | /// Gets the urls. |
+| | | 49 | | /// </summary> |
+| | | 50 | | [PublicAPI] |
+| | 64 | 51 | | public string[] Urls { get; } |
+| | | 52 | | |
+| | | 53 | | /// <summary> |
+| | | 54 | | /// Gets the mappings. |
+| | | 55 | | /// </summary> |
+| | | 56 | | [PublicAPI] |
+| | 18 | 57 | | public IEnumerable<IMapping> Mappings => _options.Mappings.Values.ToArray(); |
+| | | 58 | | |
+| | | 59 | | /// <summary> |
+| | | 60 | | /// Gets the scenarios. |
+| | | 61 | | /// </summary> |
+| | | 62 | | [PublicAPI] |
+| | 13 | 63 | | public ConcurrentDictionary<string, ScenarioState> Scenarios => new ConcurrentDictionary<string, ScenarioState>( |
+| | | 64 | | |
+| | | 65 | | #region IDisposable Members |
+| | | 66 | | /// <summary> |
+| | | 67 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
+| | | 68 | | /// </summary> |
+| | | 69 | | public void Dispose() |
+| | 0 | 70 | | { |
+| | 0 | 71 | | Dispose(true); |
+| | 0 | 72 | | GC.SuppressFinalize(this); |
+| | 0 | 73 | | } |
+| | | 74 | | |
+| | | 75 | | /// <summary> |
+| | | 76 | | /// Releases unmanaged and - optionally - managed resources. |
+| | | 77 | | /// </summary> |
+| | | 78 | | /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release |
+| | | 79 | | protected virtual void Dispose(bool disposing) |
+| | 0 | 80 | | { |
+| | 0 | 81 | | if (_httpServer != null) |
+| | 0 | 82 | | { |
+| | 0 | 83 | | _httpServer.StopAsync(); |
+| | 0 | 84 | | } |
+| | 0 | 85 | | } |
+| | | 86 | | #endregion |
+| | | 87 | | |
+| | | 88 | | #region Start/Stop |
+| | | 89 | | /// <summary> |
+| | | 90 | | /// Starts the specified settings. |
+| | | 91 | | /// </summary> |
+| | | 92 | | /// <param name="settings">The FluentMockServerSettings.</param> |
+| | | 93 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
+| | | 94 | | [PublicAPI] |
+| | | 95 | | public static FluentMockServer Start(IFluentMockServerSettings settings) |
+| | 16 | 96 | | { |
+| | 16 | 97 | | Check.NotNull(settings, nameof(settings)); |
+| | | 98 | | |
+| | 16 | 99 | | return new FluentMockServer(settings); |
+| | 16 | 100 | | } |
+| | | 101 | | |
+| | | 102 | | /// <summary> |
+| | | 103 | | /// Start this FluentMockServer. |
+| | | 104 | | /// </summary> |
+| | | 105 | | /// <param name="port">The port.</param> |
+| | | 106 | | /// <param name="ssl">The SSL support.</param> |
+| | | 107 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
+| | | 108 | | [PublicAPI] |
+| | | 109 | | public static FluentMockServer Start([CanBeNull] int? port = 0, bool ssl = false) |
+| | 26 | 110 | | { |
+| | 26 | 111 | | return new FluentMockServer(new FluentMockServerSettings |
+| | 26 | 112 | | { |
+| | 26 | 113 | | Port = port, |
+| | 26 | 114 | | UseSSL = ssl |
+| | 26 | 115 | | }); |
+| | 25 | 116 | | } |
+| | | 117 | | |
+| | | 118 | | /// <summary> |
+| | | 119 | | /// Start this FluentMockServer. |
+| | | 120 | | /// </summary> |
+| | | 121 | | /// <param name="urls">The urls to listen on.</param> |
+| | | 122 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
+| | | 123 | | [PublicAPI] |
+| | | 124 | | public static FluentMockServer Start(params string[] urls) |
+| | 1 | 125 | | { |
+| | 1 | 126 | | Check.NotNullOrEmpty(urls, nameof(urls)); |
+| | | 127 | | |
+| | 1 | 128 | | return new FluentMockServer(new FluentMockServerSettings |
+| | 1 | 129 | | { |
+| | 1 | 130 | | Urls = urls |
+| | 1 | 131 | | }); |
+| | 1 | 132 | | } |
+| | | 133 | | |
+| | | 134 | | /// <summary> |
+| | | 135 | | /// Start this FluentMockServer with the admin interface. |
+| | | 136 | | /// </summary> |
+| | | 137 | | /// <param name="port">The port.</param> |
+| | | 138 | | /// <param name="ssl">The SSL support.</param> |
+| | | 139 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
+| | | 140 | | [PublicAPI] |
+| | | 141 | | public static FluentMockServer StartWithAdminInterface(int? port = 0, bool ssl = false) |
+| | 4 | 142 | | { |
+| | 4 | 143 | | return new FluentMockServer(new FluentMockServerSettings |
+| | 4 | 144 | | { |
+| | 4 | 145 | | Port = port, |
+| | 4 | 146 | | UseSSL = ssl, |
+| | 4 | 147 | | StartAdminInterface = true |
+| | 4 | 148 | | }); |
+| | 4 | 149 | | } |
+| | | 150 | | |
+| | | 151 | | /// <summary> |
+| | | 152 | | /// Start this FluentMockServer with the admin interface. |
+| | | 153 | | /// </summary> |
+| | | 154 | | /// <param name="urls">The urls.</param> |
+| | | 155 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
+| | | 156 | | [PublicAPI] |
+| | | 157 | | public static FluentMockServer StartWithAdminInterface(params string[] urls) |
+| | 0 | 158 | | { |
+| | 0 | 159 | | Check.NotNullOrEmpty(urls, nameof(urls)); |
+| | | 160 | | |
+| | 0 | 161 | | return new FluentMockServer(new FluentMockServerSettings |
+| | 0 | 162 | | { |
+| | 0 | 163 | | Urls = urls, |
+| | 0 | 164 | | StartAdminInterface = true |
+| | 0 | 165 | | }); |
+| | 0 | 166 | | } |
+| | | 167 | | |
+| | | 168 | | /// <summary> |
+| | | 169 | | /// Start this FluentMockServer with the admin interface and read static mappings. |
+| | | 170 | | /// </summary> |
+| | | 171 | | /// <param name="urls">The urls.</param> |
+| | | 172 | | /// <returns>The <see cref="FluentMockServer"/>.</returns> |
+| | | 173 | | [PublicAPI] |
+| | | 174 | | public static FluentMockServer StartWithAdminInterfaceAndReadStaticMappings(params string[] urls) |
+| | 0 | 175 | | { |
+| | 0 | 176 | | Check.NotNullOrEmpty(urls, nameof(urls)); |
+| | | 177 | | |
+| | 0 | 178 | | return new FluentMockServer(new FluentMockServerSettings |
+| | 0 | 179 | | { |
+| | 0 | 180 | | Urls = urls, |
+| | 0 | 181 | | StartAdminInterface = true, |
+| | 0 | 182 | | ReadStaticMappings = true |
+| | 0 | 183 | | }); |
+| | 0 | 184 | | } |
+| | | 185 | | |
+| | 47 | 186 | | private FluentMockServer(IFluentMockServerSettings settings) |
+| | 47 | 187 | | { |
+| | 47 | 188 | | settings.Logger = settings.Logger ?? new WireMockConsoleLogger(); |
+| | | 189 | | |
+| | 47 | 190 | | _logger = settings.Logger; |
+| | 47 | 191 | | _fileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler(); |
+| | | 192 | | |
+| | 47 | 193 | | _logger.Info("WireMock.Net by Stef Heyenrath (https://github.com/WireMock-Net/WireMock.Net)"); |
+| | 47 | 194 | | _logger.Debug("WireMock.Net server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented) |
+| | | 195 | | |
+| | 47 | 196 | | if (settings.Urls != null) |
+| | 1 | 197 | | { |
+| | 1 | 198 | | Urls = settings.Urls.ToArray(); |
+| | 1 | 199 | | } |
+| | | 200 | | else |
+| | 46 | 201 | | { |
+| | 46 | 202 | | int port = settings.Port > 0 ? settings.Port.Value : PortUtils.FindFreeTcpPort(); |
+| | 46 | 203 | | Urls = new[] { $"{(settings.UseSSL == true ? "https" : "http")}://localhost:{port}" }; |
+| | 46 | 204 | | } |
+| | | 205 | | |
+| | 47 | 206 | | _options.PreWireMockMiddlewareInit = settings.PreWireMockMiddlewareInit; |
+| | 47 | 207 | | _options.PostWireMockMiddlewareInit = settings.PostWireMockMiddlewareInit; |
+| | 47 | 208 | | _options.Logger = _logger; |
+| | | 209 | | |
+| | | 210 | | #if USE_ASPNETCORE |
+| | 47 | 211 | | _httpServer = new AspNetCoreSelfHost(_options, Urls); |
+| | | 212 | | #else |
+| | | 213 | | _httpServer = new OwinSelfHost(_options, Urls); |
+| | | 214 | | #endif |
+| | 47 | 215 | | Ports = _httpServer.Ports; |
+| | | 216 | | |
+| | 47 | 217 | | _httpServer.StartAsync(); |
+| | | 218 | | |
+| | 47 | 219 | | using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout)) |
+| | 47 | 220 | | { |
+| | 47 | 221 | | while (!_httpServer.IsStarted) |
+| | 0 | 222 | | { |
+| | | 223 | | // Throw out exception if service start fails |
+| | 0 | 224 | | if (_httpServer.RunningException != null) |
+| | 0 | 225 | | { |
+| | 0 | 226 | | throw new Exception($"Service start failed with error: {_httpServer.RunningException.Message}", |
+| | | 227 | | } |
+| | | 228 | | |
+| | | 229 | | // Respect start timeout setting by throwing TimeoutException |
+| | 0 | 230 | | if (ctsStartTimeout.IsCancellationRequested) |
+| | 0 | 231 | | { |
+| | 0 | 232 | | throw new TimeoutException($"Service start timed out after {TimeSpan.FromMilliseconds(settings.S |
+| | | 233 | | } |
+| | | 234 | | |
+| | 0 | 235 | | ctsStartTimeout.Token.WaitHandle.WaitOne(ServerStartDelayInMs); |
+| | 0 | 236 | | } |
+| | 47 | 237 | | } |
+| | | 238 | | |
+| | 47 | 239 | | if (settings.AllowPartialMapping == true) |
+| | 1 | 240 | | { |
+| | 1 | 241 | | AllowPartialMapping(); |
+| | 1 | 242 | | } |
+| | | 243 | | |
+| | 47 | 244 | | if (settings.StartAdminInterface == true) |
+| | 11 | 245 | | { |
+| | 11 | 246 | | if (!string.IsNullOrEmpty(settings.AdminUsername) && !string.IsNullOrEmpty(settings.AdminPassword)) |
+| | 1 | 247 | | { |
+| | 1 | 248 | | SetBasicAuthentication(settings.AdminUsername, settings.AdminPassword); |
+| | 1 | 249 | | } |
+| | | 250 | | |
+| | 11 | 251 | | InitAdmin(); |
+| | 11 | 252 | | } |
+| | | 253 | | |
+| | 47 | 254 | | if (settings.ReadStaticMappings == true) |
+| | 0 | 255 | | { |
+| | 0 | 256 | | ReadStaticMappings(); |
+| | 0 | 257 | | } |
+| | | 258 | | |
+| | 47 | 259 | | if (settings.WatchStaticMappings == true) |
+| | 0 | 260 | | { |
+| | 0 | 261 | | WatchStaticMappings(); |
+| | 0 | 262 | | } |
+| | | 263 | | |
+| | 47 | 264 | | if (settings.ProxyAndRecordSettings != null) |
+| | 2 | 265 | | { |
+| | 2 | 266 | | InitProxyAndRecord(settings); |
+| | 2 | 267 | | } |
+| | | 268 | | |
+| | 47 | 269 | | if (settings.RequestLogExpirationDuration != null) |
+| | 1 | 270 | | { |
+| | 1 | 271 | | SetRequestLogExpirationDuration(settings.RequestLogExpirationDuration); |
+| | 1 | 272 | | } |
+| | | 273 | | |
+| | 47 | 274 | | if (settings.MaxRequestLogCount != null) |
+| | 0 | 275 | | { |
+| | 0 | 276 | | SetMaxRequestLogCount(settings.MaxRequestLogCount); |
+| | 0 | 277 | | } |
+| | 46 | 278 | | } |
+| | | 279 | | |
+| | | 280 | | /// <summary> |
+| | | 281 | | /// Stop this server. |
+| | | 282 | | /// </summary> |
+| | | 283 | | [PublicAPI] |
+| | | 284 | | public void Stop() |
+| | 2 | 285 | | { |
+| | 2 | 286 | | var result = _httpServer?.StopAsync(); |
+| | 2 | 287 | | result?.Wait(); // wait for stop to actually happen |
+| | 2 | 288 | | } |
+| | | 289 | | #endregion |
+| | | 290 | | |
+| | | 291 | | /// <summary> |
+| | | 292 | | /// Adds the catch all mapping. |
+| | | 293 | | /// </summary> |
+| | | 294 | | [PublicAPI] |
+| | | 295 | | public void AddCatchAllMapping() |
+| | 0 | 296 | | { |
+| | 0 | 297 | | Given(Request.Create().WithPath("/*").UsingAnyMethod()) |
+| | 0 | 298 | | .WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05")) |
+| | 0 | 299 | | .AtPriority(1000) |
+| | 0 | 300 | | .RespondWith(new DynamicResponseProvider(request => ResponseMessageBuilder.Create("No matching mapping f |
+| | 0 | 301 | | } |
+| | | 302 | | |
+| | | 303 | | /// <summary> |
+| | | 304 | | /// Resets LogEntries and Mappings. |
+| | | 305 | | /// </summary> |
+| | | 306 | | [PublicAPI] |
+| | | 307 | | public void Reset() |
+| | 0 | 308 | | { |
+| | 0 | 309 | | ResetLogEntries(); |
+| | | 310 | | |
+| | 0 | 311 | | ResetMappings(); |
+| | 0 | 312 | | } |
+| | | 313 | | |
+| | | 314 | | /// <summary> |
+| | | 315 | | /// Resets the Mappings. |
+| | | 316 | | /// </summary> |
+| | | 317 | | [PublicAPI] |
+| | | 318 | | public void ResetMappings() |
+| | 2 | 319 | | { |
+| | 18 | 320 | | foreach (var nonAdmin in _options.Mappings.ToArray().Where(m => !m.Value.IsAdminInterface)) |
+| | 4 | 321 | | { |
+| | 4 | 322 | | _options.Mappings.TryRemove(nonAdmin.Key, out _); |
+| | 4 | 323 | | } |
+| | 2 | 324 | | } |
+| | | 325 | | |
+| | | 326 | | /// <summary> |
+| | | 327 | | /// Deletes the mapping. |
+| | | 328 | | /// </summary> |
+| | | 329 | | /// <param name="guid">The unique identifier.</param> |
+| | | 330 | | [PublicAPI] |
+| | | 331 | | public bool DeleteMapping(Guid guid) |
+| | 0 | 332 | | { |
+| | | 333 | | // Check a mapping exists with the same GUID, if so, remove it. |
+| | 0 | 334 | | if (_options.Mappings.ContainsKey(guid)) |
+| | 0 | 335 | | { |
+| | 0 | 336 | | return _options.Mappings.TryRemove(guid, out _); |
+| | | 337 | | } |
+| | | 338 | | |
+| | 0 | 339 | | return false; |
+| | 0 | 340 | | } |
+| | | 341 | | |
+| | | 342 | | private bool DeleteMapping(string path) |
+| | 0 | 343 | | { |
+| | | 344 | | // Check a mapping exists with the same path, if so, remove it. |
+| | 0 | 345 | | var mapping = _options.Mappings.ToArray().FirstOrDefault(entry => string.Equals(entry.Value.Path, path, Stri |
+| | 0 | 346 | | return DeleteMapping(mapping.Key); |
+| | 0 | 347 | | } |
+| | | 348 | | |
+| | | 349 | | /// <summary> |
+| | | 350 | | /// The add request processing delay. |
+| | | 351 | | /// </summary> |
+| | | 352 | | /// <param name="delay">The delay.</param> |
+| | | 353 | | [PublicAPI] |
+| | | 354 | | public void AddGlobalProcessingDelay(TimeSpan delay) |
+| | 1 | 355 | | { |
+| | 1 | 356 | | _options.RequestProcessingDelay = delay; |
+| | 1 | 357 | | } |
+| | | 358 | | |
+| | | 359 | | /// <summary> |
+| | | 360 | | /// Allows the partial mapping. |
+| | | 361 | | /// </summary> |
+| | | 362 | | [PublicAPI] |
+| | | 363 | | public void AllowPartialMapping(bool allow = true) |
+| | 1 | 364 | | { |
+| | 1 | 365 | | _logger.Info("AllowPartialMapping is set to {0}", allow); |
+| | 1 | 366 | | _options.AllowPartialMapping = allow; |
+| | 1 | 367 | | } |
+| | | 368 | | |
+| | | 369 | | /// <summary> |
+| | | 370 | | /// Sets the basic authentication. |
+| | | 371 | | /// </summary> |
+| | | 372 | | /// <param name="username">The username.</param> |
+| | | 373 | | /// <param name="password">The password.</param> |
+| | | 374 | | [PublicAPI] |
+| | | 375 | | public void SetBasicAuthentication([NotNull] string username, [NotNull] string password) |
+| | 3 | 376 | | { |
+| | 3 | 377 | | Check.NotNull(username, nameof(username)); |
+| | 3 | 378 | | Check.NotNull(password, nameof(password)); |
+| | | 379 | | |
+| | 3 | 380 | | string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + p |
+| | 3 | 381 | | _options.AuthorizationMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, "^(?i)BASIC " + authorization |
+| | 3 | 382 | | } |
+| | | 383 | | |
+| | | 384 | | /// <summary> |
+| | | 385 | | /// Removes the basic authentication. |
+| | | 386 | | /// </summary> |
+| | | 387 | | [PublicAPI] |
+| | | 388 | | public void RemoveBasicAuthentication() |
+| | 1 | 389 | | { |
+| | 1 | 390 | | _options.AuthorizationMatcher = null; |
+| | 1 | 391 | | } |
+| | | 392 | | |
+| | | 393 | | /// <summary> |
+| | | 394 | | /// Sets the maximum RequestLog count. |
+| | | 395 | | /// </summary> |
+| | | 396 | | /// <param name="maxRequestLogCount">The maximum RequestLog count.</param> |
+| | | 397 | | [PublicAPI] |
+| | | 398 | | public void SetMaxRequestLogCount([CanBeNull] int? maxRequestLogCount) |
+| | 1 | 399 | | { |
+| | 1 | 400 | | _options.MaxRequestLogCount = maxRequestLogCount; |
+| | 1 | 401 | | } |
+| | | 402 | | |
+| | | 403 | | /// <summary> |
+| | | 404 | | /// Sets RequestLog expiration in hours. |
+| | | 405 | | /// </summary> |
+| | | 406 | | /// <param name="requestLogExpirationDuration">The RequestLog expiration in hours.</param> |
+| | | 407 | | [PublicAPI] |
+| | | 408 | | public void SetRequestLogExpirationDuration([CanBeNull] int? requestLogExpirationDuration) |
+| | 1 | 409 | | { |
+| | 1 | 410 | | _options.RequestLogExpirationDuration = requestLogExpirationDuration; |
+| | 1 | 411 | | } |
+| | | 412 | | |
+| | | 413 | | /// <summary> |
+| | | 414 | | /// Resets the Scenarios. |
+| | | 415 | | /// </summary> |
+| | | 416 | | [PublicAPI] |
+| | | 417 | | public void ResetScenarios() |
+| | 0 | 418 | | { |
+| | 0 | 419 | | _options.Scenarios.Clear(); |
+| | 0 | 420 | | } |
+| | | 421 | | |
+| | | 422 | | /// <summary> |
+| | | 423 | | /// The given. |
+| | | 424 | | /// </summary> |
+| | | 425 | | /// <param name="requestMatcher">The request matcher.</param> |
+| | | 426 | | /// <returns>The <see cref="IRespondWithAProvider"/>.</returns> |
+| | | 427 | | [PublicAPI] |
+| | | 428 | | public IRespondWithAProvider Given(IRequestMatcher requestMatcher) |
+| | 249 | 429 | | { |
+| | 249 | 430 | | return new RespondWithAProvider(RegisterMapping, requestMatcher); |
+| | 249 | 431 | | } |
+| | | 432 | | |
+| | | 433 | | private void RegisterMapping(IMapping mapping) |
+| | 249 | 434 | | { |
+| | | 435 | | // Check a mapping exists with the same Guid, if so, replace it. |
+| | 249 | 436 | | if (_options.Mappings.ContainsKey(mapping.Guid)) |
+| | 1 | 437 | | { |
+| | 1 | 438 | | _options.Mappings[mapping.Guid] = mapping; |
+| | 1 | 439 | | } |
+| | | 440 | | else |
+| | 248 | 441 | | { |
+| | 248 | 442 | | _options.Mappings.TryAdd(mapping.Guid, mapping); |
+| | 248 | 443 | | } |
+| | 249 | 444 | | } |
+| | | 445 | | } |
+| | | 446 | | } |