66using Microsoft . Extensions . DependencyInjection ;
77using Microsoft . Extensions . Hosting ;
88using Microsoft . Extensions . Logging ;
9- using Newtonsoft . Json ;
10- using Ocelot . Configuration . File ;
11- using Ocelot . DependencyInjection ;
12- using Ocelot . Middleware ;
9+ using Microsoft . Extensions . Options ;
10+
11+ //using Newtonsoft.Json;
12+ //using Ocelot.Configuration.File;
13+ //using Ocelot.DependencyInjection;
14+ //using Ocelot.Middleware;
1315using Shouldly ;
16+ using System . Collections ;
1417using System . Diagnostics ;
1518using System . Net ;
1619using System . Net . Http . Headers ;
20+ using System . Reflection ;
1721using System . Runtime . CompilerServices ;
22+ using System . Text . Encodings . Web ;
23+ using System . Text . Json ;
24+ using System . Text . Unicode ;
25+ using System . Xml ;
1826using CookieHeaderValue = Microsoft . Net . Http . Headers . CookieHeaderValue ;
1927using MediaTypeHeaderValue = System . Net . Http . Headers . MediaTypeHeaderValue ;
2028
@@ -36,7 +44,7 @@ public AcceptanceSteps()
3644 {
3745 _testId = Guid . NewGuid ( ) ;
3846 random = new Random ( ) ;
39- ocelotConfigFileName = $ "{ _testId : N} -{ ConfigurationBuilderExtensions . PrimaryConfigFile } ";
47+ ocelotConfigFileName = $ "{ _testId : N} -ocelot.json" ; // {ConfigurationBuilderExtensions.PrimaryConfigFile}";
4048 Files = [ ocelotConfigFileName ] ;
4149 Folders = [ ] ;
4250 handler = new ( ) ;
@@ -46,56 +54,100 @@ public AcceptanceSteps()
4654 protected List < string > Folders { get ; }
4755 protected string TestID { get => _testId . ToString ( "N" ) ; }
4856
49- protected static FileHostAndPort Localhost ( int port ) => new ( "localhost" , port ) ;
57+ protected virtual /*FileHostAndPort*/ object ? Localhost ( int port )
58+ {
59+ // return new("localhost", port);
60+ return Ocelot . CreateFileHostAndPort ( "localhost" , port , out Type type ) ;
61+ }
62+
5063 protected static string DownstreamUrl ( int port ) => DownstreamUrl ( port , Uri . UriSchemeHttp ) ;
5164 protected static string DownstreamUrl ( int port , string scheme ) => $ "{ scheme ?? Uri . UriSchemeHttp } ://localhost:{ port } ";
5265 protected static string LoopbackLocalhostUrl ( int port , int loopbackIndex = 0 ) => $ "{ Uri . UriSchemeHttp } ://127.0.0.{ ++ loopbackIndex } :{ port } ";
5366
54- protected virtual FileConfiguration GivenConfiguration ( params FileRoute [ ] routes ) => new ( )
67+ protected virtual /* FileConfiguration*/ object ? GivenConfiguration ( params /* FileRoute*/ object [ ] routes )
5568 {
56- Routes = [ .. routes ] ,
57- } ;
69+ object ? c = Ocelot . CreateFileConfiguration ( out Type type ) ;
70+ PropertyInfo ? property = type . GetProperty ( "Routes" ) ;
71+ if ( property ? . GetValue ( c ) is IList list )
72+ foreach ( var route in routes )
73+ list . Add ( route ) ;
74+ return c ;
75+ }
5876
59- protected static FileRoute GivenDefaultRoute ( int port ) => GivenRoute ( port ) ;
60- protected static FileRoute GivenCatchAllRoute ( int port ) => GivenRoute ( port , "/{everything}" , "/{everything}" ) ;
61- protected static FileRoute GivenRoute ( int port , string ? upstream = null , string ? downstream = null )
77+ protected virtual /* FileRoute*/ object ? GivenDefaultRoute ( int port ) => GivenRoute ( port ) ;
78+ protected virtual /* FileRoute*/ object ? GivenCatchAllRoute ( int port ) => GivenRoute ( port , "/{everything}" , "/{everything}" ) ;
79+ protected virtual /* FileRoute*/ object ? GivenRoute ( int port , string ? upstream = null , string ? downstream = null )
6280 {
63- var r = Activator . CreateInstance < FileRoute > ( ) ;
64- r . DownstreamHostAndPorts . Add ( Localhost ( port ) ) ;
65- r . DownstreamPathTemplate = downstream ?? "/" ;
66- r . DownstreamScheme = Uri . UriSchemeHttp ;
67- r . UpstreamPathTemplate = upstream ?? "/" ;
68- IList < string > uhmList = r . UpstreamHttpMethod ;
69- uhmList . Add ( HttpMethods . Get ) ;
81+ object ? r = Ocelot . CreateFileRoute ( out Type type ) ;
82+
83+ //r.DownstreamHostAndPorts.Add(Localhost(port));
84+ PropertyInfo ? property = type . GetProperty ( "DownstreamHostAndPorts" ) ;
85+ if ( property ? . GetValue ( r ) is IList downstreamHostAndPorts )
86+ downstreamHostAndPorts . Add ( Localhost ( port ) ) ;
87+
88+ //r.DownstreamPathTemplate = downstream ?? "/";
89+ property = type . GetProperty ( "DownstreamPathTemplate" ) ;
90+ property ? . SetValue ( r , downstream ?? "/" ) ;
91+
92+ //r.DownstreamScheme = Uri.UriSchemeHttp;
93+ property = type . GetProperty ( "DownstreamScheme" ) ;
94+ property ? . SetValue ( r , Uri . UriSchemeHttp ) ;
95+
96+ //r.UpstreamPathTemplate = upstream ?? "/";
97+ property = type . GetProperty ( "UpstreamPathTemplate" ) ;
98+ property ? . SetValue ( r , upstream ?? "/" ) ;
99+
100+ //r.UpstreamHttpMethod.Add(HttpMethods.Get);
101+ property = type . GetProperty ( "UpstreamHttpMethod" ) ;
102+ if ( property ? . GetValue ( r ) is IList upstreamHttpMethod )
103+ upstreamHttpMethod . Add ( HttpMethods . Get ) ;
104+
70105 return r ;
71106 }
72107
73- public void GivenThereIsAConfiguration ( FileConfiguration fileConfiguration )
108+ public virtual void GivenThereIsAConfiguration ( /* FileConfiguration*/ object fileConfiguration )
74109 => GivenThereIsAConfiguration ( fileConfiguration , ocelotConfigFileName ) ;
75- public void GivenThereIsAConfiguration ( FileConfiguration from , string toFile )
110+ public virtual void GivenThereIsAConfiguration ( /* FileConfiguration*/ object from , string toFile )
76111 {
77112 var json = SerializeJson ( from , ref toFile ) ;
78113 File . WriteAllText ( toFile , json ) ;
79114 }
80- public Task GivenThereIsAConfigurationAsync ( FileConfiguration from , string toFile )
115+ public virtual Task GivenThereIsAConfigurationAsync ( /* FileConfiguration*/ object from , string toFile )
81116 {
82117 var json = SerializeJson ( from , ref toFile ) ;
83118 return File . WriteAllTextAsync ( toFile , json ) ;
84119 }
85- protected string SerializeJson ( FileConfiguration from , ref string toFile )
120+ protected virtual string SerializeJson ( /* FileConfiguration*/ object from , ref string toFile )
86121 {
87122 toFile ??= ocelotConfigFileName ;
88123 Files . Add ( toFile ) ; // register for disposing
89- return JsonConvert . SerializeObject ( from , Formatting . Indented ) ;
124+
125+ // return JsonConvert.SerializeObject(from, Formatting.Indented);
126+ string json = JsonSerializer . Serialize ( from , JsonWebIndented ) ;
127+ return json ;
90128 }
91129
130+ public readonly static JsonSerializerOptions JsonWebIndented = new ( )
131+ {
132+ Encoder = JavaScriptEncoder . Create ( UnicodeRanges . All ) , // Avoid escaping non-ASCII
133+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase , // Use camelCase for web
134+ WriteIndented = true , // Not compact output for better readability
135+ PropertyNameCaseInsensitive = true // Optional: for deserialization
136+ } ;
137+
92138 #region GivenOcelotIsRunning
93- public void WithBasicConfiguration ( WebHostBuilderContext hosting , IConfigurationBuilder config ) => config
94- . SetBasePath ( hosting . HostingEnvironment . ContentRootPath )
95- . AddOcelot ( ocelotConfigFileName , false , false ) ;
96- public static void WithAddOcelot ( IServiceCollection services ) => services . AddOcelot ( ) ;
97- public static void WithUseOcelot ( IApplicationBuilder app ) => app . UseOcelot ( ) . Wait ( ) ;
98- public static Task < IApplicationBuilder > WithUseOcelotAsync ( IApplicationBuilder app ) => app . UseOcelot ( ) ;
139+ public void WithBasicConfiguration ( WebHostBuilderContext hosting , IConfigurationBuilder config )
140+ {
141+ config . SetBasePath ( hosting . HostingEnvironment . ContentRootPath ) ;
142+ //config.AddOcelot(ocelotConfigFileName, false, false);
143+ config . AddJsonFile ( ocelotConfigFileName , false , false ) ;
144+ }
145+
146+ //public static void WithAddOcelot(IServiceCollection services) => services.AddOcelot();
147+ public static void WithAddOcelot ( IServiceCollection services ) => Ocelot . AddOcelot ( services ) ; // services.AddOcelot();
148+
149+ public static void WithUseOcelot ( IApplicationBuilder app ) => Ocelot . UseOcelot ( app ) . Wait ( ) ; // app.UseOcelot().Wait();
150+ public static Task < IApplicationBuilder > WithUseOcelotAsync ( IApplicationBuilder app ) => Ocelot . UseOcelot ( app ) ; // app.UseOcelot();
99151
100152 public int GivenOcelotIsRunning ( )
101153 => GivenOcelotIsRunning ( null , null , null , null , null , null ) ;
0 commit comments