Example #1
0
        public static IRouteBuilder UseMockServer(this IApplicationBuilder app, MockServerConfig config)
        {
            App = app;
            RouteBuilder routeBuilder = new RouteBuilder(App);

            config.Templates = config.Templates ?? new Dictionary <string, object>();

            foreach (KeyValuePair <string, object> item in config.Templates)
            {
                var name     = item.Key;
                var template = item.Value.ToString();

                Generators[name] = () =>
                {
                    IDictionary <string, object> compiled = CompileTemplate(template);
                    return(compiled);
                };

                //IDictionary<string, object> sample = Generators[name]();

                //var json = JsonConvert.SerializeObject(sample);
            }

            foreach (KeyValuePair <string, MockServerUrlConfig> kv in config.Resources)
            {
                var url       = kv.Key;
                var urlConfig = kv.Value;

                foreach (var ukv in urlConfig)
                {
                    var method = ukv.Key;
                    MockServerHttpVerb verb = ukv.Value;
                    if (verb.Dynamic)
                    {
                        Func <HttpRequest, HttpResponse, RouteData, Task> handler = async(req, resp, route) =>
                        {
                            string body = JsonConvert.SerializeObject(CompileTemplate(verb.Content.ToString(), req, resp, route));
                            resp.ContentLength = Encoding.UTF8.GetBytes(body).Length;
                            resp.ContentType   = verb.ContentType;

                            foreach (var keyValuePair in verb.Headers)
                            {
                                resp.Headers.Add(keyValuePair.Key, keyValuePair.Value);
                            }

                            await resp.WriteAsync(body);;
                        };

                        routeBuilder.On(method, url, verb.ContentType, handler);
                    }
                    else
                    {
                        routeBuilder.On(method, url, verb);
                    }
                }
            }

            routeBuilder.OnGet("/__config", "application/json", (route) =>
            {
                return(config);
            });

            routeBuilder.BuildAndUseRouter();

            return(routeBuilder);
        }
Example #2
0
        public static void Main(string[] args)
        {
            #region CLI Argument parser
            // Create some options and a parser
            var generateConfigOption = new Option(
                "--generate-config",
                "To generate an example `mockServer.json` config, pass this parameter",
                new Argument <bool>(defaultValue: false));


            string DEFAULT_CONFIG_PATH = Path.Join(Directory.GetCurrentDirectory(), "mockServer.json");

            var configOption = new Option(
                "--config",
                "Config file path",
                new Argument <string>(DEFAULT_CONFIG_PATH));

            var urlsOption = new Option("--urls", "" +
                                        "Set ASP.Net Core URL: Example: --urls http://+:3005",
                                        new Argument <string>("http://localhost:3005"));


            // Add them to the root command
            var rootCommand = new RootCommand();
            rootCommand.Description = "Mock Server";

            rootCommand.AddOption(generateConfigOption);
            rootCommand.AddOption(configOption);
            rootCommand.AddOption(urlsOption);

            rootCommand.Handler = CommandHandler.Create <bool, string>((generateConfig, config) =>
            {
                if (config == "mockServer.json")
                {
                    throw new ArgumentException(nameof(config));
                }

                var configTemplate = new FileInfo(DEFAULT_CONFIG_PATH).FullName;

                Console.WriteLine($"--generate-config => {generateConfig}");

                if (!File.Exists(config))
                {
                    Console.WriteLine($"Generating default config => {new FileInfo(config).FullName}");
                    generateConfig = true;
                }

                if (generateConfig)
                {
                    MockServerConfig.GenerateDefaultConfig(config);
                }


                #endregion

                #region web
                IWebHostBuilder webHostBuilder = CreateWebHostBuilder(args);
                IWebHost webHost = webHostBuilder.Build();

                webHost.Run();
                #endregion
            });

            // Parse the incoming args and invoke the handler
            rootCommand.InvokeAsync(args).Wait();
        }