mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-10-31 12:45:28 +08:00 
			
		
		
		
	 f082f7318a
			
		
	
	f082f7318a
	
	
	
		
			
			* brought in rafty * moved raft classes into Ocelot and deleted from int project * started to set up rafty in Ocelot * RAFTY INSIDE OCELOT...WOOT * more work adding rafty...just need to get auth working now * rudimentary authenticated raft requests working * asyn await stuff * hacked rafty into the fileconfigurationcontroller...everything seems to be working roughly but I have a lot of refactoring to do * updated to latest rafty that doesnt need an id * hacky but all tests passing * changed admin area set up to use builder not configuration.json, changed admin area auth to use client credentials * missing code coverage * ignore raft sectionf for code coverage * ignore raft sectionf for code coverage * back to normal filters * try exclude attr * missed these * moved client secret to builder for authentication and updated docs * lock to try and fix error accessing identity server created temprsa file on build server * updated postman scripts and changed Ocelot to not always use type handling as this looked crap when manually accessing the configuration endpoint * added rafty docs * changes I missed * added serialisation code we need for rafty to process commands when they proxy to leader * moved controllers into their feature slices
		
			
				
	
	
		
			199 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			199 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using Moq;
 | |
| using Ocelot.Configuration.File;
 | |
| using Shouldly;
 | |
| using TestStack.BDDfy;
 | |
| using Xunit;
 | |
| using Newtonsoft.Json;
 | |
| using System.IO;
 | |
| using Microsoft.AspNetCore.Hosting;
 | |
| using Ocelot.Configuration.Repository;
 | |
| 
 | |
| namespace Ocelot.UnitTests.Configuration
 | |
| {
 | |
|     public class FileConfigurationRepositoryTests
 | |
|     {
 | |
|         private readonly Mock<IHostingEnvironment> _hostingEnvironment = new Mock<IHostingEnvironment>();
 | |
|         private IFileConfigurationRepository _repo;
 | |
|         private FileConfiguration _result;
 | |
|         private FileConfiguration _fileConfiguration;
 | |
|         private string _environmentName = "DEV";
 | |
| 
 | |
|         public FileConfigurationRepositoryTests()
 | |
|         {
 | |
|             _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
 | |
|             _repo = new FileConfigurationRepository(_hostingEnvironment.Object);
 | |
|         }
 | |
| 
 | |
|         [Fact]
 | |
|         public void should_return_file_configuration()
 | |
|         {
 | |
|             var config = FakeFileConfigurationForGet();
 | |
| 
 | |
|             this.Given(x => x.GivenTheConfigurationIs(config))
 | |
|                 .When(x => x.WhenIGetTheReRoutes())
 | |
|                 .Then(x => x.ThenTheFollowingIsReturned(config))
 | |
|                 .BDDfy();
 | |
|         }
 | |
| 
 | |
|         [Fact]
 | |
|         public void should_return_file_configuration_if_environment_name_is_unavailable()
 | |
|         {
 | |
|             var config = FakeFileConfigurationForGet();
 | |
| 
 | |
|             this.Given(x => x.GivenTheEnvironmentNameIsUnavailable())
 | |
|                 .And(x => x.GivenTheConfigurationIs(config))
 | |
|                 .When(x => x.WhenIGetTheReRoutes())
 | |
|                 .Then(x => x.ThenTheFollowingIsReturned(config))
 | |
|                 .BDDfy();
 | |
|         }
 | |
| 
 | |
|         [Fact]
 | |
|         public void should_set_file_configuration()
 | |
|         {
 | |
|             var config = FakeFileConfigurationForSet();
 | |
| 
 | |
|             this.Given(x => GivenIHaveAConfiguration(config))
 | |
|                 .When(x => WhenISetTheConfiguration())
 | |
|                 .Then(x => ThenTheConfigurationIsStoredAs(config))
 | |
|                 .BDDfy();
 | |
|         }
 | |
| 
 | |
|         [Fact]
 | |
|         public void should_set_file_configuration_if_environment_name_is_unavailable()
 | |
|         {
 | |
|             var config = FakeFileConfigurationForSet();
 | |
|             this.Given(x => GivenIHaveAConfiguration(config))
 | |
|                 .And(x => GivenTheEnvironmentNameIsUnavailable())
 | |
|                 .When(x => WhenISetTheConfiguration())
 | |
|                 .Then(x => ThenTheConfigurationIsStoredAs(config))
 | |
|                 .BDDfy();
 | |
|         }
 | |
| 
 | |
|         private void GivenTheEnvironmentNameIsUnavailable()
 | |
|         {
 | |
|             _environmentName = null;
 | |
|             _hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
 | |
|             _repo = new FileConfigurationRepository(_hostingEnvironment.Object);
 | |
|         }
 | |
| 
 | |
|         private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
 | |
|         {
 | |
|             _fileConfiguration = fileConfiguration;
 | |
|         }
 | |
| 
 | |
|         private void WhenISetTheConfiguration()
 | |
|         {
 | |
|             _repo.Set(_fileConfiguration);
 | |
|             _result = _repo.Get().Result.Data;
 | |
|         }
 | |
| 
 | |
|         private void ThenTheConfigurationIsStoredAs(FileConfiguration expected)
 | |
|         {
 | |
|             _result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
 | |
|             _result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
 | |
|             _result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
 | |
| 
 | |
|             for(var i = 0; i < _result.ReRoutes.Count; i++)
 | |
|             {
 | |
|                 _result.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
 | |
|                 _result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
 | |
|                 _result.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
 | |
|                 _result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         private void  GivenTheConfigurationIs(FileConfiguration fileConfiguration)
 | |
|         {
 | |
|             var configurationPath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json";
 | |
| 
 | |
|             var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
 | |
| 
 | |
|             if (File.Exists(configurationPath))
 | |
|             {
 | |
|                 File.Delete(configurationPath);
 | |
|             }
 | |
| 
 | |
|             File.WriteAllText(configurationPath, jsonConfiguration);
 | |
|         }
 | |
| 
 | |
|         private void WhenIGetTheReRoutes()
 | |
|         {
 | |
|             _result = _repo.Get().Result.Data;
 | |
|         }
 | |
| 
 | |
|         private void ThenTheFollowingIsReturned(FileConfiguration expected)
 | |
|         {
 | |
|             _result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
 | |
|             _result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
 | |
|             _result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
 | |
| 
 | |
|             for(var i = 0; i < _result.ReRoutes.Count; i++)
 | |
|             {
 | |
|                 _result.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
 | |
|                 _result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
 | |
|                 _result.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
 | |
|                 _result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         private FileConfiguration FakeFileConfigurationForSet()
 | |
|         {
 | |
|             var reRoutes = new List<FileReRoute>
 | |
|             {
 | |
|                 new FileReRoute
 | |
|                 {
 | |
|                     DownstreamHost = "123.12.12.12",
 | |
|                     DownstreamPort = 80,
 | |
|                     DownstreamScheme = "https",
 | |
|                     DownstreamPathTemplate = "/asdfs/test/{test}"
 | |
|                 }
 | |
|             };
 | |
| 
 | |
|             var globalConfiguration = new FileGlobalConfiguration
 | |
|             {
 | |
|                 ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
 | |
|                 {
 | |
|                     Port = 198,
 | |
|                     Host = "blah"
 | |
|                 }
 | |
|             };
 | |
| 
 | |
|             return new FileConfiguration
 | |
|             {
 | |
|                 GlobalConfiguration = globalConfiguration,
 | |
|                 ReRoutes = reRoutes
 | |
|             };
 | |
|         }
 | |
| 
 | |
|         private FileConfiguration FakeFileConfigurationForGet()
 | |
|         {
 | |
|             var reRoutes = new List<FileReRoute>
 | |
|             {
 | |
|                 new FileReRoute
 | |
|                 {
 | |
|                     DownstreamHost = "localhost",
 | |
|                     DownstreamPort = 80,
 | |
|                     DownstreamScheme = "https",
 | |
|                     DownstreamPathTemplate = "/test/test/{test}"
 | |
|                 }
 | |
|             };
 | |
| 
 | |
|             var globalConfiguration = new FileGlobalConfiguration
 | |
|             {
 | |
|                 ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
 | |
|                 {
 | |
|                     Port = 198,
 | |
|                     Host = "blah"
 | |
|                 }
 | |
|             };
 | |
| 
 | |
|             return new FileConfiguration
 | |
|             {
 | |
|                 GlobalConfiguration = globalConfiguration,
 | |
|                 ReRoutes = reRoutes
 | |
|             };
 | |
|         }
 | |
|     }
 | |
| } |