Skip to content

Commit

Permalink
Fix build after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Nov 1, 2024
1 parent cfd0759 commit db1fe0a
Showing 1 changed file with 62 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using System.Net;
using System.Text;

namespace Ocelot.AcceptanceTests.ServiceDiscovery
Expand Down Expand Up @@ -102,14 +101,13 @@ public void Should_load_configuration_out_of_consul()
},
};

var fakeConsulServiceDiscoveryUrl = DownstreamUrl(consulPort);
var consulConfig = new FileConfiguration
{
Routes = new List<FileRoute>
var fakeConsulServiceDiscoveryUrl = DownstreamUrl(consulPort);
var consulConfig = new FileConfiguration
{
new()
Routes = new List<FileRoute>
{
DownstreamPathTemplate = "/status",
new()
{
DownstreamPathTemplate = "/status",
DownstreamScheme = "http",
DownstreamHostAndPorts = new List<FileHostAndPort>
Expand Down Expand Up @@ -165,7 +163,7 @@ public void Should_load_configuration_out_of_consul_if_it_is_changed()
},
};

var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";

var consulConfig = new FileConfiguration
{
Expand Down Expand Up @@ -229,17 +227,17 @@ public void Should_load_configuration_out_of_consul_if_it_is_changed()
},
};

this.Given(x => GivenTheConsulConfigurationIs(consulConfig))
.And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, string.Empty))
.And(x => x.GivenThereIsAServiceRunningOn(DownstreamUrl(servicePort), "/status", 200, "Hello from Laura"))
.And(x => GivenThereIsAConfiguration(configuration))
.And(x => GivenOcelotIsRunningUsingConsulToStoreConfig())
.And(x => WhenIGetUrlOnTheApiGateway("/cs/status"))
.And(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => ThenTheResponseBodyShouldBe("Hello from Laura"))
.When(x => GivenTheConsulConfigurationIs(secondConsulConfig))
.Then(x => ThenTheConfigIsUpdatedInOcelot())
.BDDfy();
this.Given(x => GivenTheConsulConfigurationIs(consulConfig))
.And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, string.Empty))
.And(x => x.GivenThereIsAServiceRunningOn(DownstreamUrl(servicePort), "/status", 200, "Hello from Laura"))
.And(x => GivenThereIsAConfiguration(configuration))
.And(x => GivenOcelotIsRunningUsingConsulToStoreConfig())
.And(x => WhenIGetUrlOnTheApiGateway("/cs/status"))
.And(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => ThenTheResponseBodyShouldBe("Hello from Laura"))
.When(x => GivenTheConsulConfigurationIs(secondConsulConfig))
.Then(x => ThenTheConfigIsUpdatedInOcelot())
.BDDfy();
}

[Fact]
Expand Down Expand Up @@ -269,7 +267,7 @@ public void Should_handle_request_to_consul_for_downstream_service_and_make_requ
new()
{
ServiceName = serviceName,
RateLimitRule = new FileRateLimitRule
RateLimitRule = new FileRateLimitRule
{
EnableRateLimiting = true,
ClientWhitelist = new List<string>(),
Expand Down Expand Up @@ -319,7 +317,7 @@ public void Should_handle_request_to_consul_for_downstream_service_and_make_requ
.And(x => GivenThereIsAConfiguration(configuration))
.And(x => x.GivenOcelotIsRunningUsingConsulToStoreConfig())
.When(x => WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/web/something", 1))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.When(x => WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/web/something", 2))
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.When(x => WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/web/something", 1))
Expand Down Expand Up @@ -379,62 +377,62 @@ private void GivenOcelotIsRunningUsingConsulToStoreConfig()
Thread.Sleep(1000);
}

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

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

var base64 = Convert.ToBase64String(bytes);
var base64 = Convert.ToBase64String(bytes);

var kvp = new FakeConsulGetResponse(base64);
json = JsonConvert.SerializeObject(new[] { kvp });
context.Response.Headers.Append("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 kvp = new FakeConsulGetResponse(base64);
json = JsonConvert.SerializeObject(new[] { kvp });
context.Response.Headers.Append("Content-Type", "application/json");
await context.Response.WriteAsync(json);
}
else if (context.Request.Method.Equals(HttpMethods.Put, StringComparison.OrdinalIgnoreCase) && context.Request.Path.Value == "/v1/kv/InternalConfiguration")
{
var reader = new StreamReader(context.Request.Body);
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();
// 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);
_config = JsonConvert.DeserializeObject<FileConfiguration>(json);

var response = JsonConvert.SerializeObject(true);
var response = JsonConvert.SerializeObject(true);

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

Expand Down Expand Up @@ -465,7 +463,6 @@ private Task GivenThereIsAServiceRunningOn(string url, string basePath, int stat
.Configure(app =>
{
app.UsePathBase(basePath);

app.Run(async context =>
{
context.Response.StatusCode = statusCode;
Expand All @@ -476,13 +473,6 @@ private Task GivenThereIsAServiceRunningOn(string url, string basePath, int stat
return _builder.StartAsync();
}

public override void Dispose()
{
_builder?.Dispose();
_fakeConsulBuilder?.Dispose();
base.Dispose();
}

private class FakeCache : IOcelotCache<FileConfiguration>
{
public void Add(string key, FileConfiguration value, TimeSpan ttl, string region) => throw new NotImplementedException();
Expand Down

0 comments on commit db1fe0a

Please sign in to comment.