| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using WireMock.Logging; |
| | | 4 | | using WireMock.Matchers.Request; |
| | | 5 | | using System.Linq; |
| | | 6 | | using WireMock.Matchers; |
| | | 7 | | using WireMock.Util; |
| | | 8 | | using Newtonsoft.Json; |
| | | 9 | | using WireMock.Http; |
| | | 10 | | using WireMock.Owin.Mappers; |
| | | 11 | | using WireMock.Serialization; |
| | | 12 | | using WireMock.Validation; |
| | | 13 | | #if !USE_ASPNETCORE |
| | | 14 | | using Microsoft.Owin; |
| | | 15 | | using IContext = Microsoft.Owin.IOwinContext; |
| | | 16 | | using OwinMiddleware = Microsoft.Owin.OwinMiddleware; |
| | | 17 | | using Next = Microsoft.Owin.OwinMiddleware; |
| | | 18 | | #else |
| | | 19 | | using OwinMiddleware = System.Object; |
| | | 20 | | using IContext = Microsoft.AspNetCore.Http.HttpContext; |
| | | 21 | | using Next = Microsoft.AspNetCore.Http.RequestDelegate; |
| | | 22 | | #endif |
| | | 23 | | |
| | | 24 | | namespace WireMock.Owin |
| | | 25 | | { |
| | | 26 | | internal class WireMockMiddleware : OwinMiddleware |
| | | 27 | | { |
| | 1 | 28 | | private static readonly Task CompletedTask = Task.FromResult(false); |
| | | 29 | | private readonly IWireMockMiddlewareOptions _options; |
| | | 30 | | private readonly IOwinRequestMapper _requestMapper; |
| | | 31 | | private readonly IOwinResponseMapper _responseMapper; |
| | | 32 | | private readonly IMappingMatcher _mappingMatcher; |
| | | 33 | | |
| | | 34 | | #if !USE_ASPNETCORE |
| | | 35 | | public WireMockMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwin |
| | | 36 | | { |
| | | 37 | | Check.NotNull(options, nameof(options)); |
| | | 38 | | Check.NotNull(requestMapper, nameof(requestMapper)); |
| | | 39 | | Check.NotNull(responseMapper, nameof(responseMapper)); |
| | | 40 | | Check.NotNull(mappingMatcher, nameof(mappingMatcher)); |
| | | 41 | | |
| | | 42 | | _options = options; |
| | | 43 | | _requestMapper = requestMapper; |
| | | 44 | | _responseMapper = responseMapper; |
| | | 45 | | _mappingMatcher = mappingMatcher; |
| | | 46 | | } |
| | | 47 | | #else |
| | 51 | 48 | | public WireMockMiddleware(Next next, IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwin |
| | 51 | 49 | | { |
| | 51 | 50 | | Check.NotNull(options, nameof(options)); |
| | 51 | 51 | | Check.NotNull(requestMapper, nameof(requestMapper)); |
| | 51 | 52 | | Check.NotNull(responseMapper, nameof(responseMapper)); |
| | 51 | 53 | | Check.NotNull(mappingMatcher, nameof(mappingMatcher)); |
| | | 54 | | |
| | 51 | 55 | | _options = options; |
| | 51 | 56 | | _requestMapper = requestMapper; |
| | 51 | 57 | | _responseMapper = responseMapper; |
| | 51 | 58 | | _mappingMatcher = mappingMatcher; |
| | 51 | 59 | | } |
| | | 60 | | #endif |
| | | 61 | | |
| | | 62 | | #if !USE_ASPNETCORE |
| | | 63 | | public override Task Invoke(IContext ctx) |
| | | 64 | | #else |
| | | 65 | | public Task Invoke(IContext ctx) |
| | | 66 | | #endif |
| | 50 | 67 | | { |
| | 49 | 68 | | return InvokeInternal(ctx); |
| | 50 | 69 | | } |
| | | 70 | | |
| | | 71 | | private async Task InvokeInternal(IContext ctx) |
| | 49 | 72 | | { |
| | 49 | 73 | | var request = await _requestMapper.MapAsync(ctx.Request); |
| | | 74 | | |
| | 49 | 75 | | bool logRequest = false; |
| | 49 | 76 | | ResponseMessage response = null; |
| | 49 | 77 | | (IMapping TargetMapping, RequestMatchResult RequestMatchResult) result = (null, null); |
| | | 78 | | try |
| | 49 | 79 | | { |
| | 489 | 80 | | foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null)) |
| | 30 | 81 | | { |
| | | 82 | | // Set start |
| | 30 | 83 | | if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState) |
| | 4 | 84 | | { |
| | 4 | 85 | | _options.Scenarios.TryAdd(mapping.Scenario, new ScenarioState |
| | 4 | 86 | | { |
| | 4 | 87 | | Name = mapping.Scenario |
| | 4 | 88 | | }); |
| | 4 | 89 | | } |
| | 30 | 90 | | } |
| | | 91 | | |
| | 50 | 92 | | result = _mappingMatcher.Match(request); |
| | 50 | 93 | | var targetMapping = result.TargetMapping; |
| | | 94 | | |
| | 50 | 95 | | if (targetMapping == null) |
| | 13 | 96 | | { |
| | 13 | 97 | | logRequest = true; |
| | 13 | 98 | | _options.Logger.Warn("HttpStatusCode set to 404 : No matching mapping found"); |
| | 13 | 99 | | response = ResponseMessageBuilder.Create("No matching mapping found", 404); |
| | 13 | 100 | | return; |
| | | 101 | | } |
| | | 102 | | |
| | 37 | 103 | | logRequest = !targetMapping.IsAdminInterface; |
| | | 104 | | |
| | 37 | 105 | | if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null) |
| | 2 | 106 | | { |
| | 2 | 107 | | bool present = request.Headers.TryGetValue(HttpKnownHeaderNames.Authorization, out WireMockList<stri |
| | 2 | 108 | | if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfec |
| | 2 | 109 | | { |
| | 2 | 110 | | _options.Logger.Error("HttpStatusCode set to 401"); |
| | 2 | 111 | | response = ResponseMessageBuilder.Create(null, 401); |
| | 2 | 112 | | return; |
| | | 113 | | } |
| | 0 | 114 | | } |
| | | 115 | | |
| | 35 | 116 | | if (!targetMapping.IsAdminInterface && _options.RequestProcessingDelay > TimeSpan.Zero) |
| | 1 | 117 | | { |
| | 1 | 118 | | await Task.Delay(_options.RequestProcessingDelay.Value); |
| | 1 | 119 | | } |
| | | 120 | | |
| | 35 | 121 | | response = await targetMapping.ResponseToAsync(request); |
| | | 122 | | |
| | 35 | 123 | | if (targetMapping.Scenario != null) |
| | 9 | 124 | | { |
| | 9 | 125 | | _options.Scenarios[targetMapping.Scenario].NextState = targetMapping.NextState; |
| | 9 | 126 | | _options.Scenarios[targetMapping.Scenario].Started = true; |
| | 9 | 127 | | _options.Scenarios[targetMapping.Scenario].Finished = targetMapping.NextState == null; |
| | 9 | 128 | | } |
| | 35 | 129 | | } |
| | 0 | 130 | | catch (Exception ex) |
| | 0 | 131 | | { |
| | 0 | 132 | | _options.Logger.Error("HttpStatusCode set to 500"); |
| | 0 | 133 | | response = ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500); |
| | 0 | 134 | | } |
| | | 135 | | finally |
| | 50 | 136 | | { |
| | 50 | 137 | | var log = new LogEntry |
| | 50 | 138 | | { |
| | 50 | 139 | | Guid = Guid.NewGuid(), |
| | 50 | 140 | | RequestMessage = request, |
| | 50 | 141 | | ResponseMessage = response, |
| | 50 | 142 | | MappingGuid = result.TargetMapping?.Guid, |
| | 50 | 143 | | MappingTitle = result.TargetMapping?.Title, |
| | 50 | 144 | | RequestMatchResult = result.RequestMatchResult |
| | 50 | 145 | | }; |
| | | 146 | | |
| | 50 | 147 | | LogRequest(log, logRequest); |
| | | 148 | | |
| | 50 | 149 | | await _responseMapper.MapAsync(response, ctx.Response); |
| | 50 | 150 | | } |
| | | 151 | | |
| | 35 | 152 | | await CompletedTask; |
| | 50 | 153 | | } |
| | | 154 | | |
| | | 155 | | private void LogRequest(LogEntry entry, bool addRequest) |
| | 49 | 156 | | { |
| | 49 | 157 | | _options.Logger.DebugRequestResponse(LogEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__adm |
| | | 158 | | |
| | 50 | 159 | | if (addRequest) |
| | 40 | 160 | | { |
| | 40 | 161 | | _options.LogEntries.Add(entry); |
| | 40 | 162 | | } |
| | | 163 | | |
| | 50 | 164 | | if (_options.MaxRequestLogCount != null) |
| | 3 | 165 | | { |
| | 3 | 166 | | var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value; |
| | 8 | 167 | | for (int i = 0; i < amount; i++) |
| | 1 | 168 | | { |
| | 1 | 169 | | _options.LogEntries.RemoveAt(0); |
| | 1 | 170 | | } |
| | 3 | 171 | | } |
| | | 172 | | |
| | 50 | 173 | | if (_options.RequestLogExpirationDuration != null) |
| | 1 | 174 | | { |
| | 1 | 175 | | var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value); |
| | | 176 | | |
| | 4 | 177 | | for (var i = _options.LogEntries.Count - 1; i >= 0; i--) |
| | 1 | 178 | | { |
| | 1 | 179 | | var le = _options.LogEntries[i]; |
| | 1 | 180 | | if (le.RequestMessage.DateTime <= checkTime) |
| | 1 | 181 | | { |
| | 1 | 182 | | _options.LogEntries.RemoveAt(i); |
| | 1 | 183 | | } |
| | 1 | 184 | | } |
| | 1 | 185 | | } |
| | 50 | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |