Example #1
0
        public (IBodyData BodyData, IDictionary <string, WireMockList <string> > Headers) Transform(RequestMessage originalRequestMessage, ResponseMessage originalResponseMessage, IBodyData bodyData, IDictionary <string, WireMockList <string> > headers)
        {
            var transformerContext = _factory.Create();

            var model = new
            {
                request  = originalRequestMessage,
                response = originalResponseMessage
            };

            IBodyData newBodyData = null;

            if (bodyData?.DetectedBodyType != null)
            {
                newBodyData = TransformBodyData(transformerContext, model, bodyData, false);
            }

            return(newBodyData, TransformHeaders(transformerContext, model, headers));
        }
Example #2
0
        public ResponseMessage Transform(RequestMessage requestMessage, ResponseMessage original, bool useTransformerForBodyAsFile)
        {
            var handlebarsContext = _factory.Create();

            var responseMessage = new ResponseMessage();

            var model = new { request = requestMessage };

            switch (original.BodyData?.DetectedBodyType)
            {
            case BodyType.Json:
                TransformBodyAsJson(handlebarsContext, model, original, responseMessage);
                break;

            case BodyType.File:
                TransformBodyAsFile(handlebarsContext, model, original, responseMessage, useTransformerForBodyAsFile);
                break;

            case BodyType.String:
                responseMessage.BodyOriginal = original.BodyData.BodyAsString;
                TransformBodyAsString(handlebarsContext, model, original, responseMessage);
                break;
            }

            responseMessage.FaultType       = original.FaultType;
            responseMessage.FaultPercentage = original.FaultPercentage;

            // Headers
            var newHeaders = new Dictionary <string, WireMockList <string> >();

            foreach (var header in original.Headers)
            {
                var headerKey            = handlebarsContext.ParseAndRender(header.Key, model);
                var templateHeaderValues = header.Value
                                           .Select(text => handlebarsContext.ParseAndRender(text, model))
                                           .ToArray();

                newHeaders.Add(headerKey, new WireMockList <string>(templateHeaderValues));
            }

            responseMessage.Headers = newHeaders;

            switch (original.StatusCode)
            {
            case int statusCodeAsInteger:
                responseMessage.StatusCode = statusCodeAsInteger;
                break;

            case string statusCodeAsString:
                responseMessage.StatusCode = handlebarsContext.ParseAndRender(statusCodeAsString, model);
                break;
            }

            return(responseMessage);
        }