private static string GetRawContentString(BundleItem item)
        {
            switch (item.ContentFormat?.ToUpperInvariant())
            {
            case "BASE64":
                byte[] decoded = Convert.FromBase64String(item.ContentString);
                return(Encoding.UTF8.GetString(decoded));

            case "JSON":
                return(item.ContentJson.ToString());

            case null:
            case "":
            case "STRING":
                return(item.ContentString);

            default:
                throw new NotSupportedException($"Content format '{item.ContentFormat}' for bundle item with Id '{item.Id}' is not supported.");
            }
        }
        private static void ValidateItem(BundleItem item, out Uri uri, out Version version)
        {
            version = null;

            if (item.Uri == null)
            {
                throw new InvalidOperationException($"Bundle item with Id '{item.Id}' has no URI configured.");
            }

            string templated = TemplateString(item.Uri, item.TemplateValues);

            if (!Uri.TryCreate(templated, UriKind.Absolute, out uri))
            {
                throw new InvalidOperationException($"Bundle item with Id '{item.Id}' has an invalid absolute URI '{item.Uri}' configured.");
            }

            if (item.Version != null && !Version.TryParse(item.Version, out version))
            {
                throw new InvalidOperationException($"Bundle item with Id '{item.Id}' has an invalid version '{item.Version}' configured.");
            }
        }
        private static HttpRequestInterceptionBuilder SetContent(this HttpRequestInterceptionBuilder builder, BundleItem item)
        {
            string content = GetRawContentString(item) ?? string.Empty;

            if (item.TemplateValues?.Count > 0)
            {
                content = TemplateString(content, item.TemplateValues);
            }

            return(builder.WithContent(content));
        }
        public static HttpRequestInterceptionBuilder FromItem(
            BundleItem item,
            IEnumerable <KeyValuePair <string, string> > templateValues)
        {
            // Override the template values in the JSON with any user-specified values
            if (item.TemplateValues?.Count > 0)
            {
                foreach (var pair in templateValues)
                {
                    item.TemplateValues[pair.Key] = pair.Value;
                }
            }

            ValidateItem(item, out Uri uri, out Version version);

            var builder = new HttpRequestInterceptionBuilder().ForUri(uri);

            if (item.Method != null)
            {
                builder.ForMethod(new System.Net.Http.HttpMethod(item.Method));
            }

            if (item.RequestHeaders?.Count > 0)
            {
                builder.ForRequestHeaders(TemplateHeaders(item.RequestHeaders, item.TemplateValues));
            }

            if (version != null)
            {
                builder.WithVersion(version);
            }

            if (!string.IsNullOrEmpty(item.Status))
            {
                if (!Enum.TryParse(item.Status, true, out HttpStatusCode httpStatusCode))
                {
                    throw new InvalidOperationException($"Bundle item with Id '{item.Id}' has an invalid HTTP status code '{item.Status}' configured.");
                }

                builder.WithStatus(httpStatusCode);
            }

            if (item.ResponseHeaders?.Count > 0)
            {
                builder.WithResponseHeaders(TemplateHeaders(item.ResponseHeaders, item.TemplateValues));
            }

            if (item.ContentHeaders?.Count > 0)
            {
                builder.WithContentHeaders(TemplateHeaders(item.ContentHeaders, item.TemplateValues));
            }

            if (item.Priority.HasValue)
            {
                builder.HavingPriority(item.Priority.Value);
            }

            if (item.IgnorePath)
            {
                builder.IgnoringPath(ignorePath: true);
            }

            if (item.IgnoreQuery)
            {
                builder.IgnoringQuery(ignoreQuery: true);
            }

            return(builder.SetContent(item));
        }