mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 03:16:11 +00:00
Set request content type appropriately
This commit is contained in:
@@ -35,8 +35,10 @@ namespace Octokit.Tests.Http
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
req.Method == HttpMethod.Get &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
req.ContentType == null &&
|
||||
req.Body == null &&
|
||||
req.Method == HttpMethod.Get &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -57,8 +59,8 @@ namespace Octokit.Tests.Http
|
||||
|
||||
httpClient.Received(3).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
req.Method == HttpMethod.Get &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
req.Method == HttpMethod.Get &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -147,9 +149,11 @@ namespace Octokit.Tests.Http
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
req.Method == HttpMethod.Get &&
|
||||
req.Headers["Accept"] == "application/vnd.github.html" &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
req.ContentType == null &&
|
||||
req.Body == null &&
|
||||
req.Method == HttpMethod.Get &&
|
||||
req.Headers["Accept"] == "application/vnd.github.html" &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,16 +176,41 @@ namespace Octokit.Tests.Http
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
(string)req.Body == data &&
|
||||
req.Method == HttpVerb.Patch &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
(string)req.Body == data &&
|
||||
req.Method == HttpVerb.Patch &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
}
|
||||
|
||||
public class ThePutAsyncMethod
|
||||
{
|
||||
[Fact]
|
||||
public async Task RunsConfiguredAppWithAppropriateEnv()
|
||||
{
|
||||
string data = SimpleJson.SerializeObject(new object());
|
||||
var httpClient = Substitute.For<IHttpClient>();
|
||||
IResponse<string> response = new ApiResponse<string>();
|
||||
httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response));
|
||||
var connection = new Connection("Test Runner",
|
||||
ExampleUri,
|
||||
Substitute.For<ICredentialStore>(),
|
||||
httpClient,
|
||||
Substitute.For<IJsonSerializer>());
|
||||
|
||||
await connection.PutAsync<string>(new Uri("/endpoint", UriKind.Relative), new object());
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
(string)req.Body == data &&
|
||||
req.Method == HttpMethod.Put &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
}
|
||||
|
||||
public class ThePostAsyncMethod
|
||||
{
|
||||
[Fact]
|
||||
public async Task RunsConfiguredAppWithAppropriateEnv()
|
||||
public async Task SendsProperlyFormattedPostRequest()
|
||||
{
|
||||
string data = SimpleJson.SerializeObject(new object());
|
||||
var httpClient = Substitute.For<IHttpClient>();
|
||||
@@ -197,16 +226,39 @@ namespace Octokit.Tests.Http
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
(string)req.Body == data &&
|
||||
req.Method == HttpMethod.Post &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
req.ContentType == "application/x-www-form-urlencoded" &&
|
||||
(string)req.Body == data &&
|
||||
req.Method == HttpMethod.Post &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithNoBodySetsNoContentType()
|
||||
{
|
||||
var httpClient = Substitute.For<IHttpClient>();
|
||||
IResponse<string> response = new ApiResponse<string>();
|
||||
httpClient.Send<string>(Args.Request).Returns(Task.FromResult(response));
|
||||
var connection = new Connection("Test Runner",
|
||||
ExampleUri,
|
||||
Substitute.For<ICredentialStore>(),
|
||||
httpClient,
|
||||
Substitute.For<IJsonSerializer>());
|
||||
|
||||
await connection.PostAsync<string>(new Uri("/endpoint", UriKind.Relative), null);
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
req.ContentType == null &&
|
||||
req.Body == null &&
|
||||
req.Method == HttpMethod.Post &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
}
|
||||
|
||||
public class ThePostRawAsyncMethod
|
||||
{
|
||||
[Fact]
|
||||
public async Task RunsConfiguredAppWithAppropriateEnv()
|
||||
public async Task SendsProperlyFormattedPostRequestWithCorrectHeaders()
|
||||
{
|
||||
var httpClient = Substitute.For<IHttpClient>();
|
||||
IResponse<string> response = new ApiResponse<string>();
|
||||
@@ -232,7 +284,7 @@ namespace Octokit.Tests.Http
|
||||
public class TheDeleteAsyncMethod
|
||||
{
|
||||
[Fact]
|
||||
public async Task RunsConfiguredAppWithAppropriateEnv()
|
||||
public async Task SendsProperlyFormattedDeleteRequest()
|
||||
{
|
||||
var httpClient = Substitute.For<IHttpClient>();
|
||||
IResponse<string> response = new ApiResponse<string>();
|
||||
@@ -247,8 +299,10 @@ namespace Octokit.Tests.Http
|
||||
|
||||
httpClient.Received(1).Send<string>(Arg.Is<IRequest>(req =>
|
||||
req.BaseAddress == ExampleUri &&
|
||||
req.Method == HttpMethod.Delete &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
req.Body == null &&
|
||||
req.ContentType == null &&
|
||||
req.Method == HttpMethod.Delete &&
|
||||
req.Endpoint == new Uri("/endpoint", UriKind.Relative)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,15 +28,33 @@ namespace Octokit.Tests.Http
|
||||
};
|
||||
var tester = new HttpClientAdapterTester();
|
||||
|
||||
var responseMessage = tester.BuildRequestMessageTester(request);
|
||||
|
||||
Assert.Equal(2, responseMessage.Headers.Count());
|
||||
var firstHeader = responseMessage.Headers.First();
|
||||
var requestMessage = tester.BuildRequestMessageTester(request);
|
||||
|
||||
Assert.Equal(2, requestMessage.Headers.Count());
|
||||
var firstHeader = requestMessage.Headers.First();
|
||||
Assert.Equal("foo", firstHeader.Key);
|
||||
Assert.Equal("bar", firstHeader.Value.First());
|
||||
var lastHeader = responseMessage.Headers.Last();
|
||||
var lastHeader = requestMessage.Headers.Last();
|
||||
Assert.Equal("blah", lastHeader.Key);
|
||||
Assert.Equal("blase", lastHeader.Value.First());
|
||||
Assert.Null(requestMessage.Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetsBodyAndContentType()
|
||||
{
|
||||
var request = new Request
|
||||
{
|
||||
Method = HttpMethod.Post,
|
||||
Body = "{}",
|
||||
ContentType = "text/plain"
|
||||
};
|
||||
var tester = new HttpClientAdapterTester();
|
||||
|
||||
var requestMessage = tester.BuildRequestMessageTester(request);
|
||||
|
||||
Assert.NotNull(requestMessage.Content);
|
||||
Assert.Equal("text/plain", requestMessage.Content.Headers.ContentType.MediaType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+19
-13
@@ -99,13 +99,8 @@ namespace Octokit.Http
|
||||
Ensure.ArgumentNotNull(endpoint, "endpoint");
|
||||
Ensure.ArgumentNotNull(body, "body");
|
||||
|
||||
return await Run<T>(new Request
|
||||
{
|
||||
Method = HttpVerb.Patch,
|
||||
BaseAddress = BaseAddress,
|
||||
Endpoint = endpoint,
|
||||
Body = body
|
||||
});
|
||||
|
||||
return await SendData<T>(endpoint, HttpVerb.Patch, body);
|
||||
}
|
||||
|
||||
public async Task<IResponse<T>> PostAsync<T>(Uri endpoint, object body)
|
||||
@@ -140,18 +135,29 @@ namespace Octokit.Http
|
||||
return await SendData<T>(endpoint, HttpMethod.Put, body);
|
||||
}
|
||||
|
||||
async Task<IResponse<T>> SendData<T>(Uri endpoint, HttpMethod method, object body)
|
||||
async Task<IResponse<T>> SendData<T>(
|
||||
Uri endpoint,
|
||||
HttpMethod method,
|
||||
object body,
|
||||
string contentType = "application/x-www-form-urlencoded" // Per: http://developer.github.com/v3/
|
||||
)
|
||||
{
|
||||
Ensure.ArgumentNotNull(endpoint, "endpoint");
|
||||
Ensure.ArgumentNotNull(body, "body");
|
||||
|
||||
return await Run<T>(new Request
|
||||
|
||||
var request = new Request
|
||||
{
|
||||
Method = method,
|
||||
BaseAddress = BaseAddress,
|
||||
Endpoint = endpoint,
|
||||
Body = body
|
||||
});
|
||||
};
|
||||
|
||||
if (body != null)
|
||||
{
|
||||
request.Body = body;
|
||||
request.ContentType = contentType;
|
||||
}
|
||||
|
||||
return await Run<T>(request);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<T>(Uri endpoint)
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace Octokit.Http
|
||||
var body = request.Body as string;
|
||||
if (body != null)
|
||||
{
|
||||
requestMessage.Content = new StringContent(body, Encoding.UTF8);
|
||||
requestMessage.Content = new StringContent(body, Encoding.UTF8, request.ContentType);
|
||||
}
|
||||
var bodyStream = request.Body as System.IO.Stream;
|
||||
if (bodyStream != null)
|
||||
|
||||
@@ -12,5 +12,6 @@ namespace Octokit.Http
|
||||
Dictionary<string, string> Parameters { get; }
|
||||
Uri BaseAddress { get; }
|
||||
Uri Endpoint { get; }
|
||||
string ContentType { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,6 @@ namespace Octokit.Http
|
||||
public Dictionary<string, string> Parameters { get; private set; }
|
||||
public Uri BaseAddress { get; set; }
|
||||
public Uri Endpoint { get; set; }
|
||||
public string ContentType { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user