Implement PostRawAsync, for asset uploading.

This commit is contained in:
Matt Burke
2013-10-02 15:56:24 -04:00
committed by Haacked
parent d0a5c5f9a6
commit 84b42b7074
3 changed files with 58 additions and 5 deletions
+27
View File
@@ -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<IHttpClient>();
IResponse<string> response = new ApiResponse<string>();
httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response));
var connection = new Connection(ExampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());
var body = new MemoryStream(new byte[] { 48, 49, 50 });
var headers = new Dictionary<string, string> { { "Content-Type", "application/arbitrary" } };
await connection.PostRawAsync<string>(new Uri("https://other.host.com/path?query=val"), body, headers);
httpClient.Received().Send<string>(Arg.Is<IRequest>(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]
+26 -5
View File
@@ -102,6 +102,32 @@ namespace Octokit.Http
return await SendData<T>(endpoint, HttpMethod.Post, body);
}
public async Task<IResponse<T>> PostRawAsync<T>(Uri endpoint, System.IO.Stream body, IDictionary<string, string> 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<T>(request);
apiInfoParser.ParseApiHttpHeaders(response);
jsonPipeline.DeserializeResponse(response);
return response;
}
public async Task<IResponse<T>> PutAsync<T>(Uri endpoint, object body)
{
return await SendData<T>(endpoint, HttpMethod.Put, body);
@@ -200,10 +226,5 @@ namespace Octokit.Http
throw new ApiException(response.Body, response.StatusCode);
}
}
public Task<IResponse<T>> PostRawAsync<T>(Uri endpoint, System.IO.Stream body, IDictionary<string, string> headers)
{
throw new NotImplementedException();
}
}
}
+5
View File
@@ -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)
{