Skip to content

Commit

Permalink
Verify Serialize is getting called with correct params.
Browse files Browse the repository at this point in the history
  • Loading branch information
devkhan committed Mar 9, 2016
1 parent 6e49068 commit 534d54b
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions Octokit.Tests/Http/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,22 +306,30 @@ public class ThePatchMethod
[Fact]
public async Task RunsConfiguredAppWithAppropriateEnv()
{
var body = new object();
var expectedData = SimpleJson.SerializeObject(body);

var serializer = Substitute.For<IJsonSerializer>();
string data = serializer.Serialize(new object());
var httpClient = Substitute.For<IHttpClient>();
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 @@ -352,18 +360,23 @@ public async Task MakesPutRequestWithData()
{
var body = new object();
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = serializer.Serialize(body);
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 @@ -377,18 +390,24 @@ public async Task MakesPutRequestWithNoData()
{
var body = RequestBody.Empty;
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = serializer.Serialize(body);
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 @@ -401,18 +420,23 @@ public async Task MakesPutRequestWithDataAndTwoFactor()
{
var body = new object();
var serializer = Substitute.For<IJsonSerializer>();
var expectedBody = serializer.Serialize(body);
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 @@ -427,18 +451,23 @@ public async Task MakesPutRequestWithNoDataAndTwoFactor()
{
var body = RequestBody.Empty;
var serializer = Substitute.For<IJsonSerializer>();
string expectedBody = serializer.Serialize(body);
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 @@ -453,18 +482,24 @@ public class ThePostMethod
[Fact]
public async Task SendsProperlyFormattedPostRequest()
{
var body = new object();
var serializer = Substitute.For<IJsonSerializer>();
string data = serializer.Serialize(new object());
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

0 comments on commit 534d54b

Please sign in to comment.