mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 19:10:51 +08:00 
			
		
		
		
	* #280 can now add response headers inc trace id, now need to consolidate the header place holder stuff * #280 changed port for linux tests * #280 lots of hacking around to handle errors and consolidate placeholders into one class
This commit is contained in:
		
							
								
								
									
										79
									
								
								test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using Moq;
 | 
			
		||||
using Ocelot.Infrastructure;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
using Shouldly;
 | 
			
		||||
using Xunit;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.UnitTests.Infrastructure
 | 
			
		||||
{
 | 
			
		||||
    public class PlaceholdersTests
 | 
			
		||||
    {
 | 
			
		||||
        private IPlaceholders _placeholders;
 | 
			
		||||
        private Mock<IBaseUrlFinder> _finder;
 | 
			
		||||
        private Mock<IRequestScopedDataRepository> _repo;
 | 
			
		||||
        
 | 
			
		||||
        public PlaceholdersTests()
 | 
			
		||||
        {
 | 
			
		||||
            _repo = new Mock<IRequestScopedDataRepository>();
 | 
			
		||||
            _finder = new Mock<IBaseUrlFinder>();
 | 
			
		||||
            _placeholders = new Placeholders(_finder.Object, _repo.Object);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_base_url()
 | 
			
		||||
        {
 | 
			
		||||
            var baseUrl = "http://www.bbc.co.uk";
 | 
			
		||||
            _finder.Setup(x => x.Find()).Returns(baseUrl);
 | 
			
		||||
            var result = _placeholders.Get("{BaseUrl}");
 | 
			
		||||
            result.Data.ShouldBe(baseUrl);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_key_does_not_exist()
 | 
			
		||||
        {
 | 
			
		||||
            var result = _placeholders.Get("{Test}");
 | 
			
		||||
            result.IsError.ShouldBeTrue();
 | 
			
		||||
            result.Errors[0].Message.ShouldBe("Unable to find placeholder called {Test}");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_downstream_base_url_when_port_is_not_80_or_443()
 | 
			
		||||
        {
 | 
			
		||||
            var request = new HttpRequestMessage();
 | 
			
		||||
            request.RequestUri = new Uri("http://www.bbc.co.uk");
 | 
			
		||||
            var result = _placeholders.Get("{DownstreamBaseUrl}", request);
 | 
			
		||||
            result.Data.ShouldBe("http://www.bbc.co.uk/");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_downstream_base_url_when_port_is_80_or_443()
 | 
			
		||||
        {
 | 
			
		||||
              var request = new HttpRequestMessage();
 | 
			
		||||
            request.RequestUri = new Uri("http://www.bbc.co.uk:123");
 | 
			
		||||
            var result = _placeholders.Get("{DownstreamBaseUrl}", request);
 | 
			
		||||
            result.Data.ShouldBe("http://www.bbc.co.uk:123/");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_key_does_not_exist_for_http_request_message()
 | 
			
		||||
        {
 | 
			
		||||
            var result = _placeholders.Get("{Test}", new System.Net.Http.HttpRequestMessage());
 | 
			
		||||
            result.IsError.ShouldBeTrue();
 | 
			
		||||
            result.Errors[0].Message.ShouldBe("Unable to find placeholder called {Test}");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [Fact]
 | 
			
		||||
        public void should_return_trace_id()
 | 
			
		||||
        {
 | 
			
		||||
            var traceId = "123";
 | 
			
		||||
            _repo.Setup(x => x.Get<string>("TraceId")).Returns(new OkResponse<string>(traceId));
 | 
			
		||||
            var result = _placeholders.Get("{TraceId}");
 | 
			
		||||
            result.Data.ShouldBe(traceId);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user