diff --git a/src/WireMock.Net/Http/HttpListenerRequestMapper.cs b/src/WireMock.Net/Http/HttpListenerRequestMapper.cs deleted file mode 100644 index b875d34e..00000000 --- a/src/WireMock.Net/Http/HttpListenerRequestMapper.cs +++ /dev/null @@ -1,65 +0,0 @@ -//using System; -//using System.Collections.Generic; -//using System.IO; -//using System.Linq; -//using System.Net; -//using System.Text; -//#if NET45 - -//#else -//using System.Net.Http; -//#endif - -//namespace WireMock.Http -//{ -// /// -// /// The http listener request mapper. -// /// -// public class HttpListenerRequestMapperOld -// { -// /// -// /// The map. -// /// -// /// The listener request. -// /// The . -// public RequestMessage MapAsync(HttpListenerRequest listenerRequest) -// { -// Uri url = listenerRequest.Url; -// string verb = listenerRequest.HttpMethod; -// byte[] body = GetRequestBody(listenerRequest); -// Encoding bodyEncoding = body != null ? listenerRequest.ContentEncoding : null; -// string bodyAsString = bodyEncoding?.GetString(body); -// var listenerHeaders = listenerRequest.Headers; -// var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]); -// var cookies = new Dictionary(); - -// foreach (Cookie cookie in listenerRequest.Cookies) -// cookies.Add(cookie.Name, cookie.Value); - -// return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now }; -// } - -// /// -// /// The get request body. -// /// -// /// The request. -// /// The . -// private byte[] GetRequestBody(HttpListenerRequest request) -// { -// if (!request.HasEntityBody) -// { -// return null; -// } - -// using (var bodyStream = request.InputStream) -// { -// using (var memoryStream = new MemoryStream()) -// { -// bodyStream.CopyTo(memoryStream); - -// return memoryStream.ToArray(); -// } -// } -// } -// } -//} \ No newline at end of file diff --git a/src/WireMock.Net/Http/HttpListenerResponseMapper.cs b/src/WireMock.Net/Http/HttpListenerResponseMapper.cs deleted file mode 100644 index b12a669c..00000000 --- a/src/WireMock.Net/Http/HttpListenerResponseMapper.cs +++ /dev/null @@ -1,44 +0,0 @@ -//using System.Linq; -//using System.Net; -//using System.Text; -//#if NET45 - -//#else -//using System.Net.Http; -//#endif - -//namespace WireMock.Http -//{ -// /// -// /// The http listener response mapper. -// /// -// public class HttpListenerResponseMapperOld -// { -// private readonly Encoding _utf8NoBom = new UTF8Encoding(false); - -// /// -// /// The map. -// /// -// /// -// /// The response. -// /// -// /// The listenerResponse. -// public void MapAsync(ResponseMessage responseMessage, HttpListenerResponse listenerResponse) -// { -// listenerResponse.StatusCode = responseMessage.StatusCode; - -// responseMessage.Headers.ToList().ForEach(pair => listenerResponse.AddHeader(pair.Key, pair.Value)); - -// if (responseMessage.Body == null) -// return; - -// var encoding = responseMessage.BodyEncoding ?? _utf8NoBom; -// byte[] buffer = encoding.GetBytes(responseMessage.Body); - -// listenerResponse.ContentEncoding = encoding; -// listenerResponse.ContentLength64 = buffer.Length; -// listenerResponse.OutputStream.Write(buffer, 0, buffer.Length); -// listenerResponse.OutputStream.Flush(); -// } -// } -//} \ No newline at end of file diff --git a/src/WireMock.Net/Http/TinyHttpServer.cs b/src/WireMock.Net/Http/TinyHttpServer.cs deleted file mode 100644 index 073733cd..00000000 --- a/src/WireMock.Net/Http/TinyHttpServer.cs +++ /dev/null @@ -1,117 +0,0 @@ -//using System; -//using System.Collections.Generic; -//#if NET45 -//using System.Net; -//#else -//using System.Net.Http; -//#endif -//using System.Threading; -//using System.Threading.Tasks; -//using JetBrains.Annotations; -//using WireMock.Validation; - -//namespace WireMock.Http -//{ -// /// -// /// The tiny http server. -// /// -// public class TinyHttpServerOld -// { -// private readonly Action _httpHandler; - -// private readonly HttpListener _listener; - -// private readonly CancellationTokenSource _cts; - -// /// -// /// Gets a value indicating whether this server is started. -// /// -// /// -// /// true if this server is started; otherwise, false. -// /// -// public bool IsStarted { get; private set; } - -// /// -// /// Gets the url. -// /// -// /// -// /// The urls. -// /// -// [PublicAPI] -// public List Urls { get; } = new List(); - -// /// -// /// Gets the ports. -// /// -// /// -// /// The ports. -// /// -// [PublicAPI] -// public List Ports { get; } = new List(); - -// /// -// /// Initializes a new instance of the class. -// /// -// /// The uriPrefixes. -// /// The http handler. -// public TinyHttpServerOld([NotNull] Action httpHandler, [NotNull] params string[] uriPrefixes) -// { -// Check.NotNull(httpHandler, nameof(httpHandler)); -// Check.NotEmpty(uriPrefixes, nameof(uriPrefixes)); - -// _cts = new CancellationTokenSource(); - -// _httpHandler = httpHandler; - -// // Create a listener. -// _listener = new HttpListener(); -// foreach (string uriPrefix in uriPrefixes) -// { -// var uri = new Uri(uriPrefix); -// Urls.Add(uri); -// Ports.Add(uri.Port); - -// _listener.Prefixes.Add(uriPrefix); -// } -// } - -// /// -// /// Start the server. -// /// -// [PublicAPI] -// public void Start() -// { -// _listener.Start(); - -// IsStarted = true; - -// Task.Run( -// async () => -// { -// //using (_listener) -// { -// while (!_cts.Token.IsCancellationRequested) -// { -// HttpListenerContext context = await _listener.GetContextAsync(); -// _httpHandler(context, _cts.Token); -// } - -// _listener.Stop(); -// IsStarted = false; -// } -// }, -// _cts.Token); -// } - -// /// -// /// Stop the server. -// /// -// [PublicAPI] -// public void Stop() -// { -// _listener?.Stop(); - -// _cts.Cancel(); -// } -// } -//} \ No newline at end of file diff --git a/src/WireMock.Net/MappingRegistrationCallback.cs b/src/WireMock.Net/MappingRegistrationCallback.cs index ad9d79a2..78616afb 100644 --- a/src/WireMock.Net/MappingRegistrationCallback.cs +++ b/src/WireMock.Net/MappingRegistrationCallback.cs @@ -3,8 +3,6 @@ /// /// The registration callback. /// - /// - /// The route. - /// + /// The mapping. public delegate void RegistrationCallback(Mapping mapping); } \ No newline at end of file diff --git a/src/WireMock.Net/Server/FluentMockServer.LogEntries.cs b/src/WireMock.Net/Server/FluentMockServer.LogEntries.cs new file mode 100644 index 00000000..81a42120 --- /dev/null +++ b/src/WireMock.Net/Server/FluentMockServer.LogEntries.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using JetBrains.Annotations; +using WireMock.Logging; +using WireMock.Matchers.Request; +using System.Linq; + +namespace WireMock.Server +{ + public partial class FluentMockServer + { + /// + /// Gets the request logs. + /// + [PublicAPI] + public IEnumerable LogEntries + { + get + { + lock (((ICollection)_options.LogEntries).SyncRoot) + { + return new ReadOnlyCollection(_options.LogEntries); + } + } + } + + /// + /// The search log-entries based on matchers. + /// + /// The matchers. + /// The . + [PublicAPI] + public IEnumerable FindLogEntries([NotNull] params IRequestMatcher[] matchers) + { + lock (((ICollection)_options.LogEntries).SyncRoot) + { + var results = new Dictionary(); + + foreach (var log in _options.LogEntries) + { + var requestMatchResult = new RequestMatchResult(); + foreach (var matcher in matchers) + { + matcher.GetMatchingScore(log.RequestMessage, requestMatchResult); + } + + if (requestMatchResult.AverageTotalScore > 0.99) + results.Add(log, requestMatchResult); + } + + return new ReadOnlyCollection(results.OrderBy(x => x.Value).Select(x => x.Key).ToList()); + } + } + + /// + /// Resets the LogEntries. + /// + [PublicAPI] + public void ResetLogEntries() + { + lock (((ICollection)_options.LogEntries).SyncRoot) + { + _options.LogEntries.Clear(); + } + } + + /// + /// Deletes the mapping. + /// + /// The unique identifier. + [PublicAPI] + public bool DeleteLogEntry(Guid guid) + { + lock (((ICollection)_options.LogEntries).SyncRoot) + { + // Check a logentry exists with the same GUID, if so, remove it. + var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid); + if (existing != null) + { + _options.LogEntries.Remove(existing); + return true; + } + + return false; + } + } + } +} diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 07182753..2682993f 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -41,49 +41,6 @@ namespace WireMock.Server [PublicAPI] public string[] Urls { get; } - /// - /// Gets the request logs. - /// - [PublicAPI] - public IEnumerable LogEntries - { - get - { - lock (((ICollection)_options.LogEntries).SyncRoot) - { - return new ReadOnlyCollection(_options.LogEntries); - } - } - } - - /// - /// The search log-entries based on matchers. - /// - /// The matchers. - /// The . - [PublicAPI] - public IEnumerable FindLogEntries([NotNull] params IRequestMatcher[] matchers) - { - lock (((ICollection)_options.LogEntries).SyncRoot) - { - var results = new Dictionary(); - - foreach (var log in _options.LogEntries) - { - var requestMatchResult = new RequestMatchResult(); - foreach (var matcher in matchers) - { - matcher.GetMatchingScore(log.RequestMessage, requestMatchResult); - } - - if (requestMatchResult.AverageTotalScore > 0.99) - results.Add(log, requestMatchResult); - } - - return new ReadOnlyCollection(results.OrderBy(x => x.Value).Select(x => x.Key).ToList()); - } - } - /// /// Gets the mappings. /// @@ -271,39 +228,6 @@ namespace WireMock.Server ResetMappings(); } - /// - /// Resets the LogEntries. - /// - [PublicAPI] - public void ResetLogEntries() - { - lock (((ICollection)_options.LogEntries).SyncRoot) - { - _options.LogEntries.Clear(); - } - } - - /// - /// Deletes the mapping. - /// - /// The unique identifier. - [PublicAPI] - public bool DeleteLogEntry(Guid guid) - { - lock (((ICollection)_options.LogEntries).SyncRoot) - { - // Check a logentry exists with the same GUID, if so, remove it. - var existing = _options.LogEntries.FirstOrDefault(m => m.Guid == guid); - if (existing != null) - { - _options.LogEntries.Remove(existing); - return true; - } - - return false; - } - } - /// /// Resets the Mappings. /// @@ -406,91 +330,5 @@ namespace WireMock.Server _options.Mappings.Add(mapping); } } - - //private async void HandleRequestOld(IOwinContext ctx) - //{ - // if (_requestProcessingDelay > TimeSpan.Zero) - // { - // lock (_syncRoot) - // { - // Task.Delay(_requestProcessingDelay.Value).Wait(); - // } - // } - - // var request = _requestMapper.MapAsync(ctx.Request); - - // ResponseMessage response = null; - // Mapping targetMapping = null; - // RequestMatchResult requestMatchResult = null; - // try - // { - // var mappings = _mappings - // .Select(m => new { Mapping = m, MatchResult = m.IsRequestHandled(request) }) - // .ToList(); - - // if (_allowPartialMapping) - // { - // var partialMappings = mappings - // .Where(pm => pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch || !pm.Mapping.IsAdminInterface) - // .OrderBy(m => m.MatchResult) - // .ThenBy(m => m.Mapping.Priority) - // .ToList(); - - // var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0); - - // targetMapping = bestPartialMatch?.Mapping; - // requestMatchResult = bestPartialMatch?.MatchResult; - // } - // else - // { - // var perfectMatch = mappings - // .OrderBy(m => m.Mapping.Priority) - // .FirstOrDefault(m => m.MatchResult.IsPerfectMatch); - - // targetMapping = perfectMatch?.Mapping; - // requestMatchResult = perfectMatch?.MatchResult; - // } - - // if (targetMapping == null) - // { - // response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" }; - // return; - // } - - // if (targetMapping.IsAdminInterface && _authorizationMatcher != null) - // { - // string authorization; - // bool present = request.Headers.TryGetValue("Authorization", out authorization); - // if (!present || _authorizationMatcher.IsMatch(authorization) < 1.0) - // { - // response = new ResponseMessage { StatusCode = 401 }; - // return; - // } - // } - - // response = await targetMapping.ResponseTo(request); - // } - // catch (Exception ex) - // { - // response = new ResponseMessage { StatusCode = 500, Body = ex.ToString() }; - // } - // finally - // { - // var log = new LogEntry - // { - // Guid = Guid.NewGuid(), - // RequestMessage = request, - // ResponseMessage = response, - // MappingGuid = targetMapping?.Guid, - // MappingTitle = targetMapping?.Title, - // RequestMatchResult = requestMatchResult - // }; - - // LogRequest(log); - - // _responseMapper.MapAsync(response, ctx.Response); - // ctx.Response.Close(); - // } - //} } } \ No newline at end of file diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index 09279010..f2aa2b2c 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -26,6 +26,10 @@ NETSTANDARD + + + + All