diff --git a/src/tests/Tochka.JsonRpc.Client.Tests/JsonRpcClientBaseTests.cs b/src/tests/Tochka.JsonRpc.Client.Tests/JsonRpcClientBaseTests.cs index 4eedf05..dc6192f 100644 --- a/src/tests/Tochka.JsonRpc.Client.Tests/JsonRpcClientBaseTests.cs +++ b/src/tests/Tochka.JsonRpc.Client.Tests/JsonRpcClientBaseTests.cs @@ -195,6 +195,177 @@ public async Task SendRequest6_ChainsToInternalMethod() clientMock.Verify(); } + [Test] + public async Task SendRequestWithoutParams1_ChainsToInternalMethod() + { + var request = new Request(new NullRpcId(), Method, null); + clientMock.Setup(x => x.SendRequestInternal(RequestUrl, request, It.IsAny())) + .ReturnsAsync(Mock.Of()) + .Verifiable(); + + await clientMock.Object.SendRequest(RequestUrl, request, new CancellationToken()); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParams2_ChainsToInternalMethod() + { + var request = new Request(new NullRpcId(), Method, null); + clientMock.Setup(x => x.SendRequestInternal(null, request, It.IsAny())) + .ReturnsAsync(Mock.Of()) + .Verifiable(); + + await clientMock.Object.SendRequest(request, new CancellationToken()); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParams3_ChainsToInternalMethod() + { + var id = Mock.Of(); + generatorMock.Setup(static g => g.GenerateId()) + .Returns(id) + .Verifiable(); + clientMock.Setup(x => x.SendRequestInternal(RequestUrl, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of()) + .Verifiable(); + + await clientMock.Object.SendRequest(RequestUrl, Method, new CancellationToken()); + + generatorMock.Verify(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParams4_ChainsToInternalMethod() + { + var id = Mock.Of(); + generatorMock.Setup(static g => g.GenerateId()) + .Returns(id) + .Verifiable(); + clientMock.Setup(x => x.SendRequestInternal(null, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of()) + .Verifiable(); + + await clientMock.Object.SendRequest(Method, new CancellationToken()); + + generatorMock.Verify(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParams5_ChainsToInternalMethod() + { + var id = Mock.Of(); + clientMock.Setup(x => x.SendRequestInternal(RequestUrl, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of()) + .Verifiable(); + + await clientMock.Object.SendRequest(RequestUrl, id, Method, new CancellationToken()); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParams6_ChainsToInternalMethod() + { + var id = Mock.Of(); + clientMock.Setup(x => x.SendRequestInternal(null, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of()) + .Verifiable(); + + await clientMock.Object.SendRequest(id, Method, new CancellationToken()); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParamsWithResponse1_ChainsToInternalMethod() + { + var request = new Request(new NullRpcId(), Method); + clientMock.Setup(x => x.SendRequestInternal(null, request, It.IsAny())) + .ReturnsAsync(Mock.Of>()) + .Verifiable(); + + await clientMock.Object.SendRequest(request, CancellationToken.None); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParamsWithResponse2_ChainsToInternalMethod() + { + var request = new Request(new NullRpcId(), Method); + clientMock.Setup(x => x.SendRequestInternal(PostUrl, request, It.IsAny())) + .ReturnsAsync(Mock.Of>()) + .Verifiable(); + + await clientMock.Object.SendRequest(PostUrl, request, CancellationToken.None); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParamsWithResponse3_ChainsToInternalMethod() + { + var id = Mock.Of(); + generatorMock.Setup(static g => g.GenerateId()) + .Returns(id) + .Verifiable(); + clientMock.Setup(x => x.SendRequestInternal(RequestUrl, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of>()) + .Verifiable(); + await clientMock.Object.SendRequest(RequestUrl, Method, new CancellationToken()); + + generatorMock.Verify(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParamsWithResponse4_ChainsToInternalMethod() + { + var id = Mock.Of(); + generatorMock.Setup(static g => g.GenerateId()) + .Returns(id) + .Verifiable(); + clientMock.Setup(x => x.SendRequestInternal(null, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of>()) + .Verifiable(); + + await clientMock.Object.SendRequest(Method, new CancellationToken()); + + generatorMock.Verify(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParamsWithResponse5_ChainsToInternalMethod() + { + var id = Mock.Of(); + clientMock.Setup(x => x.SendRequestInternal(RequestUrl, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of>()) + .Verifiable(); + + await clientMock.Object.SendRequest(RequestUrl, id, Method, new CancellationToken()); + + clientMock.Verify(); + } + + [Test] + public async Task SendRequestWithoutParamsWithResponse6_ChainsToInternalMethod() + { + var id = Mock.Of(); + clientMock.Setup(x => x.SendRequestInternal(null, It.Is(y => y.Method == Method && y.Id == id), It.IsAny())) + .ReturnsAsync(Mock.Of>()) + .Verifiable(); + + await clientMock.Object.SendRequest(id, Method, new CancellationToken()); + + clientMock.Verify(); + } + [Test] public async Task SendBatch1_ChainsToInternalMethod() { @@ -375,6 +546,148 @@ public async Task SendRequestInternal_ThrowOnBatchResponse() clientMock.Verify(); } + [Test] + public async Task SendRequestInternalWithoutParams_PostContentToRequestUrlAndParseResponse() + { + var request = new Request(new NullRpcId(), Method); + handlerMock.Expect(HttpMethod.Post, PostUrl) + .Respond(HttpStatusCode.OK); + clientMock.Setup(static c => c.CreateContext()) + .Returns(Mock.Of()); + clientMock.Setup(static c => c.GetContent(It.IsAny(), It.IsAny())) + .ReturnsAsync(ResponseContent) + .Verifiable(); + clientMock.Setup(static c => c.ParseBody(ResponseContent)) + .Returns(new SingleResponseWrapper(Mock.Of())) + .Verifiable(); + + await clientMock.Object.SendRequestInternal(RequestUrl, request, CancellationToken.None); + + handlerMock.VerifyNoOutstandingRequest(); + handlerMock.VerifyNoOutstandingExpectation(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestInternalWithoutParams_FillContext() + { + var request = new Request(new NullRpcId(), Method); + var contextMock = new Mock(); + var response = new HttpResponseMessage(); + var singleResponse = Mock.Of(); + clientMock.Setup(static c => c.CreateContext()) + .Returns(contextMock.Object); + handlerMock.When(PostUrl) + .Respond(() => Task.FromResult(response)); + clientMock.Setup(static c => c.GetContent(It.IsAny(), It.IsAny())) + .ReturnsAsync(ResponseContent) + .Verifiable(); + clientMock.Setup(static c => c.ParseBody(ResponseContent)) + .Returns(new SingleResponseWrapper(singleResponse)) + .Verifiable(); + + await clientMock.Object.SendRequestInternal(RequestUrl, request, CancellationToken.None); + + contextMock.Verify(static c => c.WithRequestUrl(RequestUrl)); + contextMock.Verify(static c => c.WithSingle(It.IsAny())); + contextMock.Verify(c => c.WithHttpResponse(response)); + contextMock.Verify(c => c.WithHttpContent(response.Content, ResponseContent)); + contextMock.Verify(c => c.WithSingleResponse(singleResponse)); + } + + [Test] + public async Task SendRequestInternalWithoutParams_ThrowOnBatchResponse() + { + var request = new Request(new NullRpcId(), Method); + var contextMock = new Mock(); + clientMock.Setup(static c => c.CreateContext()) + .Returns(contextMock.Object); + handlerMock.Expect(HttpMethod.Post, PostUrl) + .Respond(HttpStatusCode.OK); + clientMock.Setup(static c => c.GetContent(It.IsAny(), It.IsAny())) + .ReturnsAsync(ResponseContent) + .Verifiable(); + clientMock.Setup(static c => c.ParseBody(ResponseContent)) + .Returns(new BatchResponseWrapper(new List())) + .Verifiable(); + + var act = async () => await clientMock.Object.SendRequestInternal(RequestUrl, request, CancellationToken.None); + + await act.Should().ThrowAsync(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestInternalWithoutParamsWithResponse_PostContentToRequestUrlAndParseResponse() + { + var request = new Request(new NullRpcId(), Method); + handlerMock.Expect(HttpMethod.Post, PostUrl) + .Respond(HttpStatusCode.OK); + clientMock.Setup(static c => c.CreateContext()) + .Returns(Mock.Of()); + clientMock.Setup(static c => c.GetContent(It.IsAny(), It.IsAny())) + .ReturnsAsync(ResponseContent) + .Verifiable(); + clientMock.Setup(static c => c.ParseBody(ResponseContent)) + .Returns(new SingleResponseWrapper(Mock.Of())) + .Verifiable(); + + await clientMock.Object.SendRequestInternal(RequestUrl, request, CancellationToken.None); + + handlerMock.VerifyNoOutstandingRequest(); + handlerMock.VerifyNoOutstandingExpectation(); + clientMock.Verify(); + } + + [Test] + public async Task SendRequestInternalWithoutParamsWithResponse_FillContext() + { + var request = new Request(new NullRpcId(), Method); + var contextMock = new Mock(); + var response = new HttpResponseMessage(); + var singleResponse = Mock.Of(); + clientMock.Setup(static c => c.CreateContext()) + .Returns(contextMock.Object); + handlerMock.When(PostUrl) + .Respond(() => Task.FromResult(response)); + clientMock.Setup(static c => c.GetContent(It.IsAny(), It.IsAny())) + .ReturnsAsync(ResponseContent) + .Verifiable(); + clientMock.Setup(static c => c.ParseBody(ResponseContent)) + .Returns(new SingleResponseWrapper(singleResponse)) + .Verifiable(); + + await clientMock.Object.SendRequestInternal(RequestUrl, request, CancellationToken.None); + + contextMock.Verify(static c => c.WithRequestUrl(RequestUrl)); + contextMock.Verify(static c => c.WithSingle(It.IsAny())); + contextMock.Verify(c => c.WithHttpResponse(response)); + contextMock.Verify(c => c.WithHttpContent(response.Content, ResponseContent)); + contextMock.Verify(c => c.WithSingleResponse(singleResponse)); + } + + [Test] + public async Task SendRequestInternalWithoutParamsWithResponse_ThrowOnBatchResponse() + { + var request = new Request(new NullRpcId(), Method); + var contextMock = new Mock(); + clientMock.Setup(static c => c.CreateContext()) + .Returns(contextMock.Object); + handlerMock.Expect(HttpMethod.Post, PostUrl) + .Respond(HttpStatusCode.OK); + clientMock.Setup(static c => c.GetContent(It.IsAny(), It.IsAny())) + .ReturnsAsync(ResponseContent) + .Verifiable(); + clientMock.Setup(static c => c.ParseBody(ResponseContent)) + .Returns(new BatchResponseWrapper(new List())) + .Verifiable(); + + var act = async () => await clientMock.Object.SendRequestInternal(RequestUrl, request, CancellationToken.None); + + await act.Should().ThrowAsync(); + clientMock.Verify(); + } + [Test] public async Task SendBatchInternal_BatchWithoutRequests_PostContentToRequestUrl() {