Skip to content

Commit

Permalink
Allow setting Content-Length header on the response (#1158)
Browse files Browse the repository at this point in the history
* Allow setting Content-Length header on the response

* fix?
  • Loading branch information
StefH authored Aug 16, 2024
1 parent 7e162a0 commit 0884440
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 277 deletions.
21 changes: 9 additions & 12 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ private static void RunOnLocal()
public static void Run()
{
RunOnLocal();
return;

var mappingBuilder = new MappingBuilder();
mappingBuilder
Expand Down Expand Up @@ -308,17 +307,6 @@ public static void Run()
.RespondWith(Response.Create()
.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

#if MIMEKIT
Expand Down Expand Up @@ -377,6 +365,15 @@ public static void Run()
.WithHeader("Content-Type", "text/plain")
);

server
.Given(Request.Create()
.UsingHead()
.WithPath("/cl")
)
.RespondWith(Response.Create()
.WithHeader("Content-Length", "42")
);

server
.Given(Request.Create()
.UsingMethod("GET")
Expand Down
4 changes: 2 additions & 2 deletions src/WireMock.Net/Http/HttpKnownHeaderNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace WireMock.Http;
/// </summary>
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 =
{
Accept,
Connection,
ContentLength,
ContentType,
Date, // RFC1123Pattern
Expect,
Expand Down
53 changes: 29 additions & 24 deletions src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ internal class OwinResponseMapper : IOwinResponseMapper
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);

// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
#if !USE_ASPNETCORE
private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
#else
private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
#endif
{ HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }
};
private static readonly IDictionary<string, Action<IResponse, bool, WireMockList<string>>> ResponseHeadersToFix =
new Dictionary<string, Action<IResponse, bool, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase)
{
{ HttpKnownHeaderNames.ContentType, (r, _, v) => r.ContentType = v.FirstOrDefault() },
{ HttpKnownHeaderNames.ContentLength, (r, hasBody, v) =>
{
// 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>
/// Constructor
Expand Down Expand Up @@ -83,23 +90,21 @@ public async Task MapAsync(IResponseMessage? responseMessage, IResponse response
}

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!);
break;

case { } when statusCodeType == typeof(string):
// Note: this case will also match on null
int.TryParse(responseMessage.StatusCode as string, out var result);
response.StatusCode = MapStatusCode(result);
break;

default:
break;
}
else if (statusCodeType == typeof(string))
{
// Note: this case will also match on null
int.TryParse(responseMessage.StatusCode as string, out var statusCodeTypeAsInt);
response.StatusCode = MapStatusCode(statusCodeTypeAsInt);
}
}

SetResponseHeaders(responseMessage, response);
SetResponseHeaders(responseMessage, bytes, response);

if (bytes != null)
{
Expand Down Expand Up @@ -160,7 +165,7 @@ private bool IsFault(IResponseMessage responseMessage)
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)
AppendResponseHeader(
Expand All @@ -179,11 +184,11 @@ private static void SetResponseHeaders(IResponseMessage responseMessage, IRespon
var value = item.Value;
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
{
action?.Invoke(response, value);
action?.Invoke(response, bytes != null, value);
}
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))
{
AppendResponseHeader(response, headerName, value.ToArray());
Expand All @@ -206,7 +211,7 @@ private static void SetResponseTrailingHeaders(IResponseMessage responseMessage,
var value = item.Value;
if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
{
action?.Invoke(response, value);
action?.Invoke(response, false, value);
}
else
{
Expand Down
Loading

0 comments on commit 0884440

Please sign in to comment.