Skip to content

Commit

Permalink
Allow setting Content-Length header on the response
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Aug 16, 2024
1 parent 7e162a0 commit da80619
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 272 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
39 changes: 20 additions & 19 deletions src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ 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, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase)
{
{ HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() },
{ HttpKnownHeaderNames.ContentLength, (r, v) =>
{
if (long.TryParse(v.FirstOrDefault(), out var contentLength))
{
r.ContentLength = contentLength;
}
}
}
};

/// <summary>
Expand Down Expand Up @@ -83,20 +88,16 @@ 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) && int.TryParse(responseMessage.StatusCode as string, out var statusCodeTypeAsInt))
{
response.StatusCode = MapStatusCode(statusCodeTypeAsInt);
}
}

SetResponseHeaders(responseMessage, response);
Expand Down Expand Up @@ -183,7 +184,7 @@ private static void SetResponseHeaders(IResponseMessage responseMessage, IRespon
}
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 Down
Loading

0 comments on commit da80619

Please sign in to comment.