コード例 #1
0
 /// <inheritdoc />
 public override async Task Invoke(HttpContext context)
コード例 #2
0
 public override Task Invoke(IContext p_ctx)
コード例 #3
0
        public Task Invoke(IContext p_ctx)
#endif
        {
            return(InvokeInternal(p_ctx));
        }
コード例 #4
0
 /// <summary>
 /// Checks whether the request can be handled by the concrete query
 /// middleware.
 /// </summary>
 /// <param name="context">An OWIN context.</param>
 /// <returns>
 /// A value indicating whether the request can be handled.
 /// </returns>
 protected abstract bool CanHandleRequest(HttpContext context);
コード例 #5
0
        private async Task InvokeInternal(IContext ctx)
        {
            var request = await _requestMapper.MapAsync(ctx.Request, _options);

            bool            logRequest = false;
            ResponseMessage response   = null;

            (MappingMatcherResult Match, MappingMatcherResult Partial)result = (null, null);
            try
            {
                foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))
                {
                    // Set scenario start
                    if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
                    {
                        _options.Scenarios.TryAdd(mapping.Scenario, new ScenarioState
                        {
                            Name = mapping.Scenario
                        });
                    }
                }

                result = _mappingMatcher.FindBestMatch(request);

                var targetMapping = result.Match?.Mapping;
                if (targetMapping == null)
                {
                    logRequest = true;
                    _options.Logger.Warn("HttpStatusCode set to 404 : No matching mapping found");
                    response = ResponseMessageBuilder.Create("No matching mapping found", 404);
                    return;
                }

                logRequest = targetMapping.LogMapping;

                if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
                {
                    bool present = request.Headers.TryGetValue(HttpKnownHeaderNames.Authorization, out WireMockList <string> authorization);
                    if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect)
                    {
                        _options.Logger.Error("HttpStatusCode set to 401");
                        response = ResponseMessageBuilder.Create(null, 401);
                        return;
                    }
                }

                if (!targetMapping.IsAdminInterface && _options.RequestProcessingDelay > TimeSpan.Zero)
                {
                    await Task.Delay(_options.RequestProcessingDelay.Value);
                }

                response = await targetMapping.ProvideResponseAsync(request);

                var responseBuilder = targetMapping.Provider as Response;

                if (responseBuilder?.ProxyAndRecordSettings?.SaveMapping == true || targetMapping?.Settings?.ProxyAndRecordSettings?.SaveMapping == true)
                {
                    _options.Mappings.TryAdd(targetMapping.Guid, targetMapping);
                }

                if (responseBuilder?.ProxyAndRecordSettings?.SaveMappingToFile == true || targetMapping?.Settings?.ProxyAndRecordSettings?.SaveMappingToFile == true)
                {
                    var matcherMapper      = new MatcherMapper(targetMapping.Settings);
                    var mappingConverter   = new MappingConverter(matcherMapper);
                    var mappingToFileSaver = new MappingToFileSaver(targetMapping.Settings, mappingConverter);

                    mappingToFileSaver.SaveMappingToFile(targetMapping);
                }

                if (targetMapping.Scenario != null)
                {
                    UpdateScenarioState(targetMapping);
                }
            }
            catch (Exception ex)
            {
                _options.Logger.Error($"Providing a Response for Mapping '{result.Match?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
                response = ResponseMessageBuilder.Create(ex.Message, 500);
            }
            finally
            {
                var log = new LogEntry
                {
                    Guid            = Guid.NewGuid(),
                    RequestMessage  = request,
                    ResponseMessage = response,

                    MappingGuid        = result.Match?.Mapping?.Guid,
                    MappingTitle       = result.Match?.Mapping?.Title,
                    RequestMatchResult = result.Match?.RequestMatchResult,

                    PartialMappingGuid  = result.Partial?.Mapping?.Guid,
                    PartialMappingTitle = result.Partial?.Mapping?.Title,
                    PartialMatchResult  = result.Partial?.RequestMatchResult
                };

                LogRequest(log, logRequest);

                await _responseMapper.MapAsync(response, ctx.Response);
            }

            await CompletedTask;
        }
コード例 #6
0
 private static bool HasQueryParameter(HttpContext context)
 {
     return(context.Request.QueryString.HasValue &&
            context.Request.Query.ToDictionary(i => i.Key, i => i.Value)
            .ContainsKey(_queryIdentifier));
 }
コード例 #7
0
        private async Task InvokeInternal(IContext ctx)
        {
#if !USE_ASPNETCORE
            if (ctx.Get <bool>("gotoNext"))
            {
                await Next?.Invoke(ctx);

                return;
            }
#else
#endif

            var request = await _requestMapper.MapAsync(ctx.Request, _options);

            bool                 logRequest = false;
            ResponseMessage      response   = null;
            MappingMatcherResult result     = null;
            try
            {
                foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))
                {
                    // Set start
                    if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
                    {
                        _options.Scenarios.TryAdd(mapping.Scenario, new ScenarioState
                        {
                            Name = mapping.Scenario
                        });
                    }
                }

                result = _mappingMatcher.FindBestMatch(request);

                var targetMapping = result?.Mapping;
                if (targetMapping == null)
                {
                    logRequest = true;
                    _options.Logger.Warn("HttpStatusCode set to 404 : No matching mapping found");
                    response = ResponseMessageBuilder.Create("No matching mapping found", 404);
                    return;
                }

                logRequest = targetMapping.LogMapping;

                if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null)
                {
                    bool present = request.Headers.TryGetValue(HttpKnownHeaderNames.Authorization, out WireMockList <string> authorization);
                    if (!present || _options.AuthorizationMatcher.IsMatch(authorization.ToString()) < MatchScores.Perfect)
                    {
                        _options.Logger.Error("HttpStatusCode set to 401");
                        response = ResponseMessageBuilder.Create(null, 401);
                        return;
                    }
                }

                if (!targetMapping.IsAdminInterface && _options.RequestProcessingDelay > TimeSpan.Zero)
                {
                    await Task.Delay(_options.RequestProcessingDelay.Value);
                }

                response = await targetMapping.ProvideResponseAsync(request);

                if (targetMapping.Scenario != null)
                {
                    _options.Scenarios[targetMapping.Scenario].NextState = targetMapping.NextState;
                    _options.Scenarios[targetMapping.Scenario].Started   = true;
                    _options.Scenarios[targetMapping.Scenario].Finished  = targetMapping.NextState == null;
                }
            }
            catch (Exception ex)
            {
                _options.Logger.Error($"Providing a Response for Mapping '{result?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
                response = ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500);
            }
            finally
            {
                var log = new LogEntry
                {
                    Guid               = Guid.NewGuid(),
                    RequestMessage     = request,
                    ResponseMessage    = response,
                    MappingGuid        = result?.Mapping?.Guid,
                    MappingTitle       = result?.Mapping?.Title,
                    RequestMatchResult = result?.RequestMatchResult
                };

                LogRequest(log, logRequest);

                await _responseMapper.MapAsync(response, ctx.Response);
            }

            await CompletedTask;
        }
コード例 #8
0
 protected override bool CanHandleRequest(HttpContext context)
 {
     return(string.Equals(
                context.Request.Method, _postMethod,
                StringComparison.Ordinal));
 }
コード例 #9
0
 /// <inheritdoc />
 protected override bool CanHandleRequest(HttpContext context)
 {
     return(string.Equals(context.Request.Method, _getMethod,
                          StringComparison.Ordinal) && HasQueryParameter(context));
 }
コード例 #10
0
        public UserManagerApp Create(IdentityFactoryOptions <UserManagerApp> identitiyFactorOptions, Microsoft.Owin.IOwinContext owinContext)
        {
            DataContext    db   = owinContext.Get <DataContext>();
            UserManagerApp user = new UserManagerApp(new UserStore <Kullanici>(db));

            return(user);
        }
コード例 #11
0
 public Task OnAfterHandleRequestAsync(Microsoft.Owin.IOwinContext context, Configuration.Hosting.ResourceContext resourceContext)
 {
     Logger.Warn("Entered not implemeted section");
     throw new NotImplementedException();
 }
コード例 #12
0
 /// <summary>
 /// Creates a new query request.
 /// </summary>
 /// <param name="context">An OWIN context.</param>
 /// <returns>A new query request.</returns>
 protected abstract Task<IQueryRequestBuilder> CreateQueryRequestAsync(
     HttpContext context);
コード例 #13
0
 protected static T GetService<T>(HttpContext context) =>
     (T)context.RequestServices.GetService(typeof(T));
コード例 #14
0
 public static bool IsValidPath(
     this HttpContext context,
     PathString path)
 {
     return(context.Request.Path.StartsWithSegments(path));
 }
コード例 #15
0
 protected abstract Task ExecuteRequestAsync(
     HttpContext context,
     IServiceProvider services);
コード例 #16
0
 private async Task HandleRequestAsync(
     HttpContext context)
 {
     await ExecuteRequestAsync(context, context.RequestServices)
     .ConfigureAwait(false);
 }