mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
finished removing fake, all tests now working
This commit is contained in:
parent
cdeb97731e
commit
581c9d01b9
@ -1,22 +1,21 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using Ocelot.Library.Infrastructure.Configuration.Yaml;
|
||||||
|
using Shouldly;
|
||||||
|
using TestStack.BDDfy;
|
||||||
|
using Xunit;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
||||||
namespace Ocelot.AcceptanceTests
|
namespace Ocelot.AcceptanceTests
|
||||||
{
|
{
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.TestHost;
|
|
||||||
using Ocelot.Library.Infrastructure.Configuration.Yaml;
|
|
||||||
using Shouldly;
|
|
||||||
using TestStack.BDDfy;
|
|
||||||
using Xunit;
|
|
||||||
using YamlDotNet.Serialization;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
|
|
||||||
public class OcelotTests : IDisposable
|
public class OcelotTests : IDisposable
|
||||||
{
|
{
|
||||||
private TestServer _server;
|
private TestServer _server;
|
||||||
@ -24,7 +23,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
private HttpResponseMessage _response;
|
private HttpResponseMessage _response;
|
||||||
private readonly string _configurationPath;
|
private readonly string _configurationPath;
|
||||||
private StringContent _postContent;
|
private StringContent _postContent;
|
||||||
private Task _fake;
|
private IWebHost _builder;
|
||||||
|
|
||||||
public OcelotTests()
|
public OcelotTests()
|
||||||
{
|
{
|
||||||
@ -44,12 +43,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_response_200()
|
public void should_return_response_200()
|
||||||
{
|
{
|
||||||
var serviceResponse = new DefaultHttpContext();
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||||
serviceResponse.Request.Method = "get";
|
|
||||||
serviceResponse.Response.Body = GenerateStreamFromString("Hello from Laura");
|
|
||||||
serviceResponse.Response.StatusCode = 200;
|
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", serviceResponse))
|
|
||||||
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
||||||
{
|
{
|
||||||
ReRoutes = new List<YamlReRoute>
|
ReRoutes = new List<YamlReRoute>
|
||||||
@ -72,11 +66,7 @@ namespace Ocelot.AcceptanceTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void should_return_response_201()
|
public void should_return_response_201()
|
||||||
{
|
{
|
||||||
var serviceResponse = new DefaultHttpContext();
|
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 201, string.Empty))
|
||||||
serviceResponse.Request.Method = "post";
|
|
||||||
serviceResponse.Response.StatusCode = 201;
|
|
||||||
|
|
||||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", serviceResponse))
|
|
||||||
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
.And(x => x.GivenThereIsAConfiguration(new YamlConfiguration
|
||||||
{
|
{
|
||||||
ReRoutes = new List<YamlReRoute>
|
ReRoutes = new List<YamlReRoute>
|
||||||
@ -100,15 +90,6 @@ namespace Ocelot.AcceptanceTests
|
|||||||
_postContent = new StringContent(postcontent);
|
_postContent = new StringContent(postcontent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Stream GenerateStreamFromString(string s)
|
|
||||||
{
|
|
||||||
var stream = new MemoryStream();
|
|
||||||
var writer = new StreamWriter(stream);
|
|
||||||
writer.Write(s);
|
|
||||||
writer.Flush();
|
|
||||||
stream.Position = 0;
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This is annoying cos it should be in the constructor but we need to set up the yaml file before calling startup so its a step.
|
/// This is annoying cos it should be in the constructor but we need to set up the yaml file before calling startup so its a step.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -135,34 +116,25 @@ namespace Ocelot.AcceptanceTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GivenThereIsAServiceRunningOn(string url, HttpContext httpContext)
|
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||||
{
|
{
|
||||||
var builder = new WebHostBuilder()
|
_builder = new WebHostBuilder()
|
||||||
.Configure(app =>
|
|
||||||
{
|
|
||||||
app.Run(async context =>
|
|
||||||
{
|
|
||||||
context.Response.Body = httpContext.Response.Body;
|
|
||||||
context.Response.StatusCode = httpContext.Response.StatusCode;
|
|
||||||
|
|
||||||
/* if (context.Request.Method.ToLower() == "get")
|
|
||||||
{
|
|
||||||
await context.Response.WriteAsync("Hello from Laura");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
context.Response.StatusCode = 201;
|
|
||||||
}*/
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.UseUrls(url)
|
.UseUrls(url)
|
||||||
.UseKestrel()
|
.UseKestrel()
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||||
.UseIISIntegration()
|
.UseIISIntegration()
|
||||||
.UseUrls(url)
|
.UseUrls(url)
|
||||||
|
.Configure(app =>
|
||||||
|
{
|
||||||
|
app.Run(async context =>
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = statusCode;
|
||||||
|
await context.Response.WriteAsync(responseBody);
|
||||||
|
});
|
||||||
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
_fake = Task.Run(() => builder.Run());
|
_builder.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
private void WhenIGetUrlOnTheApiGateway(string url)
|
||||||
@ -187,6 +159,10 @@ namespace Ocelot.AcceptanceTests
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
if (_builder != null)
|
||||||
|
{
|
||||||
|
_builder.Dispose();
|
||||||
|
}
|
||||||
_client.Dispose();
|
_client.Dispose();
|
||||||
_server.Dispose();
|
_server.Dispose();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user