mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 05:48:16 +08:00
brought in flurl and stated adding tests for the requester
This commit is contained in:
@ -5,11 +5,11 @@ using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ConfigurationValidationTests
|
||||
{
|
||||
private Configuration _configuration;
|
||||
private Library.Infrastructure.Configuration.Configuration _configuration;
|
||||
private readonly IConfigurationValidator _configurationValidator;
|
||||
private Response<ConfigurationValidationResult> _result;
|
||||
|
||||
@ -21,7 +21,7 @@ namespace Ocelot.UnitTests
|
||||
[Fact]
|
||||
public void configuration_is_valid_with_one_reroute()
|
||||
{
|
||||
this.Given(x => x.GivenAConfiguration(new Configuration()
|
||||
this.Given(x => x.GivenAConfiguration(new Library.Infrastructure.Configuration.Configuration()
|
||||
{
|
||||
ReRoutes = new List<ReRoute>
|
||||
{
|
||||
@ -40,7 +40,7 @@ namespace Ocelot.UnitTests
|
||||
[Fact]
|
||||
public void configuration_is_not_valid_with_duplicate_reroutes()
|
||||
{
|
||||
this.Given(x => x.GivenAConfiguration(new Configuration()
|
||||
this.Given(x => x.GivenAConfiguration(new Library.Infrastructure.Configuration.Configuration()
|
||||
{
|
||||
ReRoutes = new List<ReRoute>
|
||||
{
|
||||
@ -62,7 +62,7 @@ namespace Ocelot.UnitTests
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAConfiguration(Configuration configuration)
|
||||
private void GivenAConfiguration(Library.Infrastructure.Configuration.Configuration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Ocelot.Library.Infrastructure.Configuration;
|
||||
@ -12,31 +9,31 @@ using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
{
|
||||
public class DownstreamRouteFinderTests
|
||||
{
|
||||
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
|
||||
private readonly Mock<IOptions<Configuration>> _mockConfig;
|
||||
private readonly Mock<IOptions<Library.Infrastructure.Configuration.Configuration>> _mockConfig;
|
||||
private readonly Mock<IUrlPathToUrlTemplateMatcher> _mockMatcher;
|
||||
private string _upstreamUrlPath;
|
||||
private Response<DownstreamRoute> _result;
|
||||
private Response<DownstreamRoute> _response;
|
||||
private Configuration _configuration;
|
||||
private Library.Infrastructure.Configuration.Configuration _configuration;
|
||||
private UrlMatch _match;
|
||||
|
||||
public DownstreamRouteFinderTests()
|
||||
{
|
||||
_mockConfig = new Mock<IOptions<Configuration>>();
|
||||
_mockConfig = new Mock<IOptions<Library.Infrastructure.Configuration.Configuration>>();
|
||||
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
|
||||
_downstreamRouteFinder = new DownstreamRouteFinder(_mockConfig.Object, _mockMatcher.Object);
|
||||
_downstreamRouteFinder = new Library.Infrastructure.DownstreamRouteFinder.DownstreamRouteFinder(_mockConfig.Object, _mockMatcher.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_route()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(x => x.GivenTheConfigurationIs(new Configuration {
|
||||
.And(x => x.GivenTheConfigurationIs(new Library.Infrastructure.Configuration.Configuration {
|
||||
ReRoutes = new List<ReRoute>
|
||||
{
|
||||
new ReRoute()
|
||||
@ -58,7 +55,7 @@ namespace Ocelot.UnitTests
|
||||
public void should_not_return_route()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("somePath"))
|
||||
.And(x => x.GivenTheConfigurationIs(new Configuration
|
||||
.And(x => x.GivenTheConfigurationIs(new Library.Infrastructure.Configuration.Configuration
|
||||
{
|
||||
ReRoutes = new List<ReRoute>
|
||||
{
|
||||
@ -96,7 +93,7 @@ namespace Ocelot.UnitTests
|
||||
.Returns(_match);
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(Configuration configuration)
|
||||
private void GivenTheConfigurationIs(Library.Infrastructure.Configuration.Configuration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_mockConfig
|
119
test/Ocelot.UnitTests/Requester/RequesterTests.cs
Normal file
119
test/Ocelot.UnitTests/Requester/RequesterTests.cs
Normal file
@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Flurl.Http.Testing;
|
||||
using Ocelot.Library.Infrastructure.Requester;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public class RequesterTests : IDisposable
|
||||
{
|
||||
private readonly IHttpRequester _httpRequester;
|
||||
private readonly HttpTest _httpTest;
|
||||
private string _httpMethod;
|
||||
private string _downstreamUrl;
|
||||
private HttpResponseMessage _result;
|
||||
private HttpContent _content;
|
||||
|
||||
public RequesterTests()
|
||||
{
|
||||
_httpTest = new HttpTest();
|
||||
_httpRequester = new HttpClientHttpRequester();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_downstream_url_correctly()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveHttpMethod("GET"))
|
||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
||||
.And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.OK))
|
||||
.When(x => x.WhenIMakeARequest())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheDownstreamServerIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_obey_http_method()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
||||
.And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.Created))
|
||||
.When(x => x.WhenIMakeARequest())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.Created))
|
||||
.And(x => x.ThenTheDownstreamServerIsCalledCorrectly())
|
||||
.And(x => x.ThenTheCorrectHttpMethodIsUsed(HttpMethod.Post))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_forward_http_content()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveHttpMethod("POST"))
|
||||
.And(x => x.GivenIHaveDownstreamUrl("http://www.bbc.co.uk"))
|
||||
.And(x => x.GivenIHaveTheHttpContent(new StringContent("Hi from Tom")))
|
||||
.And(x => x.GivenTheDownstreamServerReturns(HttpStatusCode.Created))
|
||||
.When(x => x.WhenIMakeARequest())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(HttpStatusCode.Created))
|
||||
.And(x => x.ThenTheDownstreamServerIsCalledCorrectly())
|
||||
.And(x => x.ThenTheCorrectHttpMethodIsUsed(HttpMethod.Post))
|
||||
.And(x => x.ThenTheCorrectContentIsUsed(new StringContent("Hi from Tom")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIHaveTheHttpContent(HttpContent content)
|
||||
{
|
||||
_content = content;
|
||||
}
|
||||
|
||||
private void GivenIHaveHttpMethod(string httpMethod)
|
||||
{
|
||||
_httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
private void GivenIHaveDownstreamUrl(string downstreamUrl)
|
||||
{
|
||||
_downstreamUrl = downstreamUrl;
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamServerReturns(HttpStatusCode statusCode)
|
||||
{
|
||||
_httpTest.RespondWith(_content != null ? _content.ReadAsStringAsync().Result : string.Empty, (int)statusCode);
|
||||
}
|
||||
|
||||
private void WhenIMakeARequest()
|
||||
{
|
||||
_result = _httpRequester.GetResponse(_httpMethod, _downstreamUrl, _content != null ? _content.ReadAsStreamAsync().Result : Stream.Null).Result;
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(HttpStatusCode expected)
|
||||
{
|
||||
_result.StatusCode.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamServerIsCalledCorrectly()
|
||||
{
|
||||
_httpTest.ShouldHaveCalled(_downstreamUrl);
|
||||
}
|
||||
|
||||
private void ThenTheCorrectHttpMethodIsUsed(HttpMethod expected)
|
||||
{
|
||||
_httpTest.CallLog[0].Request.Method.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheCorrectContentIsUsed(HttpContent content)
|
||||
{
|
||||
_httpTest.CallLog[0].Response.Content.ReadAsStringAsync().Result.ShouldBe(content.ReadAsStringAsync().Result);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_httpTest.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
14
test/Ocelot.UnitTests/Responder/ResponderTests.cs
Normal file
14
test/Ocelot.UnitTests/Responder/ResponderTests.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Responder
|
||||
{
|
||||
public class ResponderTests
|
||||
{
|
||||
[Fact]
|
||||
public void should_do_something()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -2,12 +2,11 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests
|
||||
namespace Ocelot.UnitTests.UrlMatcher
|
||||
{
|
||||
using TestStack.BDDfy;
|
||||
|
||||
public class UrlPathToUrlTemplateMatcherTests
|
||||
{
|
||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
@ -3,12 +3,11 @@ using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests
|
||||
namespace Ocelot.UnitTests.UrlTemplateReplacer
|
||||
{
|
||||
using TestStack.BDDfy;
|
||||
|
||||
public class UpstreamUrlPathTemplateVariableReplacerTests
|
||||
{
|
||||
private DownstreamRoute _downstreamRoute;
|
Reference in New Issue
Block a user