public async Task When_sending_a_get_request() { var endpoint = new Uri("http://localhost:1/api/"); using (IRestClient client = new RestClient()) using (var server = new SimpleHttpListener(endpoint)) { server.OnRequest += (sender, context) => { if (context.Request.HttpMethod != HttpMethod.Get.ToString()) { context.Response.StatusCode = (int)HttpStatusCode.NotAcceptable; context.Response.Close(); } else { var reply = Encoding.UTF8.GetBytes("Hello There!"); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.OutputStream.Write(reply, 0, reply.Length); context.Response.Close(); } }; await server.ListenAsync(); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = endpoint }; request.Headers.Add("Foo", "Bar"); var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead); response.EnsureSuccessStatusCode(); var respStr = await response.Content.ReadAsStringAsync(); respStr.ShouldBe("Hello There!"); } }
public void When_sending_a_request_with_string_uri() { var endpoint = "http://localhost:33/api"; var endpointUri = new Uri(endpoint); ServicePointManager.FindServicePoint(endpointUri).ConnectionLeaseTimeout.ShouldBe(-1); using (IRestClient client = new RestClient()) { client.SendAsync(new HttpRequestMessage(HttpMethod.Get, endpoint)); ServicePointManager.FindServicePoint(endpointUri) .ConnectionLeaseTimeout .ShouldBe((int)1.Minutes().TotalMilliseconds); client.Endpoints.Length.ShouldBe(1); client.Endpoints[0].OriginalString.ShouldBe(endpoint); client.ClearEndpoints(); client.Endpoints.ShouldBeEmpty(); } }
public async Task When_sending_a_request_with_cancellation_and_completion_option() { var endpoint = new Uri("http://localhost:24/api/"); using (IRestClient client = new RestClient()) using (var server = new SimpleHttpListener(endpoint)) { await server.ListenAsync(); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = endpoint }; request.Headers.Add("Foo", "Bar"); var cts = new CancellationTokenSource(1.Seconds()); Should.Throw <TaskCanceledException>( async() => await client.SendAsync( request, HttpCompletionOption.ResponseContentRead, cts.Token)); } }
public async Task When_sending_a_delete_request() { using var handler = new FakeResponseHandler(); using var expectedResponse = StringResponseMessage; var endpoint = new Uri("http://example.org/api/19"); handler.AddFakeResponse(endpoint, expectedResponse); using IRestClient client = new RestClient(handler: handler); var request = new HttpRequestMessage { Method = HttpMethod.Delete, RequestUri = endpoint }; var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead); response.EnsureSuccessStatusCode(); response.StatusCode.ShouldBe(HttpStatusCode.OK); var respStr = await response.Content.ReadAsStringAsync(); respStr.ShouldBe(ExpectedMessage); }
public async Task When_sending_a_request_then_cancelling_all_pending_requests() { var endpoint = new Uri("http://localhost:26/api/"); using (IRestClient client = new RestClient()) using (var server = new SimpleHttpListener(endpoint)) { await server.ListenAsync(); var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = endpoint }; request.Headers.Add("Foo", "Bar"); var copy = client; #pragma warning disable 4014 Task.Delay(1.Seconds()).ContinueWith(_ => copy.CancelPendingRequests()); #pragma warning restore 4014 Should.Throw <TaskCanceledException>(async() => await client.SendAsync(request)); } }