public void should_return_base_url()
        {
            var baseUrl = "http://www.bbc.co.uk";

            _finder.Setup(x => x.Find()).Returns(baseUrl);
            var result = _placeholders.Get("{BaseUrl}");

            result.Data.ShouldBe(baseUrl);
        }
        public void should_return_value_from_underlying_placeholders()
        {
            var          baseUrl = "http://www.bbc.co.uk";
            const string key     = "{BaseUrl}";

            _basePlaceholders.Setup(x => x.Get(key)).Returns(new OkResponse <string>(baseUrl));
            var result = _placeholders.Get(key);

            result.Data.ShouldBe(baseUrl);
        }
        public Response <string> Get(string key)
        {
            var placeholderResponse = _placeholders.Get(key);

            if (!placeholderResponse.IsError)
            {
                return(placeholderResponse);
            }

            return(GetFromConfig(CleanKey(key)));
        }
        public Response Replace(HttpResponseMessage response, List <HeaderFindAndReplace> fAndRs, HttpRequestMessage request)
        {
            foreach (var f in fAndRs)
            {
                //if the response headers contain a matching find and replace
                if (response.Headers.TryGetValues(f.Key, out var values))
                {
                    //check to see if it is a placeholder in the find...
                    var placeholderValue = _placeholders.Get(f.Find, request);

                    if (!placeholderValue.IsError)
                    {
                        //if it is we need to get the value of the placeholder
                        //var find = replacePlaceholder(httpRequestMessage);
                        var replaced = values.ToList()[f.Index].Replace(placeholderValue.Data, f.Replace.LastCharAsForwardSlash());
                        response.Headers.Remove(f.Key);
                        response.Headers.Add(f.Key, replaced);
                    }
                    else
                    {
                        var replaced = values.ToList()[f.Index].Replace(f.Find, f.Replace);
                        response.Headers.Remove(f.Key);
                        response.Headers.Add(f.Key, replaced);
                    }
                }
            }

            return(new OkResponse());
        }
        private Response <HeaderFindAndReplace> Map(KeyValuePair <string, string> input)
        {
            var findAndReplace = input.Value.Split(",");

            var replace = findAndReplace[1].TrimStart();

            var startOfPlaceholder = replace.IndexOf("{");

            if (startOfPlaceholder > -1)
            {
                var endOfPlaceholder = replace.IndexOf("}", startOfPlaceholder);

                var placeholder = replace.Substring(startOfPlaceholder, startOfPlaceholder + (endOfPlaceholder + 1));

                var value = _placeholders.Get(placeholder);

                if (value.IsError)
                {
                    return(new ErrorResponse <HeaderFindAndReplace>(value.Errors));
                }

                replace = replace.Replace(placeholder, value.Data);
            }

            var hAndr = new HeaderFindAndReplace(input.Key, findAndReplace[0], replace, 0);

            return(new OkResponse <HeaderFindAndReplace>(hAndr));
        }
Beispiel #6
0
        public void SetHeadersOnDownstreamRequest(IEnumerable <AddHeader> headers, HttpContext context)
        {
            var requestHeader = context.Request.Headers;

            foreach (var header in headers)
            {
                if (requestHeader.ContainsKey(header.Key))
                {
                    requestHeader.Remove(header.Key);
                }

                if (header.Value.StartsWith("{") && header.Value.EndsWith("}"))
                {
                    var value = _placeholders.Get(header.Value);

                    if (value.IsError)
                    {
                        _logger.LogWarning($"Unable to add header to response {header.Key}: {header.Value}");
                        continue;
                    }

                    requestHeader.Add(header.Key, new StringValues(value.Data));
                }
                else
                {
                    requestHeader.Add(header.Key, header.Value);
                }
            }
        }
Beispiel #7
0
        public void Add(List <AddHeader> addHeaders, DownstreamResponse response)
        {
            foreach (var add in addHeaders)
            {
                if (add.Value.StartsWith('{') && add.Value.EndsWith('}'))
                {
                    var value = _placeholders.Get(add.Value);

                    if (value.IsError)
                    {
                        _logger.LogWarning($"Unable to add header to response {add.Key}: {add.Value}");
                        continue;
                    }

                    response.Headers.Add(new Header(add.Key, new List <string> {
                        value.Data
                    }));
                }
                else
                {
                    response.Headers.Add(new Header(add.Key, new List <string> {
                        add.Value
                    }));
                }
            }
        }
Beispiel #8
0
        public Response Replace(DownstreamContext context, List <HeaderFindAndReplace> fAndRs)
        {
            var response = context.DownstreamResponse;
            var request  = context.DownstreamRequest;

            foreach (var f in fAndRs)
            {
                var dict = response.Headers.ToDictionary(x => x.Key);

                //if the response headers contain a matching find and replace
                if (dict.TryGetValue(f.Key, out var values))
                {
                    //check to see if it is a placeholder in the find...
                    var placeholderValue = _placeholders.Get(f.Find, request);

                    if (!placeholderValue.IsError)
                    {
                        //if it is we need to get the value of the placeholder
                        var replaced = values.Values.ToList()[f.Index].Replace(placeholderValue.Data, f.Replace.LastCharAsForwardSlash());

                        response.Headers.Remove(response.Headers.First(item => item.Key == f.Key));
                        response.Headers.Add(new Header(f.Key, new List <string> {
                            replaced
                        }));
                    }
                    else
                    {
                        var replaced = values.Values.ToList()[f.Index].Replace(f.Find, f.Replace);

                        response.Headers.Remove(response.Headers.First(item => item.Key == f.Key));
                        response.Headers.Add(new Header(f.Key, new List <string> {
                            replaced
                        }));
                    }
                }
            }

            return(new OkResponse());
        }
Beispiel #9
0
        public void Add(List <AddHeader> addHeaders, HttpResponseMessage response)
        {
            foreach (var add in addHeaders)
            {
                if (add.Value.StartsWith('{') && add.Value.EndsWith('}'))
                {
                    var value = _placeholders.Get(add.Value);

                    if (value.IsError)
                    {
                        _logger.LogError($"Unable to add header to response {add.Key}: {add.Value}");
                        continue;
                    }

                    response.Headers.TryAddWithoutValidation(add.Key, value.Data);
                }
                else
                {
                    response.Headers.TryAddWithoutValidation(add.Key, add.Value);
                }
            }
        }