mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
Allow setting Content-Length header on the response (#1158)
* Allow setting Content-Length header on the response * fix?
This commit is contained in:
@@ -137,7 +137,6 @@ message HelloReply {
|
|||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
RunOnLocal();
|
RunOnLocal();
|
||||||
return;
|
|
||||||
|
|
||||||
var mappingBuilder = new MappingBuilder();
|
var mappingBuilder = new MappingBuilder();
|
||||||
mappingBuilder
|
mappingBuilder
|
||||||
@@ -308,17 +307,6 @@ message HelloReply {
|
|||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
.WithBody("GraphQL is ok")
|
.WithBody("GraphQL is ok")
|
||||||
);
|
);
|
||||||
|
|
||||||
//server
|
|
||||||
// .AddGraphQLSchema("my-graphql", TestSchema, customScalars)
|
|
||||||
// .Given(Request.Create()
|
|
||||||
// .WithPath("/graphql2")
|
|
||||||
// .UsingPost()
|
|
||||||
// )
|
|
||||||
// .WithGraphQLSchema("my-graphql")
|
|
||||||
// .RespondWith(Response.Create()
|
|
||||||
// .WithBody("GraphQL is ok")
|
|
||||||
// );
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MIMEKIT
|
#if MIMEKIT
|
||||||
@@ -377,6 +365,15 @@ message HelloReply {
|
|||||||
.WithHeader("Content-Type", "text/plain")
|
.WithHeader("Content-Type", "text/plain")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create()
|
||||||
|
.UsingHead()
|
||||||
|
.WithPath("/cl")
|
||||||
|
)
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithHeader("Content-Length", "42")
|
||||||
|
);
|
||||||
|
|
||||||
server
|
server
|
||||||
.Given(Request.Create()
|
.Given(Request.Create()
|
||||||
.UsingMethod("GET")
|
.UsingMethod("GET")
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ namespace WireMock.Http;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal static class HttpKnownHeaderNames
|
internal static class HttpKnownHeaderNames
|
||||||
{
|
{
|
||||||
// https://docs.microsoft.com/en-us/dotnet/api/system.net.webheadercollection.isrestricted
|
// - https://docs.microsoft.com/en-us/dotnet/api/system.net.webheadercollection.isrestricted
|
||||||
|
// - ContentLength is allowed per #720
|
||||||
private static readonly string[] RestrictedResponseHeaders =
|
private static readonly string[] RestrictedResponseHeaders =
|
||||||
{
|
{
|
||||||
Accept,
|
Accept,
|
||||||
Connection,
|
Connection,
|
||||||
ContentLength,
|
|
||||||
ContentType,
|
ContentType,
|
||||||
Date, // RFC1123Pattern
|
Date, // RFC1123Pattern
|
||||||
Expect,
|
Expect,
|
||||||
|
|||||||
@@ -37,13 +37,20 @@ namespace WireMock.Owin.Mappers
|
|||||||
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
||||||
|
|
||||||
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
||||||
#if !USE_ASPNETCORE
|
private static readonly IDictionary<string, Action<IResponse, bool, WireMockList<string>>> ResponseHeadersToFix =
|
||||||
private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
|
new Dictionary<string, Action<IResponse, bool, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase)
|
||||||
#else
|
{
|
||||||
private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
|
{ HttpKnownHeaderNames.ContentType, (r, _, v) => r.ContentType = v.FirstOrDefault() },
|
||||||
#endif
|
{ HttpKnownHeaderNames.ContentLength, (r, hasBody, v) =>
|
||||||
{ HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }
|
{
|
||||||
};
|
// Only set the Content-Length header if the response does not have a body
|
||||||
|
if (!hasBody && long.TryParse(v.FirstOrDefault(), out var contentLength))
|
||||||
|
{
|
||||||
|
r.ContentLength = contentLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructor
|
/// Constructor
|
||||||
@@ -83,23 +90,21 @@ namespace WireMock.Owin.Mappers
|
|||||||
}
|
}
|
||||||
|
|
||||||
var statusCodeType = responseMessage.StatusCode?.GetType();
|
var statusCodeType = responseMessage.StatusCode?.GetType();
|
||||||
switch (statusCodeType)
|
if (statusCodeType != null)
|
||||||
{
|
{
|
||||||
case { } when statusCodeType == typeof(int) || statusCodeType == typeof(int?) || statusCodeType.GetTypeInfo().IsEnum:
|
if (statusCodeType == typeof(int) || statusCodeType == typeof(int?) || statusCodeType.GetTypeInfo().IsEnum)
|
||||||
|
{
|
||||||
response.StatusCode = MapStatusCode((int)responseMessage.StatusCode!);
|
response.StatusCode = MapStatusCode((int)responseMessage.StatusCode!);
|
||||||
break;
|
}
|
||||||
|
else if (statusCodeType == typeof(string))
|
||||||
case { } when statusCodeType == typeof(string):
|
{
|
||||||
// Note: this case will also match on null
|
// Note: this case will also match on null
|
||||||
int.TryParse(responseMessage.StatusCode as string, out var result);
|
int.TryParse(responseMessage.StatusCode as string, out var statusCodeTypeAsInt);
|
||||||
response.StatusCode = MapStatusCode(result);
|
response.StatusCode = MapStatusCode(statusCodeTypeAsInt);
|
||||||
break;
|
}
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SetResponseHeaders(responseMessage, response);
|
SetResponseHeaders(responseMessage, bytes, response);
|
||||||
|
|
||||||
if (bytes != null)
|
if (bytes != null)
|
||||||
{
|
{
|
||||||
@@ -160,7 +165,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetResponseHeaders(IResponseMessage responseMessage, IResponse response)
|
private static void SetResponseHeaders(IResponseMessage responseMessage, byte[]? bytes, IResponse response)
|
||||||
{
|
{
|
||||||
// Force setting the Date header (#577)
|
// Force setting the Date header (#577)
|
||||||
AppendResponseHeader(
|
AppendResponseHeader(
|
||||||
@@ -179,11 +184,11 @@ namespace WireMock.Owin.Mappers
|
|||||||
var value = item.Value;
|
var value = item.Value;
|
||||||
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
||||||
{
|
{
|
||||||
action?.Invoke(response, value);
|
action?.Invoke(response, bytes != null, value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Check if this response header can be added (#148 and #227)
|
// Check if this response header can be added (#148, #227 and #720)
|
||||||
if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
|
if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
|
||||||
{
|
{
|
||||||
AppendResponseHeader(response, headerName, value.ToArray());
|
AppendResponseHeader(response, headerName, value.ToArray());
|
||||||
@@ -206,7 +211,7 @@ namespace WireMock.Owin.Mappers
|
|||||||
var value = item.Value;
|
var value = item.Value;
|
||||||
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
|
||||||
{
|
{
|
||||||
action?.Invoke(response, value);
|
action?.Invoke(response, false, value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,278 +25,277 @@ using Response = Microsoft.AspNetCore.Http.HttpResponse;
|
|||||||
using Microsoft.Extensions.Primitives;
|
using Microsoft.Extensions.Primitives;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace WireMock.Net.Tests.Owin.Mappers
|
namespace WireMock.Net.Tests.Owin.Mappers;
|
||||||
|
|
||||||
|
public class OwinResponseMapperTests
|
||||||
{
|
{
|
||||||
public class OwinResponseMapperTests
|
private static readonly Task CompletedTask = Task.FromResult(true);
|
||||||
|
private readonly OwinResponseMapper _sut;
|
||||||
|
private readonly Mock<IResponse> _responseMock;
|
||||||
|
private readonly Mock<Stream> _stream;
|
||||||
|
private readonly Mock<IHeaderDictionary> _headers;
|
||||||
|
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
|
||||||
|
private readonly Mock<IWireMockMiddlewareOptions> _optionsMock;
|
||||||
|
|
||||||
|
public OwinResponseMapperTests()
|
||||||
{
|
{
|
||||||
private static readonly Task CompletedTask = Task.FromResult(true);
|
_stream = new Mock<Stream>();
|
||||||
private readonly OwinResponseMapper _sut;
|
_stream.SetupAllProperties();
|
||||||
private readonly Mock<IResponse> _responseMock;
|
_stream.Setup(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>())).Returns(CompletedTask);
|
||||||
private readonly Mock<Stream> _stream;
|
|
||||||
private readonly Mock<IHeaderDictionary> _headers;
|
|
||||||
private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock;
|
|
||||||
private readonly Mock<IWireMockMiddlewareOptions> _optionsMock;
|
|
||||||
|
|
||||||
public OwinResponseMapperTests()
|
_fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
||||||
{
|
_fileSystemHandlerMock.SetupAllProperties();
|
||||||
_stream = new Mock<Stream>();
|
|
||||||
_stream.SetupAllProperties();
|
|
||||||
_stream.Setup(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>())).Returns(CompletedTask);
|
|
||||||
|
|
||||||
_fileSystemHandlerMock = new Mock<IFileSystemHandler>();
|
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
||||||
_fileSystemHandlerMock.SetupAllProperties();
|
_optionsMock.SetupAllProperties();
|
||||||
|
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
||||||
|
|
||||||
_optionsMock = new Mock<IWireMockMiddlewareOptions>();
|
_headers = new Mock<IHeaderDictionary>();
|
||||||
_optionsMock.SetupAllProperties();
|
_headers.SetupAllProperties();
|
||||||
_optionsMock.SetupGet(o => o.FileSystemHandler).Returns(_fileSystemHandlerMock.Object);
|
|
||||||
|
|
||||||
_headers = new Mock<IHeaderDictionary>();
|
|
||||||
_headers.SetupAllProperties();
|
|
||||||
#if NET452
|
#if NET452
|
||||||
_headers.Setup(h => h.AppendValues(It.IsAny<string>(), It.IsAny<string[]>()));
|
_headers.Setup(h => h.AppendValues(It.IsAny<string>(), It.IsAny<string[]>()));
|
||||||
#else
|
#else
|
||||||
_headers.Setup(h => h.Add(It.IsAny<string>(), It.IsAny<StringValues>()));
|
_headers.Setup(h => h.Add(It.IsAny<string>(), It.IsAny<StringValues>()));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
_responseMock = new Mock<IResponse>();
|
_responseMock = new Mock<IResponse>();
|
||||||
_responseMock.SetupAllProperties();
|
_responseMock.SetupAllProperties();
|
||||||
_responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
|
_responseMock.SetupGet(r => r.Body).Returns(_stream.Object);
|
||||||
_responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);
|
_responseMock.SetupGet(r => r.Headers).Returns(_headers.Object);
|
||||||
|
|
||||||
_sut = new OwinResponseMapper(_optionsMock.Object);
|
_sut = new OwinResponseMapper(_optionsMock.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_Null()
|
public async Task OwinResponseMapper_MapAsync_Null()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
await _sut.MapAsync(null, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(300, 300)]
|
||||||
|
[InlineData(500, 500)]
|
||||||
|
public async Task OwinResponseMapper_MapAsync_Valid_StatusCode(object code, int expected)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Act
|
StatusCode = code
|
||||||
await _sut.MapAsync(null, _responseMock.Object).ConfigureAwait(false);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
// Act
|
||||||
[InlineData(300, 300)]
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
[InlineData(500, 500)]
|
|
||||||
public async Task OwinResponseMapper_MapAsync_Valid_StatusCode(object code, int expected)
|
// Assert
|
||||||
|
_responseMock.VerifySet(r => r.StatusCode = expected, Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData(0, 200)]
|
||||||
|
[InlineData(-1, 200)]
|
||||||
|
[InlineData(10000, 200)]
|
||||||
|
[InlineData(300, 300)]
|
||||||
|
public async Task OwinResponseMapper_MapAsync_Invalid_StatusCode_When_AllowOnlyDefinedHttpStatusCodeInResponseSet_Is_True(object code, int expected)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_optionsMock.SetupGet(o => o.AllowOnlyDefinedHttpStatusCodeInResponse).Returns(true);
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
StatusCode = code
|
||||||
var responseMessage = new ResponseMessage
|
};
|
||||||
{
|
|
||||||
StatusCode = code
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_responseMock.VerifySet(r => r.StatusCode = expected, Times.Once);
|
_responseMock.VerifySet(r => r.StatusCode = expected, Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Fact]
|
||||||
[InlineData(0, 200)]
|
public async Task OwinResponseMapper_MapAsync_StatusCode_Is_Null()
|
||||||
[InlineData(-1, 200)]
|
{
|
||||||
[InlineData(10000, 200)]
|
// Arrange
|
||||||
[InlineData(300, 300)]
|
var responseMessage = new ResponseMessage
|
||||||
public async Task OwinResponseMapper_MapAsync_Invalid_StatusCode_When_AllowOnlyDefinedHttpStatusCodeInResponseSet_Is_True(object code, int expected)
|
|
||||||
{
|
{
|
||||||
// Arrange
|
StatusCode = null
|
||||||
_optionsMock.SetupGet(o => o.AllowOnlyDefinedHttpStatusCodeInResponse).Returns(true);
|
};
|
||||||
var responseMessage = new ResponseMessage
|
|
||||||
{
|
|
||||||
StatusCode = code
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_responseMock.VerifySet(r => r.StatusCode = expected, Times.Once);
|
_responseMock.VerifySet(r => r.StatusCode = It.IsAny<int>(), Times.Never);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public async Task OwinResponseMapper_MapAsync_StatusCode_Is_Null()
|
[InlineData(0, 0)]
|
||||||
|
[InlineData(-1, -1)]
|
||||||
|
[InlineData(10000, 10000)]
|
||||||
|
[InlineData(300, 300)]
|
||||||
|
public async Task OwinResponseMapper_MapAsync_StatusCode_Is_NotInEnumRange(object code, int expected)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
StatusCode = code
|
||||||
var responseMessage = new ResponseMessage
|
};
|
||||||
{
|
|
||||||
StatusCode = null
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_responseMock.VerifySet(r => r.StatusCode = It.IsAny<int>(), Times.Never);
|
_responseMock.VerifySet(r => r.StatusCode = expected, Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Fact]
|
||||||
[InlineData(0, 0)]
|
public async Task OwinResponseMapper_MapAsync_NoBody()
|
||||||
[InlineData(-1, -1)]
|
{
|
||||||
[InlineData(10000, 10000)]
|
// Arrange
|
||||||
[InlineData(300, 300)]
|
var responseMessage = new ResponseMessage
|
||||||
public async Task OwinResponseMapper_MapAsync_StatusCode_Is_NotInEnumRange(object code, int expected)
|
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>()
|
||||||
var responseMessage = new ResponseMessage
|
};
|
||||||
{
|
|
||||||
StatusCode = code
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_responseMock.VerifySet(r => r.StatusCode = expected, Times.Once);
|
_stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Never);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_NoBody()
|
public async Task OwinResponseMapper_MapAsync_Body()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string body = "abcd";
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
var responseMessage = new ResponseMessage
|
BodyData = new BodyData { DetectedBodyType = BodyType.String, BodyAsString = body }
|
||||||
{
|
};
|
||||||
Headers = new Dictionary<string, WireMockList<string>>()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()), Times.Never);
|
_stream.Verify(s => s.WriteAsync(new byte[] { 97, 98, 99, 100 }, 0, 4, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_Body()
|
public async Task OwinResponseMapper_MapAsync_BodyAsBytes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var bytes = new byte[] { 48, 49 };
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
string body = "abcd";
|
BodyData = new BodyData { DetectedBodyType = BodyType.Bytes, BodyAsBytes = bytes }
|
||||||
var responseMessage = new ResponseMessage
|
};
|
||||||
{
|
|
||||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
|
||||||
BodyData = new BodyData { DetectedBodyType = BodyType.String, BodyAsString = body }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_stream.Verify(s => s.WriteAsync(new byte[] { 97, 98, 99, 100 }, 0, 4, It.IsAny<CancellationToken>()), Times.Once);
|
_stream.Verify(s => s.WriteAsync(bytes, 0, bytes.Length, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_BodyAsBytes()
|
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var json = new { t = "x", i = (string?)null };
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
var bytes = new byte[] { 48, 49 };
|
BodyData = new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = json, BodyAsJsonIndented = false }
|
||||||
var responseMessage = new ResponseMessage
|
};
|
||||||
{
|
|
||||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
|
||||||
BodyData = new BodyData { DetectedBodyType = BodyType.Bytes, BodyAsBytes = bytes }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_stream.Verify(s => s.WriteAsync(bytes, 0, bytes.Length, It.IsAny<CancellationToken>()), Times.Once);
|
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_BodyAsJson()
|
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x", "y") } }
|
||||||
var json = new { t = "x", i = (string?)null };
|
};
|
||||||
var responseMessage = new ResponseMessage
|
|
||||||
{
|
|
||||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
|
||||||
BodyData = new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = json, BodyAsJsonIndented = false }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_stream.Verify(s => s.WriteAsync(new byte[] { 123, 34, 116, 34, 58, 34, 120, 34, 125 }, 0, 9, It.IsAny<CancellationToken>()), Times.Once);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task OwinResponseMapper_MapAsync_SetResponseHeaders()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var responseMessage = new ResponseMessage
|
|
||||||
{
|
|
||||||
Headers = new Dictionary<string, WireMockList<string>> { { "h", new WireMockList<string>("x", "y") } }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
#if NET452
|
#if NET452
|
||||||
_headers.Verify(h => h.AppendValues("h", new string[] { "x", "y" }), Times.Once);
|
_headers.Verify(h => h.AppendValues("h", new string[] { "x", "y" }), Times.Once);
|
||||||
#else
|
#else
|
||||||
var v = new StringValues();
|
var v = new StringValues();
|
||||||
_headers.Verify(h => h.TryGetValue("h", out v), Times.Once);
|
_headers.Verify(h => h.TryGetValue("h", out v), Times.Once);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void OwinResponseMapper_MapAsync_BodyAsFile_ThrowsException()
|
public void OwinResponseMapper_MapAsync_BodyAsFile_ThrowsException()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
var responseMessage = new ResponseMessage
|
BodyData = new BodyData { DetectedBodyType = BodyType.File, BodyAsFile = string.Empty }
|
||||||
{
|
};
|
||||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
_fileSystemHandlerMock.Setup(f => f.ReadResponseBodyAsFile(It.IsAny<string>())).Throws<FileNotFoundException>();
|
||||||
BodyData = new BodyData { DetectedBodyType = BodyType.File, BodyAsFile = string.Empty }
|
|
||||||
};
|
|
||||||
_fileSystemHandlerMock.Setup(f => f.ReadResponseBodyAsFile(It.IsAny<string>())).Throws<FileNotFoundException>();
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
Func<Task> action = () => _sut.MapAsync(responseMessage, _responseMock.Object);
|
Func<Task> action = () => _sut.MapAsync(responseMessage, _responseMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
action.Should().ThrowAsync<FileNotFoundException>();
|
action.Should().ThrowAsync<FileNotFoundException>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OwinResponseMapper_MapAsync_WithFault_EMPTY_RESPONSE()
|
public async Task OwinResponseMapper_MapAsync_WithFault_EMPTY_RESPONSE()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
string body = "abc";
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
string body = "abc";
|
BodyData = new BodyData { DetectedBodyType = BodyType.String, BodyAsString = body },
|
||||||
var responseMessage = new ResponseMessage
|
FaultType = FaultType.EMPTY_RESPONSE
|
||||||
{
|
};
|
||||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
|
||||||
BodyData = new BodyData { DetectedBodyType = BodyType.String, BodyAsString = body },
|
|
||||||
FaultType = FaultType.EMPTY_RESPONSE
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_stream.Verify(s => s.WriteAsync(EmptyArray<byte>.Value, 0, 0, It.IsAny<CancellationToken>()), Times.Once);
|
_stream.Verify(s => s.WriteAsync(EmptyArray<byte>.Value, 0, 0, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("abcd", BodyType.String)]
|
[InlineData("abcd", BodyType.String)]
|
||||||
[InlineData("", BodyType.String)]
|
[InlineData("", BodyType.String)]
|
||||||
[InlineData(null, BodyType.None)]
|
[InlineData(null, BodyType.None)]
|
||||||
public async Task OwinResponseMapper_MapAsync_WithFault_MALFORMED_RESPONSE_CHUNK(string body, BodyType detected)
|
public async Task OwinResponseMapper_MapAsync_WithFault_MALFORMED_RESPONSE_CHUNK(string body, BodyType detected)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var responseMessage = new ResponseMessage
|
||||||
{
|
{
|
||||||
// Arrange
|
Headers = new Dictionary<string, WireMockList<string>>(),
|
||||||
var responseMessage = new ResponseMessage
|
BodyData = new BodyData { DetectedBodyType = detected, BodyAsString = body },
|
||||||
{
|
StatusCode = 100,
|
||||||
Headers = new Dictionary<string, WireMockList<string>>(),
|
FaultType = FaultType.MALFORMED_RESPONSE_CHUNK
|
||||||
BodyData = new BodyData { DetectedBodyType = detected, BodyAsString = body },
|
};
|
||||||
StatusCode = 100,
|
|
||||||
FaultType = FaultType.MALFORMED_RESPONSE_CHUNK
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
await _sut.MapAsync(responseMessage, _responseMock.Object).ConfigureAwait(false);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
_responseMock.VerifySet(r => r.StatusCode = 100, Times.Once);
|
_responseMock.VerifySet(r => r.StatusCode = 100, Times.Once);
|
||||||
_stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), 0, It.Is<int>(count => count >= 0), It.IsAny<CancellationToken>()), Times.Once);
|
_stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), 0, It.Is<int>(count => count >= 0), It.IsAny<CancellationToken>()), Times.Once);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@ using FluentAssertions;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
|
using WireMock.Http;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Net.Tests.Serialization;
|
using WireMock.Net.Tests.Serialization;
|
||||||
using WireMock.Net.Xunit;
|
using WireMock.Net.Xunit;
|
||||||
@@ -351,7 +352,7 @@ public partial class WireMockServerTests
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockServer_Should_exclude_restrictedResponseHeader()
|
public async Task WireMockServer_Should_Exclude_RestrictedResponseHeader()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
string path = $"/foo_{Guid.NewGuid()}";
|
string path = $"/foo_{Guid.NewGuid()}";
|
||||||
@@ -371,6 +372,26 @@ public partial class WireMockServerTests
|
|||||||
server.Stop();
|
server.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact] // #720
|
||||||
|
public async Task WireMockServer_Should_AllowResponseHeaderContentLength_For_HEAD()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
const string length = "42";
|
||||||
|
var path = $"/cl_{Guid.NewGuid()}";
|
||||||
|
using var server = WireMockServer.Start();
|
||||||
|
|
||||||
|
server
|
||||||
|
.Given(Request.Create().WithPath(path).UsingHead())
|
||||||
|
.RespondWith(Response.Create().WithHeader(HttpKnownHeaderNames.ContentLength, length));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, path);
|
||||||
|
var response = await server.CreateClient().SendAsync(httpRequestMessage).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.Content.Headers.GetValues(HttpKnownHeaderNames.ContentLength).Should().Contain(length);
|
||||||
|
}
|
||||||
|
|
||||||
#if !NET452 && !NET461
|
#if !NET452 && !NET461
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("TRACE")]
|
[InlineData("TRACE")]
|
||||||
|
|||||||
Reference in New Issue
Block a user