Skip to content

Commit 1f5d2bf

Browse files
committed
Fix handling HTTP responses without a body (#412)
1 parent 92c85ea commit 1f5d2bf

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Utility/TypeExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,9 @@ private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext)
166166
StatusCode = httpResponseContext.StatusCode.ToString("d")
167167
};
168168

169-
if (httpResponseContext.Body != null)
170-
{
171-
rpcHttp.Body = httpResponseContext.Body.ToTypedData();
172-
}
169+
rpcHttp.Body = httpResponseContext.Body == null
170+
? string.Empty.ToTypedData()
171+
: httpResponseContext.Body.ToTypedData();
173172

174173
rpcHttp.EnableContentNegotiation = httpResponseContext.EnableContentNegotiation;
175174

test/Unit/Utility/TypeExtensionsTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,31 @@ public void TestObjectToTypedData_WhenBodyIsNotJsonAndContentTypeIsSpecified_Add
576576
Assert.Equal("text/html", input.ToTypedData().Http.Headers["content-type"]);
577577
}
578578

579+
[Theory]
580+
[InlineData(null, HttpStatusCode.OK)]
581+
[InlineData("text/plain", HttpStatusCode.OK)]
582+
[InlineData("application/json", HttpStatusCode.OK)]
583+
[InlineData("anything/else", HttpStatusCode.OK)]
584+
[InlineData(null, HttpStatusCode.NoContent)]
585+
[InlineData("text/plain", HttpStatusCode.NoContent)]
586+
[InlineData("application/json", HttpStatusCode.NoContent)]
587+
[InlineData("anything/else", HttpStatusCode.NoContent)]
588+
public void TestObjectToTypedData_WhenBodyIsNull(string contentType, HttpStatusCode statusCode)
589+
{
590+
var input = new HttpResponseContext
591+
{
592+
Body = null,
593+
ContentType = contentType,
594+
StatusCode = statusCode,
595+
};
596+
597+
var result = input.ToTypedData().Http;
598+
599+
Assert.Equal(string.Empty, result.Body.String);
600+
var expectedContentType = contentType ?? "text/plain";
601+
Assert.Equal(expectedContentType, result.Headers["content-type"]);
602+
}
603+
579604
[Fact]
580605
public void TestObjectToTypedDataInt()
581606
{

0 commit comments

Comments
 (0)