private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
        {
            _fakeConsulBuilder = new WebHostBuilder()
                                 .UseUrls(url)
                                 .UseKestrel()
                                 .UseContentRoot(Directory.GetCurrentDirectory())
                                 .UseIISIntegration()
                                 .UseUrls(url)
                                 .Configure(app =>
            {
                app.Run(async context =>
                {
                    if (context.Request.Method.ToLower() == "get" && context.Request.Path.Value == "/v1/kv/InternalConfiguration")
                    {
                        var json = JsonConvert.SerializeObject(_config);

                        var bytes = Encoding.UTF8.GetBytes(json);

                        var base64 = Convert.ToBase64String(bytes);

                        var kvp = new FakeConsulGetResponse(base64);
                        json    = JsonConvert.SerializeObject(new FakeConsulGetResponse[] { kvp });
                        context.Response.Headers.Add("Content-Type", "application/json");
                        await context.Response.WriteAsync(json);
                    }
                    else if (context.Request.Method.ToLower() == "put" && context.Request.Path.Value == "/v1/kv/InternalConfiguration")
                    {
                        try
                        {
                            var reader = new StreamReader(context.Request.Body);

                            // Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
                            // var json = reader.ReadToEnd();
                            var json = await reader.ReadToEndAsync();

                            _config = JsonConvert.DeserializeObject <FileConfiguration>(json);

                            var response = JsonConvert.SerializeObject(true);

                            await context.Response.WriteAsync(response);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            throw;
                        }
                    }
                    else if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
                    {
                        var json = JsonConvert.SerializeObject(_consulServices);
                        context.Response.Headers.Add("Content-Type", "application/json");
                        await context.Response.WriteAsync(json);
                    }
                });
            })
                                 .Build();

            _fakeConsulBuilder.Start();
        }
示例#2
0
        private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
        {
            _fakeConsulBuilder = new WebHostBuilder()
                                 .UseUrls(url)
                                 .UseKestrel()
                                 .UseContentRoot(Directory.GetCurrentDirectory())
                                 .UseIISIntegration()
                                 .UseUrls(url)
                                 .Configure(app =>
            {
                app.Run(async context =>
                {
                    if (context.Request.Method.ToLower() == "get" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration")
                    {
                        var json = JsonConvert.SerializeObject(_config);

                        var bytes = Encoding.UTF8.GetBytes(json);

                        var base64 = Convert.ToBase64String(bytes);

                        var kvp = new FakeConsulGetResponse(base64);

                        await context.Response.WriteJsonAsync(new FakeConsulGetResponse[] { kvp });
                    }

                    else if (context.Request.Method.ToLower() == "put" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration")
                    {
                        try
                        {
                            var reader = new StreamReader(context.Request.Body);

                            var json = reader.ReadToEnd();

                            var settings = new JsonSerializerSettings();
                            settings.Converters.Add(new AuthenticationConfigConverter());
                            _config = JsonConvert.DeserializeObject <OcelotConfiguration>(json, settings);

                            var response = JsonConvert.SerializeObject(true);

                            await context.Response.WriteAsync(response);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                            throw;
                        }
                    }
                });
            })
                                 .Build();

            _fakeConsulBuilder.Start();
        }