Support requests with HttpContent body

If a 3rd party client needs to provide a specific HttpContent, we should
allow that in the adapter. Our clients probably shouldn't do this as it
would break encapsulation.
This commit is contained in:
Haacked
2013-10-11 14:34:09 -07:00
parent db0dc1c00b
commit db2f805bc8
3 changed files with 26 additions and 2 deletions

View File

@@ -3,7 +3,6 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using NSubstitute; using NSubstitute;
using Octokit.Internal;
using Xunit; using Xunit;
using Xunit.Extensions; using Xunit.Extensions;

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
@@ -57,6 +58,23 @@ namespace Octokit.Tests.Http
Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType); Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType);
} }
[Fact]
public void SetsHttpContentBody()
{
var request = new Request
{
Method = HttpMethod.Post,
Body = new FormUrlEncodedContent(new Dictionary<string, string> {{"foo", "bar"}})
};
var tester = new HttpClientAdapterTester();
var requestMessage = tester.BuildRequestMessageTester(request);
Assert.NotNull(requestMessage.Content);
Assert.IsType<FormUrlEncodedContent>(requestMessage.Content);
Assert.Equal("application/x-www-form-urlencoded", requestMessage.Content.Headers.ContentType.MediaType);
}
[Fact] [Fact]
public void EnsuresArguments() public void EnsuresArguments()
{ {

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
@@ -68,13 +69,19 @@ namespace Octokit.Internal
{ {
requestMessage.Headers.Add(header.Key, header.Value); requestMessage.Headers.Add(header.Key, header.Value);
} }
var httpContent = request.Body as HttpContent;
if (httpContent != null)
{
requestMessage.Content = httpContent;
}
var body = request.Body as string; var body = request.Body as string;
if (body != null) if (body != null)
{ {
requestMessage.Content = new StringContent(body, Encoding.UTF8, request.ContentType); requestMessage.Content = new StringContent(body, Encoding.UTF8, request.ContentType);
} }
var bodyStream = request.Body as System.IO.Stream;
var bodyStream = request.Body as Stream;
if (bodyStream != null) if (bodyStream != null)
{ {
requestMessage.Content = new StreamContent(bodyStream); requestMessage.Content = new StreamContent(bodyStream);