Exemplo n.º 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)");
            }
        }
Exemplo n.º 2
0
        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");
            }
        }
        private async Task PrepareResponse(
            HttpResponse httpResponse,
            Template transformedTemplate,
            IResponseBodyFactory responseBodyFactory,
            MockacoOptions options)
        {
            httpResponse.StatusCode = GetResponseStatusFromTemplate(transformedTemplate.Response, options);

            AddHeadersFromTemplate(httpResponse, transformedTemplate.Response, options);

            var bodyBytes = await responseBodyFactory.GetResponseBodyBytesFromTemplate(transformedTemplate.Response);

            if (bodyBytes == default)
            {
                return;
            }

            await httpResponse.Body.WriteAsync(bodyBytes, 0, bodyBytes.Length, default);
        }
        private void AddHeadersFromTemplate(HttpResponse response, ResponseTemplate responseTemplate, MockacoOptions options)
        {
            if (responseTemplate?.Headers != null)
            {
                foreach (var header in responseTemplate.Headers)
                {
                    response.Headers.Add(header.Key, header.Value);
                }
            }

            if (string.IsNullOrEmpty(response.ContentType))
            {
                response.ContentType = options.DefaultHttpContentType;
            }
        }
 private int GetResponseStatusFromTemplate(ResponseTemplate responseTemplate, MockacoOptions options)
 {
     return(responseTemplate?.Status == default(HttpStatusCode)
                     ? (int)options.DefaultHttpStatusCode
                     : (int)responseTemplate.Status);
 }
Exemplo n.º 6
0
        private async Task PerformCallback(HttpContext httpContext, CallbackTemplate callbackTemplate, MockacoOptions options, long elapsedMilliseconds)
        {
            try
            {
                var request = PrepareHttpRequest(callbackTemplate, options);

                var httpClient = PrepareHttpClient(httpContext, callbackTemplate);

                await DelayRequest(callbackTemplate, elapsedMilliseconds);

                var stopwatch = Stopwatch.StartNew();

                _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");
            }
        }
Exemplo n.º 7
0
        private static void PrepareHeaders(CallbackTemplate callBackTemplate, HttpRequestMessage httpRequest, MockacoOptions options)
        {
            if (callBackTemplate.Headers != null)
            {
                foreach (var header in callBackTemplate.Headers.Where(h => h.Key != HttpHeaders.ContentType))
                {
                    if (httpRequest.Headers.Contains(header.Key))
                    {
                        httpRequest.Headers.Remove(header.Key);
                    }

                    httpRequest.Headers.Add(header.Key, header.Value);
                }
            }

            if (!httpRequest.Headers.Accept.Any())
            {
                httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(options.DefaultHttpContentType));
            }
        }
Exemplo n.º 8
0
        private static HttpRequestMessage PrepareHttpRequest(CallbackTemplate callbackTemplate, MockacoOptions options)
        {
            var request = new HttpRequestMessage(new HttpMethod(callbackTemplate.Method), callbackTemplate.Url);

            var formatting = callbackTemplate.Indented.GetValueOrDefault(true) ? Formatting.Indented : default;

            if (callbackTemplate.Body != null)
            {
                request.Content = callbackTemplate.Headers?.ContainsKey(HttpHeaders.ContentType) == true
                    ? new StringContent(callbackTemplate.Body.ToString(), Encoding.UTF8, callbackTemplate.Headers[HttpHeaders.ContentType])
                    : new StringContent(callbackTemplate.Body.ToString(formatting));
            }

            PrepareHeaders(callbackTemplate, request, options);

            return(request);
        }