Example #1
0
        public static IRouteBuilder OnGet(this IRouteBuilder builder,
                                          string template,
                                          string contentType,
                                          string content)
        {
            builder.MapGet(template, (req, resp, route) =>
            {
                var verb = new MockServerHttpVerb()
                {
                    ContentType = contentType,
                    Content     = content
                };

                return(Content(resp, verb));
            });

            return(builder);
        }
Example #2
0
        public static IRouteBuilder OnGet(this IRouteBuilder builder,
                                          string template,
                                          string contentType,
                                          Func <HttpRequest, HttpResponse, RouteData, string> gen)
        {
            builder.MapGet(template, (req, resp, route) =>
            {
                string content = gen(req, resp, route);
                var verb       = new MockServerHttpVerb()
                {
                    ContentType = contentType,
                    Content     = content
                };

                return(Content(resp, verb));
            });

            return(builder);
        }
Example #3
0
        public static IRouteBuilder OnGet <T>(this IRouteBuilder builder,
                                              string template,
                                              string contentType,
                                              Func <RouteData, T> gen)
        {
            builder.MapGet(template, (req, resp, route) =>
            {
                var body       = gen(route);
                string content = JsonConvert.SerializeObject(body);

                var verb = new MockServerHttpVerb()
                {
                    ContentType = contentType,
                    Content     = content
                };

                return(Content(resp, verb));
            });

            return(builder);
        }
Example #4
0
        public static IRouteBuilder On(this IRouteBuilder builder,
                                       string method,
                                       string template,
                                       string contentType,
                                       object content, Dictionary <string, string> headers = null, int statusCode = 200)
        {
            var verb = new MockServerHttpVerb()
            {
                Content     = content,
                ContentType = contentType,
                Headers     = headers ?? new Dictionary <string, string>(),
                StatusCode  = statusCode
            };

            builder.MapVerb(method, template, (req, resp, route) =>
            {
                return(Content(resp, verb));
            });

            return(builder);
        }
Example #5
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);
        }