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 content = JsonConvert.SerializeObject(CompileTemplate(verb.Content.ToString(), req, resp, route));

                        await resp.WriteAsync(content);;
                    };

                    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);
    }
Exemple #2
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor |
                                   ForwardedHeaders.XForwardedProto
            });

            var configFile = Configuration.GetValue <string>("config", "mockServer.json");

            MockServerConfig mockConfig = MockServerConfig.ReadConfig(configFile);

            app.UseMockServer(mockConfig);
        }
Exemple #3
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));


            var defaultConfigFileFullPath = Path.Join(Directory.GetCurrentDirectory(), CONFIG_FILE_NAME);


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

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

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

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

                //var configTemplate = new FileInfo(DEFAULT_CONFIG_FILE).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();
        }