mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-19 07:43:48 +01:00
Add support for GZip and Deflate (#439)
* gzip - wip * wip * tests * fix gzip and deflate * CheckIfShouldKillVBCSCompiler * DisableRequestBodyDecompressing
This commit is contained in:
@@ -243,18 +243,36 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
// assign
|
||||
BodyData bodyData;
|
||||
if (body is byte[] b)
|
||||
bodyData = await BodyParser.Parse(new MemoryStream(b), null, true);
|
||||
{
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(b),
|
||||
ContentType = null,
|
||||
DeserializeJson = true
|
||||
};
|
||||
bodyData = await BodyParser.Parse(bodyParserSettings);
|
||||
}
|
||||
else if (body is string s)
|
||||
bodyData = await BodyParser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(s)), null, true);
|
||||
{
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(s)),
|
||||
ContentType = null,
|
||||
DeserializeJson = true
|
||||
};
|
||||
bodyData = await BodyParser.Parse(bodyParserSettings);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", bodyData);
|
||||
|
||||
// act
|
||||
var result = new RequestMatchResult();
|
||||
var score = matcher.GetMatchingScore(requestMessage, result);
|
||||
|
||||
|
||||
// assert
|
||||
Check.That(score).IsEqualTo(shouldMatch ? 1d : 0d);
|
||||
}
|
||||
@@ -265,7 +283,7 @@ namespace WireMock.Net.Tests.RequestMatchers
|
||||
{
|
||||
var json = "{'a':'b'}";
|
||||
var str = "HelloWorld";
|
||||
var bytes = new byte[] {0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00};
|
||||
var bytes = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 };
|
||||
|
||||
return new TheoryData<object, RequestMessageBodyMatcher, bool>
|
||||
{
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using NFluent;
|
||||
using System;
|
||||
using NFluent;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
@@ -19,10 +22,15 @@ namespace WireMock.Net.Tests.Util
|
||||
public async Task BodyParser_Parse_ContentTypeJson(string contentType, string bodyAsJson, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
|
||||
{
|
||||
// Arrange
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsJson));
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsJson)),
|
||||
ContentType = contentType,
|
||||
DeserializeJson = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var body = await BodyParser.Parse(memoryStream, contentType, true);
|
||||
var body = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
Check.That(body.BodyAsBytes).IsNotNull();
|
||||
@@ -38,10 +46,15 @@ namespace WireMock.Net.Tests.Util
|
||||
public async Task BodyParser_Parse_ContentTypeString(string contentType, string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
|
||||
{
|
||||
// Arrange
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString));
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString)),
|
||||
ContentType = contentType,
|
||||
DeserializeJson = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var body = await BodyParser.Parse(memoryStream, contentType, true);
|
||||
var body = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
Check.That(body.BodyAsBytes).IsNotNull();
|
||||
@@ -52,16 +65,21 @@ namespace WireMock.Net.Tests.Util
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new byte[] {34, 97, 34}, BodyType.Json)]
|
||||
[InlineData(new byte[] {97}, BodyType.String)]
|
||||
[InlineData(new byte[] {0xFF, 0xD8, 0xFF, 0xE0}, BodyType.Bytes)]
|
||||
[InlineData(new byte[] { 34, 97, 34 }, BodyType.Json)]
|
||||
[InlineData(new byte[] { 97 }, BodyType.String)]
|
||||
[InlineData(new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 }, BodyType.Bytes)]
|
||||
public async Task BodyParser_Parse_DetectedBodyType(byte[] content, BodyType detectedBodyType)
|
||||
{
|
||||
// arrange
|
||||
var memoryStream = new MemoryStream(content);
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(content),
|
||||
ContentType = null,
|
||||
DeserializeJson = true
|
||||
};
|
||||
|
||||
// act
|
||||
var body = await BodyParser.Parse(memoryStream, null, true);
|
||||
var body = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// assert
|
||||
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
|
||||
@@ -74,10 +92,15 @@ namespace WireMock.Net.Tests.Util
|
||||
public async Task BodyParser_Parse_DetectedBodyTypeNoJsonParsing(byte[] content, BodyType detectedBodyType)
|
||||
{
|
||||
// arrange
|
||||
var memoryStream = new MemoryStream(content);
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(content),
|
||||
ContentType = null,
|
||||
DeserializeJson = false
|
||||
};
|
||||
|
||||
// act
|
||||
var body = await BodyParser.Parse(memoryStream, null, false);
|
||||
var body = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// assert
|
||||
Check.That(body.DetectedBodyType).IsEqualTo(detectedBodyType);
|
||||
@@ -108,10 +131,15 @@ Content-Type: text/html
|
||||
|
||||
-----------------------------9051914041544843365972754266--";
|
||||
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(body));
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(body)),
|
||||
ContentType = contentType,
|
||||
DeserializeJson = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await BodyParser.Parse(memoryStream, contentType, true);
|
||||
var result = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
Check.That(result.DetectedBodyType).IsEqualTo(BodyType.String);
|
||||
@@ -127,11 +155,15 @@ Content-Type: text/html
|
||||
// Arrange
|
||||
string contentType = "multipart/form-data";
|
||||
string body = char.ConvertFromUtf32(0x1D161); //U+1D161 = MUSICAL SYMBOL SIXTEENTH NOTE
|
||||
|
||||
var memoryStream = new MemoryStream(Encoding.UTF32.GetBytes(body));
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(body)),
|
||||
ContentType = contentType,
|
||||
DeserializeJson = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await BodyParser.Parse(memoryStream, contentType, true);
|
||||
var result = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
Check.That(result.DetectedBodyType).IsEqualTo(BodyType.Bytes);
|
||||
@@ -142,14 +174,19 @@ Content-Type: text/html
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, "hello", BodyType.String, BodyType.Bytes)]
|
||||
public async Task BodyParser_Parse_ContentTypeIsNull(string contentType, string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
|
||||
[InlineData("hello", BodyType.String, BodyType.Bytes)]
|
||||
public async Task BodyParser_Parse_ContentTypeIsNull(string bodyAsString, BodyType detectedBodyType, BodyType detectedBodyTypeFromContentType)
|
||||
{
|
||||
// Arrange
|
||||
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString));
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(Encoding.UTF8.GetBytes(bodyAsString)),
|
||||
ContentType = null,
|
||||
DeserializeJson = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var body = await BodyParser.Parse(memoryStream, contentType, true);
|
||||
var body = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
Check.That(body.BodyAsBytes).IsNotNull();
|
||||
@@ -159,6 +196,60 @@ Content-Type: text/html
|
||||
Check.That(body.DetectedBodyTypeFromContentType).IsEqualTo(detectedBodyTypeFromContentType);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("gzip")]
|
||||
[InlineData("deflate")]
|
||||
public async Task BodyParser_Parse_ContentEncoding_GZip_And_DecompressGzipAndDeflate_Is_True_Should_Decompress(string compression)
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Encoding.ASCII.GetBytes("0");
|
||||
var compressed = CompressionUtils.Compress(compression, bytes);
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(compressed),
|
||||
ContentType = "text/plain",
|
||||
DeserializeJson = false,
|
||||
ContentEncoding = compression.ToUpperInvariant(),
|
||||
DecompressGZipAndDeflate = true
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
result.DetectedBodyType.Should().Be(BodyType.String);
|
||||
result.DetectedBodyTypeFromContentType.Should().Be(BodyType.String);
|
||||
result.BodyAsBytes.Should().BeEquivalentTo(new byte[] { 48 });
|
||||
result.BodyAsJson.Should().BeNull();
|
||||
result.BodyAsString.Should().Be("0");
|
||||
result.DetectedCompression.Should().Be(compression);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("gzip")]
|
||||
[InlineData("deflate")]
|
||||
public async Task BodyParser_Parse_ContentEncoding_GZip_And_DecompressGzipAndDeflate_Is_False_Should_Not_Decompress(string compression)
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Encoding.ASCII.GetBytes(Guid.NewGuid().ToString());
|
||||
var compressed = CompressionUtils.Compress(compression, bytes);
|
||||
var bodyParserSettings = new BodyParserSettings
|
||||
{
|
||||
Stream = new MemoryStream(compressed),
|
||||
ContentType = "text/plain",
|
||||
DeserializeJson = false,
|
||||
ContentEncoding = compression.ToUpperInvariant(),
|
||||
DecompressGZipAndDeflate = false
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await BodyParser.Parse(bodyParserSettings);
|
||||
|
||||
// Assert
|
||||
result.BodyAsBytes.Should().BeEquivalentTo(compressed);
|
||||
result.DetectedCompression.Should().BeNull();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("HEAD", false)]
|
||||
[InlineData("GET", false)]
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests
|
||||
@@ -252,5 +256,35 @@ namespace WireMock.Net.Tests
|
||||
Check.That(response.StatusCode).Equals(HttpStatusCode.Created);
|
||||
Check.That(await response.Content.ReadAsStringAsync()).Contains("Mapping added");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("gzip")]
|
||||
[InlineData("deflate")]
|
||||
public async Task WireMockServer_Should_SupportRequestGZipAndDeflate(string contentEncoding)
|
||||
{
|
||||
// Arrange
|
||||
const string body = "hello wiremock";
|
||||
byte[] compressed = CompressionUtils.Compress(contentEncoding, Encoding.UTF8.GetBytes(body));
|
||||
|
||||
var server = WireMockServer.Start();
|
||||
server.Given(
|
||||
Request.Create()
|
||||
.WithPath("/foo")
|
||||
.WithBody("hello wiremock")
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create().WithBody("OK")
|
||||
);
|
||||
|
||||
var content = new StreamContent(new MemoryStream(compressed));
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||
content.Headers.ContentEncoding.Add(contentEncoding);
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().PostAsync($"{server.Urls[0]}/foo", content);
|
||||
|
||||
// Assert
|
||||
Check.That(await response.Content.ReadAsStringAsync()).Contains("OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user