Пример #1
0
        private async Task PerformCallbacks(
            HttpContext httpContext,
            IMockacoContext mockacoContext,
            IScriptContext scriptContext,
            ITemplateTransformer templateTransformer,
            MockacoOptions options)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();

                var template = await templateTransformer.Transform(mockacoContext.Mock.RawTemplate, scriptContext);

                var callbackTasks = new List <Task>();

                foreach (var callbackTemplate in template.Callbacks)
                {
                    callbackTasks.Add(PerformCallback(httpContext, callbackTemplate, options, stopwatch.ElapsedMilliseconds));
                }

                await Task.WhenAll(callbackTasks);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error preparing callback(s)");
            }
        }
        public async Task Invoke(
            HttpContext httpContext,
            IMockacoContext mockacoContext,
            IScriptContext scriptContext,
            IMockProvider mockProvider,
            ITemplateTransformer templateTransformer,
            IEnumerable <IRequestMatcher> requestMatchers
            )
        {
            LogHttpContext(httpContext);

            AttachRequestToScriptContext(httpContext, mockacoContext, scriptContext);

            if (mockacoContext.Errors.Any())
            {
                return;
            }

            foreach (var mock in mockProvider.GetMocks())
            {
                if (requestMatchers.All(_ => _.IsMatch(httpContext.Request, mock)))
                {
                    scriptContext.AttachRouteParameters(httpContext.Request, mock);

                    var template = await templateTransformer.Transform(mock.RawTemplate, scriptContext);

                    var conditionIsMatch = template.Request?.Condition ?? true;

                    if (conditionIsMatch)
                    {
                        _logger.LogInformation("Incoming request matched {mock}", mock);

                        mockacoContext.Mock = mock;
                        mockacoContext.TransformedTemplate = template;

                        await _next(httpContext);

                        return;
                    }
                    else
                    {
                        _logger.LogDebug("Incoming request didn't match condition for {mock}", mock);
                    }
                }
                else
                {
                    _logger.LogDebug("Incoming request didn't match {mock}", mock);
                }
            }

            _logger.LogInformation("Incoming request didn't match any mock");

            mockacoContext.Errors.Add(new Error("Incoming request didn't match any mock"));
        }
        protected virtual string FillTemplateProperty(ITemplateProvider provider, ITemplateTransformer transformer,
                                                      TemplateData templateData)
        {
            if (provider == null || transformer == null)
            {
                return(null);
            }

            Dictionary <string, string> filledTemplates = transformer.Transform(
                provider, new List <TemplateData> {
                templateData
            });

            return(filledTemplates[templateData.Language ?? string.Empty]);
        }
        protected virtual List <string> FillTemplateProperty(ITemplateProvider provider, ITemplateTransformer transformer,
                                                             List <Subscriber <TKey> > subscribers, List <TemplateData> templateData)
        {
            if (provider == null || transformer == null)
            {
                return(subscribers
                       .Select(x => (string)null)
                       .ToList());
            }

            Dictionary <string, string> filledTemplates = transformer.Transform(provider, templateData);

            return(subscribers
                   .Select(subscriber => filledTemplates[subscriber.Language ?? string.Empty])
                   .ToList());
        }
        public async Task <bool> IsMatch(HttpRequest httpRequest, Mock mock)
        {
            var conditionMatcherScriptContext = new ScriptContext(_fakerFactory, _requestBodyFactory, _globalVarialeStorage);

            AttachRequestToScriptContext(httpRequest.HttpContext, _mockacoContext, conditionMatcherScriptContext);

            if (_mockacoContext.Errors.Any())
            {
                return(false);
            }

            await conditionMatcherScriptContext.AttachRouteParameters(httpRequest, mock);

            var template = await _templateTransformer.Transform(mock.RawTemplate, conditionMatcherScriptContext);

            var isMatch = template.Request?.Condition ?? true;

            return(isMatch);
        }
Пример #6
0
        private static async Task <CallbackTemplate> PrepareCallbackTemplate(IMockacoContext mockacoContext, IScriptContext scriptContext, ITemplateTransformer templateTransformer)
        {
            var template = await templateTransformer.Transform(mockacoContext.Mock.RawTemplate, scriptContext);

            return(template.Callback);
        }