Example #1
0
        /// <summary>
        /// Gets the configuration by resource.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="method">Http method.</param>
        /// <returns>JsonConfigurationItem instance if found, otherwise null</returns>
        public JsonConfigurationItem GetConfigurationByResource(string resource, string method)
        {
            var separator = new[] { '/' };

            method   = method.ToLower();
            resource = resource.Substring(1); // remove '/'
            var item = this.Configurations.FirstOrDefault(e => e.Resource.Equals(resource.ToLower()));

            if (item != null && item.Method == method)
            {
                return(item);
            }

            var resourceParts            = resource.Split(separator);
            JsonConfigurationItem result = null;

            Parallel.ForEach(this.Configurations, conf =>
            {
                var confParts = conf.Resource.Split(separator);
                var res       = this.TryMatchResources(confParts, resourceParts);
                if (res && conf.Method == method)
                {
                    result = conf;
                    return;
                }
            });

            return(result);
        }
Example #2
0
        /// <summary>
        /// Trasforms the specified source path.
        /// </summary>
        /// <param name="sourcePath">The source path.</param>
        /// <param name="targetPath">The target path.</param>
        public void Trasform(string sourcePath, string targetPath)
        {
            var jsonConfigs = new List <JsonConfigurationItem>();
            var source      = JObject.Parse(File.ReadAllText(sourcePath));
            var basePath    = source["basePath"];

            foreach (var api in source["apis"])
            {
                var path = api["path"].Value <string>();

                foreach (var operation in api["operations"])
                {
                    var cfg = new JsonConfigurationItem
                    {
                        Name         = path,
                        Resource     = string.Format("{0}{1}", basePath, path),
                        MinDelay     = 0,
                        RandomDelay  = 0,
                        DoNotRespond = false,
                        Method       = operation["method"].Value <string>(),
                        Response     = new ResponseItem()
                    };

                    jsonConfigs.Add(cfg);
                }
            }

            using (var stream = new StreamWriter(targetPath))
            {
                stream.WriteLine(JsonConvert.SerializeObject(jsonConfigs, Formatting.Indented));
            }
        }
 private void TestData(JsonConfigurationItem item, string name, string resource, string method, int minDelay, int randomDelay,
                       Dictionary <string, string> json, Dictionary <string, string> headers, HttpStatusCode statusCode)
 {
     item.Name.Should().Be(name);
     item.Resource.Should().Be(resource);
     item.Method.Should().Be(method);
     item.MinDelay.Should().Be(minDelay);
     item.RandomDelay.Should().Be(randomDelay);
     item.Response.Should().NotBeNull();
     item.Response.Json.ShouldBeEquivalentTo(json);
     item.Response.Headers.ShouldBeEquivalentTo(headers);
     item.Response.StatusCode.ShouldBeEquivalentTo(statusCode);
 }