Skip to content

Commit

Permalink
Merge pull request octokit#1133 from devkhan/json-serializer-fix
Browse files Browse the repository at this point in the history
[RFC] [WIP] IJsonSerializer parameter in Connection.cs wasn't being used
  • Loading branch information
ryangribble committed Mar 9, 2016
2 parents 73d2e8f + 534d54b commit a7935da
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
67 changes: 54 additions & 13 deletions Octokit.Tests/Http/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,30 @@ public class ThePatchMethod
[Fact]
public async Task RunsConfiguredAppWithAppropriateEnv()
{
string data = SimpleJson.SerializeObject(new object());
var httpClient = Substitute.For<IHttpClient>();
var body = new object();
var expectedData = SimpleJson.SerializeObject(body);

var serializer = Substitute.For<IJsonSerializer>();
serializer.Serialize(body).Returns(expectedData);

IResponse response = new Response();
httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response));
var httpClient = Substitute.For<IHttpClient>();
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>());
serializer);

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

serializer.Received(1).Serialize(body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == data &&
(string)req.Body == expectedData &&
req.Method == HttpVerb.Patch &&
req.ContentType == "application/x-www-form-urlencoded" &&
req.Endpoint == new Uri("endpoint", UriKind.Relative)), Args.CancellationToken);
Expand Down Expand Up @@ -350,18 +359,24 @@ public class ThePutMethod
public async Task MakesPutRequestWithData()
{
var body = new object();
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();

serializer.Serialize(body).Returns(expectedBody);

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>());
serializer);

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

serializer.Received(1).Serialize(body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == expectedBody &&
Expand All @@ -374,18 +389,25 @@ public async Task MakesPutRequestWithData()
public async Task MakesPutRequestWithNoData()
{
var body = RequestBody.Empty;
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();

serializer.Serialize(body).Returns(expectedBody);

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>());
serializer);

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

serializer.Received(1).Serialize(body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == expectedBody &&
Expand All @@ -397,18 +419,24 @@ public async Task MakesPutRequestWithNoData()
public async Task MakesPutRequestWithDataAndTwoFactor()
{
var body = new object();
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();

serializer.Serialize(body).Returns(expectedBody);

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>());
serializer);

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

serializer.Received(1).Serialize(body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == expectedBody &&
Expand All @@ -422,18 +450,24 @@ public async Task MakesPutRequestWithDataAndTwoFactor()
public async Task MakesPutRequestWithNoDataAndTwoFactor()
{
var body = RequestBody.Empty;
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();

serializer.Serialize(body).Returns(expectedBody);

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>());
serializer);

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

serializer.Received(1).Serialize(body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
(string)req.Body == expectedBody &&
Expand All @@ -448,17 +482,24 @@ public class ThePostMethod
[Fact]
public async Task SendsProperlyFormattedPostRequest()
{
string data = SimpleJson.SerializeObject(new object());
var body = new object();
var serializer = Substitute.For<IJsonSerializer>();
var data = SimpleJson.SerializeObject(body);
var httpClient = Substitute.For<IHttpClient>();
IResponse response = new Response();

serializer.Serialize(body).Returns(data);

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>());
serializer);

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

await connection.Post<string>(new Uri("endpoint", UriKind.Relative), new object(), null, null);
serializer.Received(1).Serialize(body);

httpClient.Received(1).Send(Arg.Is<IRequest>(req =>
req.BaseAddress == _exampleUri &&
Expand Down
2 changes: 1 addition & 1 deletion Octokit/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Connection(
BaseAddress = baseAddress;
_authenticator = new Authenticator(credentialStore);
_httpClient = httpClient;
_jsonPipeline = new JsonHttpPipeline();
_jsonPipeline = new JsonHttpPipeline(serializer);
}

/// <summary>
Expand Down

0 comments on commit a7935da

Please sign in to comment.