Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HeadMessageHandler: Support empty content eg. 302 #134

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/WebApiContrib/MessageHandlers/HeadMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
.ContinueWith<HttpResponseMessage>(task => {
var response = task.Result;
response.RequestMessage.Method = HttpMethod.Head;
response.Content = new HeadContent(response.Content);
if (response.Content != null)
response.Content = new HeadContent(response.Content);
return task.Result;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Should_return_headers_and_no_body_when_method_is_head()

var response = client.SendAsync(requestMessage).Result;

Assert.AreEqual("Boo",response.ReasonPhrase);
Assert.AreEqual("Boo", response.ReasonPhrase);
Assert.AreEqual(0, response.Content.Headers.ContentLength);
}

Expand All @@ -48,5 +48,27 @@ public void Should_do_nothing_when_method_is_get()
Assert.AreEqual("Boo", response.ReasonPhrase);
Assert.AreEqual("Hello world", response.Content.ReadAsStringAsync().Result);
}

[Test]
public void Should_support_empry_bodies()
{
var messageHandler302 = new HeadMessageHandler();
var reasonPhrase = "302 from Controller.Redirect(url)";
messageHandler302.InnerHandler = new PrecannedMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
{
ReasonPhrase = reasonPhrase,
Content = null
});

var requestMessage = new HttpRequestMessage(HttpMethod.Head, "http://foo/bar");

var client = new HttpClient(messageHandler302);

var response = client.SendAsync(requestMessage).Result;

Assert.AreEqual(reasonPhrase, response.ReasonPhrase);
Assert.AreEqual(null, response.Content);

}
}
}