Skip to content

Commit

Permalink
Merge pull request #856 from davidalpert/add-membership-throws
Browse files Browse the repository at this point in the history
fix the TeamClient.AddMembership(..) call
  • Loading branch information
shiftkey committed Aug 1, 2015
2 parents 1776ca1 + b4d71c8 commit 747f88d
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Octokit.Tests/Clients/TeamsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ public async Task RequestsTheCorrectUrl()
Args.Object);
}

[Fact]
public async Task AllowsEmptyBody()
{
var connection = Substitute.For<IConnection>();

var apiConnection = new ApiConnection(connection);

var client = new TeamsClient(apiConnection);

await client.AddMembership(1, "user");

connection.Received().Put<Dictionary<string, string>>(
Arg.Is<Uri>(u => u.ToString() == "teams/1/memberships/user"),
Arg.Is<object>(u => u == RequestBody.Empty));
}

[Fact]
public async Task EnsuresNonNullOrEmptyLogin()
{
Expand Down
63 changes: 57 additions & 6 deletions Octokit.Tests/Http/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ public class ThePutMethod
[Fact]
public async Task MakesPutRequestWithData()
{
string data = SimpleJson.SerializeObject(new object());
var body = new object();
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();
httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
Expand All @@ -357,20 +358,46 @@ public async Task MakesPutRequestWithData()
httpClient,
Substitute.For<IJsonSerializer>());

await connection.Put<string>(new Uri("endpoint", UriKind.Relative), new object());
await connection.Put<string>(new Uri("endpoint", UriKind.Relative), body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == data &&
(string)req.Body == expectedBody &&
req.Method == HttpMethod.Put &&
req.ContentType == "application/x-www-form-urlencoded" &&
req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken);
}

[Fact]
public async Task MakesPutRequestWithNoData()
{
var body = RequestBody.Empty;
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();
httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
var connection = new Connection(new ProductHeaderValue("OctokitTests"),
_exampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());

await connection.Put<string>(new Uri("endpoint", UriKind.Relative), body);

Console.WriteLine(expectedBody);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == expectedBody &&
req.Method == HttpMethod.Put &&
req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken);
}

[Fact]
public async Task MakesPutRequestWithDataAndTwoFactor()
{
string data = SimpleJson.SerializeObject(new object());
var body = new object();
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();
httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
Expand All @@ -380,16 +407,40 @@ public async Task MakesPutRequestWithDataAndTwoFactor()
httpClient,
Substitute.For<IJsonSerializer>());

await connection.Put<string>(new Uri("endpoint", UriKind.Relative), new object(), "two-factor");
await connection.Put<string>(new Uri("endpoint", UriKind.Relative), body, "two-factor");

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == data &&
(string)req.Body == expectedBody &&
req.Method == HttpMethod.Put &&
req.Headers["X-GitHub-OTP"] == "two-factor" &&
req.ContentType == "application/x-www-form-urlencoded" &&
req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken);
}

[Fact]
public async Task MakesPutRequestWithNoDataAndTwoFactor()
{
var body = RequestBody.Empty;
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();
httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
var connection = new Connection(new ProductHeaderValue("OctokitTests"),
_exampleUri,
Substitute.For<ICredentialStore>(),
httpClient,
Substitute.For<IJsonSerializer>());

await connection.Put<string>(new Uri("endpoint", UriKind.Relative), body, "two-factor");

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == expectedBody &&
req.Method == HttpMethod.Put &&
req.Headers["X-GitHub-OTP"] == "two-factor" &&
req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken);
}
}

public class ThePostMethod
Expand Down
2 changes: 1 addition & 1 deletion Octokit/Clients/TeamsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public async Task<TeamMembership> AddMembership(int id, string login)

try
{
response = await ApiConnection.Put<Dictionary<string, string>>(endpoint, null);
response = await ApiConnection.Put<Dictionary<string, string>>(endpoint, RequestBody.Empty);
}
catch (NotFoundException)
{
Expand Down
12 changes: 12 additions & 0 deletions Octokit/Http/RequestBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Octokit
{
/// <summary>
/// Container for the static <see cref="Empty"/> method that represents an
/// intentional empty request body to avoid overloading <code>null</code>.
/// </summary>
public static class RequestBody
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2211:NonConstantFieldsShouldNotBeVisible")]
public static object Empty = new object();
}
}
1 change: 1 addition & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
<Compile Include="Http\RequestBody.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
<Compile Include="Http\RequestBody.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Octokit/Octokit-Monotouch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
<Compile Include="Http\RequestBody.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
<Compile Include="Http\RequestBody.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit-netcore45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@
<Compile Include="Models\Response\ResourceRateLimit.cs" />
<Compile Include="Models\Response\MiscellaneousRateLimit.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
<Compile Include="Http\RequestBody.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
Expand Down
1 change: 1 addition & 0 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="Helpers\SerializeNullAttribute.cs" />
<Compile Include="Http\HttpMessageHandlerFactory.cs" />
<Compile Include="Http\ProductHeaderValue.cs" />
<Compile Include="Http\RequestBody.cs" />
<Compile Include="Models\Request\GistFileUpdate.cs" />
<Compile Include="Models\Request\NewMerge.cs" />
<Compile Include="Models\Request\PublicRepositoryRequest.cs" />
Expand Down

0 comments on commit 747f88d

Please sign in to comment.