mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 19:08:16 +08:00
#263 map all content specific headers to downstream request content property, make sure we dont map them to request specific headers, added a gzip encoding acceptance test (#267)
This commit is contained in:
116
test/Ocelot.AcceptanceTests/GzipTests.cs
Normal file
116
test/Ocelot.AcceptanceTests/GzipTests.cs
Normal file
@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class GzipTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public GzipTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_simple_url()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var input = "people";
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Hello from Laura", "\"people\""))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.And(x => _steps.GivenThePostHasGzipContent(input))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expected)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
if(context.Request.Headers.TryGetValue("Content-Encoding", out var contentEncoding))
|
||||
{
|
||||
contentEncoding.First().ShouldBe("gzip");
|
||||
|
||||
string text = null;
|
||||
using (var decompress = new GZipStream(context.Request.Body, CompressionMode.Decompress))
|
||||
{
|
||||
using (var sr = new StreamReader(decompress)) {
|
||||
text = sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
if(text != expected)
|
||||
{
|
||||
throw new Exception("not gzipped");
|
||||
}
|
||||
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync("downstream path didnt match base path");
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -22,6 +22,8 @@ using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
@ -582,6 +584,22 @@ namespace Ocelot.AcceptanceTests
|
||||
_postContent = new StringContent(postcontent);
|
||||
}
|
||||
|
||||
public void GivenThePostHasGzipContent(object input)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(input);
|
||||
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
|
||||
{
|
||||
gzip.Write(jsonBytes, 0, jsonBytes.Length);
|
||||
}
|
||||
ms.Position = 0;
|
||||
StreamContent content = new StreamContent(ms);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
content.Headers.ContentEncoding.Add("gzip");
|
||||
_postContent = content;
|
||||
}
|
||||
|
||||
public void ThenTheResponseBodyShouldBe(string expectedBody)
|
||||
{
|
||||
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
|
||||
|
Reference in New Issue
Block a user