Exemple #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)");
            }
        }
Exemple #2
0
 public MailController(INotificationsSettings notificationsSettings, ISmtpClient mail, ITemplateTransformer templateTransformer, ILogger logger)
 {
     _notificationsSettings = notificationsSettings ?? throw new ArgumentNullException(nameof(notificationsSettings));
     _mail = mail ?? throw new ArgumentNullException(nameof(mail));
     _templateTransformer = templateTransformer ?? throw new ArgumentNullException(nameof(templateTransformer));
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }
        private async Task PerformCallback(
            HttpContext httpContext,
            IMockacoContext mockacoContext,
            IScriptContext scriptContext,
            ITemplateTransformer templateTransformer,
            MockacoOptions options)
        {
            try
            {
                var stopwatch = Stopwatch.StartNew();

                var callbackTemplate = await PrepareCallbackTemplate(mockacoContext, scriptContext, templateTransformer);

                var request = PrepareHttpRequest(callbackTemplate, options);

                var httpClient = PrepareHttpClient(httpContext, callbackTemplate);

                await DelayRequest(callbackTemplate, stopwatch.ElapsedMilliseconds);

                stopwatch.Restart();

                _logger.LogDebug("Callback started");

                await PerformRequest(request, httpClient);

                _logger.LogDebug("Callback finished in {0} ms", stopwatch.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Callback error");
            }
        }
        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"));
        }
 public RequestConditionMatcher(
     ITemplateTransformer templateTransformer,
     IFakerFactory fakerFactory,
     IRequestBodyFactory requestBodyFactory,
     IMockacoContext mockacoContext,
     IGlobalVariableStorage globalVariableStoreage,
     ILogger <RequestConditionMatcher> logger)
 {
     _templateTransformer  = templateTransformer;
     _fakerFactory         = fakerFactory;
     _requestBodyFactory   = requestBodyFactory;
     _mockacoContext       = mockacoContext;
     _globalVarialeStorage = globalVariableStoreage;
     _logger = logger;
 }
        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]);
        }
        public Task Invoke(
            HttpContext httpContext,
            IMockacoContext mockacoContext,
            IScriptContext scriptContext,
            ITemplateTransformer templateTransformer,
            IOptionsSnapshot <MockacoOptions> options)
        {
            if (mockacoContext.TransformedTemplate?.Callback == null)
            {
                return(Task.CompletedTask);
            }

            httpContext.Response.OnCompleted(
                () =>
            {
                var fireAndForgetTask = PerformCallback(httpContext, mockacoContext, scriptContext, templateTransformer, options.Value);
                return(Task.CompletedTask);
            });

            return(Task.CompletedTask);
        }
Exemple #8
0
        public Task Invoke(
            HttpContext httpContext,
            IMockacoContext mockacoContext,
            IScriptContext scriptContext,
            ITemplateTransformer templateTransformer,
            IOptionsSnapshot <MockacoOptions> options)
        {
            if (mockacoContext.TransformedTemplate?.Callbacks?.Any() != true)
            {
                return(Task.CompletedTask);
            }

            httpContext.Response.OnCompleted(
                () =>
            {
                //TODO Refactor to avoid method with too many parameters (maybe a CallbackRunnerFactory?)
                var fireAndForgetTask = PerformCallbacks(httpContext, mockacoContext, scriptContext, templateTransformer, options.Value);
                return(Task.CompletedTask);
            });

            return(Task.CompletedTask);
        }
 //инициализация
 public NotificationSettings(ITemplateProvider templateProvider, ITemplateTransformer templateTransformer)
 {
     TemplateProvider = templateProvider;
     TemplateTransformer = templateTransformer;
 }
Exemple #10
0
 public MailKitSmtpClient(ITemplateTransformer templateTransformer, ILogger logger, ISmtpSettings smtpSettings)
 {
     _viewRenderService = templateTransformer ?? throw new ArgumentNullException(nameof(templateTransformer));
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
     _smtpSettings = smtpSettings ?? throw new ArgumentNullException(nameof(smtpSettings));
 }
        private static async Task <CallbackTemplate> PrepareCallbackTemplate(IMockacoContext mockacoContext, IScriptContext scriptContext, ITemplateTransformer templateTransformer)
        {
            var template = await templateTransformer.Transform(mockacoContext.Mock.RawTemplate, scriptContext);

            return(template.Callback);
        }
        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());
        }