diff --git a/Octokit.Tests/Http/ConnectionTests.cs b/Octokit.Tests/Http/ConnectionTests.cs index 23582e5cc8..dcf12016d7 100644 --- a/Octokit.Tests/Http/ConnectionTests.cs +++ b/Octokit.Tests/Http/ConnectionTests.cs @@ -306,21 +306,30 @@ public class ThePatchMethod [Fact] public async Task RunsConfiguredAppWithAppropriateEnv() { - string data = SimpleJson.SerializeObject(new object()); - var httpClient = Substitute.For(); + var body = new object(); + var expectedData = SimpleJson.SerializeObject(body); + + var serializer = Substitute.For(); + serializer.Serialize(body).Returns(expectedData); + IResponse response = new Response(); - httpClient.Send(Args.Request, Args.CancellationToken).Returns(Task.FromResult(response)); + var httpClient = Substitute.For(); + httpClient.Send(Args.Request, Args.CancellationToken) + .Returns(Task.FromResult(response)); + var connection = new Connection(new ProductHeaderValue("OctokitTests"), _exampleUri, Substitute.For(), httpClient, - Substitute.For()); + serializer); - await connection.Patch(new Uri("endpoint", UriKind.Relative), new object()); + await connection.Patch(new Uri("endpoint", UriKind.Relative), body); + + serializer.Received(1).Serialize(body); httpClient.Received(1).Send(Arg.Is(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); @@ -350,18 +359,24 @@ public class ThePutMethod public async Task MakesPutRequestWithData() { var body = new object(); + var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); 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(), httpClient, - Substitute.For()); + serializer); await connection.Put(new Uri("endpoint", UriKind.Relative), body); + serializer.Received(1).Serialize(body); + httpClient.Received(1).Send(Arg.Is(req => req.BaseAddress == _exampleUri && (string)req.Body == expectedBody && @@ -374,18 +389,25 @@ public async Task MakesPutRequestWithData() public async Task MakesPutRequestWithNoData() { var body = RequestBody.Empty; + var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); 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(), httpClient, - Substitute.For()); + serializer); await connection.Put(new Uri("endpoint", UriKind.Relative), body); + serializer.Received(1).Serialize(body); + httpClient.Received(1).Send(Arg.Is(req => req.BaseAddress == _exampleUri && (string)req.Body == expectedBody && @@ -397,18 +419,24 @@ public async Task MakesPutRequestWithNoData() public async Task MakesPutRequestWithDataAndTwoFactor() { var body = new object(); + var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); 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(), httpClient, - Substitute.For()); + serializer); await connection.Put(new Uri("endpoint", UriKind.Relative), body, "two-factor"); + serializer.Received(1).Serialize(body); + httpClient.Received(1).Send(Arg.Is(req => req.BaseAddress == _exampleUri && (string)req.Body == expectedBody && @@ -422,18 +450,24 @@ public async Task MakesPutRequestWithDataAndTwoFactor() public async Task MakesPutRequestWithNoDataAndTwoFactor() { var body = RequestBody.Empty; + var serializer = Substitute.For(); var expectedBody = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); 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(), httpClient, - Substitute.For()); + serializer); await connection.Put(new Uri("endpoint", UriKind.Relative), body, "two-factor"); + serializer.Received(1).Serialize(body); + httpClient.Received(1).Send(Arg.Is(req => req.BaseAddress == _exampleUri && (string)req.Body == expectedBody && @@ -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(); + var data = SimpleJson.SerializeObject(body); var httpClient = Substitute.For(); 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(), httpClient, - Substitute.For()); + serializer); + + await connection.Post(new Uri("endpoint", UriKind.Relative), body, null, null); - await connection.Post(new Uri("endpoint", UriKind.Relative), new object(), null, null); + serializer.Received(1).Serialize(body); httpClient.Received(1).Send(Arg.Is(req => req.BaseAddress == _exampleUri && diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index dabee2710b..ba1035d728 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -133,7 +133,7 @@ public Connection( BaseAddress = baseAddress; _authenticator = new Authenticator(credentialStore); _httpClient = httpClient; - _jsonPipeline = new JsonHttpPipeline(); + _jsonPipeline = new JsonHttpPipeline(serializer); } ///