diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index 13b20e19..d1eb8511 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; @@ -233,6 +235,31 @@ namespace Octokit.Tests.Http } } + public class ThePostRawAsyncMethod + { + [Fact] + public async Task RunsConfiguredAppWithAppropriateEnv() + { + var httpClient = Substitute.For(); + IResponse response = new ApiResponse(); + httpClient.Send(Args.Request).Returns(Task.FromResult(response)); + var connection = new Connection(ExampleUri, + Substitute.For(), + httpClient, + Substitute.For()); + + var body = new MemoryStream(new byte[] { 48, 49, 50 }); + var headers = new Dictionary { { "Content-Type", "application/arbitrary" } }; + await connection.PostRawAsync(new Uri("https://other.host.com/path?query=val"), body, headers); + + httpClient.Received().Send(Arg.Is(req => + req.BaseAddress == ExampleUri && + req.Body == body && + req.Method == HttpMethod.Post && + req.Endpoint == new Uri("https://other.host.com/path?query=val"))); + } + } + public class TheDeleteAsyncMethod { [Fact] diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index ac16394f..43814f7a 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -102,6 +102,32 @@ namespace Octokit.Http return await SendData(endpoint, HttpMethod.Post, body); } + public async Task> PostRawAsync(Uri endpoint, System.IO.Stream body, IDictionary headers) + { + Ensure.ArgumentNotNull(endpoint, "endpoint"); + Ensure.ArgumentNotNull(body, "body"); + + var request = new Request + { + Method = HttpMethod.Post, + BaseAddress = BaseAddress, + Endpoint = endpoint, + Body = body + }; + if (headers != null) + { + foreach (var header in headers) + { + request.Headers[header.Key] = header.Value; + } + } + authenticator.Apply(request); + var response = await httpClient.Send(request); + apiInfoParser.ParseApiHttpHeaders(response); + jsonPipeline.DeserializeResponse(response); + return response; + } + public async Task> PutAsync(Uri endpoint, object body) { return await SendData(endpoint, HttpMethod.Put, body); @@ -200,10 +226,5 @@ namespace Octokit.Http throw new ApiException(response.Body, response.StatusCode); } } - - public Task> PostRawAsync(Uri endpoint, System.IO.Stream body, IDictionary headers) - { - throw new NotImplementedException(); - } } } diff --git a/Octokit/Http/HttpClientAdapter.cs b/Octokit/Http/HttpClientAdapter.cs index b0a1461e..9cfbcd5d 100644 --- a/Octokit/Http/HttpClientAdapter.cs +++ b/Octokit/Http/HttpClientAdapter.cs @@ -58,6 +58,11 @@ namespace Octokit.Http { requestMessage.Content = new StringContent(body, Encoding.UTF8); } + var bodyStream = request.Body as System.IO.Stream; + if (bodyStream != null) + { + requestMessage.Content = new StreamContent(bodyStream); + } } catch (Exception) {