public async Task InvalidExceptionWithBadStatusCode() { var uri = new Uri("https://test.com/test"); foreach (HttpStatusCode statusCode in Enum.GetValues(typeof(HttpStatusCode)).Cast <HttpStatusCode>()) { var responseMessage = new HttpResponseMessage(statusCode); responseMessage.Content = new StringContent("Bad"); var fakeHandler = new FakeResponseHandler(); fakeHandler.AddFakeResponse(uri, responseMessage); var factory = new Mock <IHttpClientFactory>(); factory .Setup(f => f.CreateClient(Options.DefaultName)) .Returns(new HttpClient(fakeHandler)); var client = new HttpRpcTransportClient(httpClientFactory: factory.Object); Func <Task> func = () => client.SendRequestAsync(uri, "{}"); if (!responseMessage.IsSuccessStatusCode) { await Assert.ThrowsAsync <RpcClientInvalidStatusCodeException>(func); } else { await func(); } } }
public async Task InvalidExceptionWithBadStatusCode() { var uri = new Uri("https://test.com/test"); foreach (HttpStatusCode statusCode in Enum.GetValues(typeof(HttpStatusCode)).Cast <HttpStatusCode>()) { var responseMessage = new HttpResponseMessage(statusCode); responseMessage.Content = new StringContent("Bad"); var fakeHandler = new FakeResponseHandler(); fakeHandler.AddFakeResponse(uri, responseMessage); var factory = new Mock <IHttpClientFactory>(MockBehavior.Strict); factory .Setup(f => f.CreateClient(Options.DefaultName)) .Returns(new HttpClient(fakeHandler)); var streamCompressor = new Mock <IStreamCompressor>(MockBehavior.Strict); System.IO.Stream stream; streamCompressor .Setup(c => c.TryGetCompressionStream(It.IsAny <System.IO.Stream>(), It.IsAny <string>(), It.IsAny <System.IO.Compression.CompressionMode>(), out stream)) .Returns(false); var client = new HttpRpcTransportClient(streamCompressor.Object, httpClientFactory: factory.Object); Func <Task> func = () => client.SendRequestAsync(uri, "{}"); if (!responseMessage.IsSuccessStatusCode) { await Assert.ThrowsAsync <RpcClientInvalidStatusCodeException>(func); } else { await func(); } } }
//https://github.com/edjCase/JsonRpc static void Main(string[] args) { var url = "http://localhost:59385/Items/"; IRpcTransportClient transportClient = new HttpRpcTransportClient(); RpcClient client = new RpcClient(new Uri(url), transportClient: transportClient); RpcRequest request = RpcRequest.WithParameterList("Get", null, "Id1"); RpcResponse <Item> response = client.SendRequestAsync <Item>(request).GetAwaiter().GetResult(); Console.WriteLine($"response.Result.Id: {response.Result.Id} | response.Result.Name: {response.Result.Name}"); }
private static async Task Test4() { IRpcTransportClient transportClient = new HttpRpcTransportClient(() => Task.FromResult(IntegrationTestRunner.authHeaderValue)); RpcClient client = new RpcClient(new Uri(url), transportClient: transportClient); RpcRequest request = RpcRequest.WithParameterList("CharacterCount", new[] { "Test" }, "Id1"); RpcResponse <TestObject> response = await client.SendRequestAsync <TestObject>(request); if (response.Result.Test != 4) { throw new Exception("Test 1 failed."); } }
private static async Task Test1() { // var options = new WebSocketRpcTransportClientOptions(); // string url = "ws://localhost:5000/WebSocket"; //IRpcTransportClient transportClient = new WebSocketRpcTransportClient(Options.Create(options)); IRpcTransportClient transportClient = new HttpRpcTransportClient(() => Task.FromResult(IntegrationTestRunner.authHeaderValue)); RpcClient client = new RpcClient(new Uri(url), transportClient: transportClient); RpcRequest request = RpcRequest.WithParameterList("CharacterCount", new[] { "Test" }, "Id1"); RpcResponse <TestObject> response = await client.SendRequestAsync <TestObject>(request); if (response.Result.Test != 4) { throw new Exception("Test 1 failed."); } }
private static async Task Test2() { IRpcTransportClient transportClient = new HttpRpcTransportClient(() => Task.FromResult(IntegrationTestRunner.authHeaderValue)); RpcClient client = new RpcClient(new Uri(url), transportClient: transportClient); List <RpcRequest> requests = new List <RpcRequest> { RpcRequest.WithParameterList("CharacterCount", new[] { "Test" }, "Id1"), RpcRequest.WithParameterList("CharacterCount", new[] { "Test2" }, "Id2"), RpcRequest.WithParameterList("CharacterCount", new[] { "Test23" }, "Id3") }; List <RpcResponse <TestObject> > bulkResponse = await client.SendBulkRequestAsync <TestObject>(requests); foreach (RpcResponse <TestObject> r in bulkResponse) { switch (r.Id.StringValue) { case "Id1": if (r.Result.Test != 4) { throw new Exception("Test 2.1 failed."); } break; case "Id2": if (r.Result.Test != 5) { throw new Exception("Test 2.2 failed."); } break; case "Id3": if (r.Result.Test != 6) { throw new Exception("Test 2.3 failed."); } break; default: throw new ArgumentOutOfRangeException(nameof(r.Id)); } } }