public void When_sending_a_put_which_times_out() { var timeout = 3.Seconds(); var endpoint = new Uri("http://example.org/api/44"); using var handler = new DelayedResponseHandler(10.Seconds()); using IRestClient client = new RestClient(handler: handler); Should.Throw <TaskCanceledException>( async() => await client.PutAsync(endpoint, new MultipartFormDataContent(), timeout)); Should.Throw <TaskCanceledException>( async() => await client.PutAsync(endpoint.OriginalString, new MultipartFormDataContent(), timeout)); }
public async Task When_sending_a_put_request_via_explicit_put_with_string() { var endpoint = "http://localhost:3/api/"; using (IRestClient client = new RestClient()) using (var server = new SimpleHttpListener(new Uri(endpoint))) { server.OnRequest += (sender, context) => { if (context.Request.HttpMethod != HttpMethod.Put.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 response = await client.PutAsync(endpoint, new MultipartFormDataContent()); response.EnsureSuccessStatusCode(); var respStr = await response.Content.ReadAsStringAsync(); respStr.ShouldBe("Hello There!"); } }
public void When_sending_an_explicit_put_with_string_and_cancellation() { using var handler = new DelayedResponseHandler(5.Seconds()); using IRestClient client = new RestClient(handler: handler); var cts = new CancellationTokenSource(1.Seconds()); Should.Throw <TaskCanceledException>( async() => await client.PutAsync( "http://example.org/api/5", new MultipartFormDataContent(), cts.Token)); }
public async Task When_sending_a_put_with_timeout() { var timeout = 3.Seconds(); using var handler = new FakeResponseHandler(); using var expectedResponse = StringResponseMessage; var endpoint = new Uri("http://example.org/api/43"); handler.AddFakeResponse(endpoint, expectedResponse); using IRestClient client = new RestClient(handler: handler); var resp1 = await client.PutAsync(endpoint, new MultipartFormDataContent(), timeout); resp1.EnsureSuccessStatusCode(); (await resp1.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage); var resp2 = await client.PutAsync(endpoint.OriginalString, new MultipartFormDataContent(), timeout); resp2.EnsureSuccessStatusCode(); (await resp2.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage); }
public async Task When_sending_a_put_request_via_explicit_put_with_string_with_cancellation() { var endpoint = "http://localhost:5/api/"; using (IRestClient client = new RestClient()) using (var server = new SimpleHttpListener(new Uri(endpoint))) { await server.ListenAsync(); var cts = new CancellationTokenSource(1.Seconds()); Should.Throw <TaskCanceledException>(async() => await client.PutAsync(endpoint, new MultipartFormDataContent(), cts.Token)); } }
public async Task When_sending_an_explicit_put_with_uri() { using var handler = new FakeResponseHandler(); using var expectedResponse = StreamResponseMessage; var endpoint = new Uri("http://example.org/api/2"); handler.AddFakeResponse(endpoint, expectedResponse); using IRestClient client = new RestClient(handler: handler); var response = await client.PutAsync(endpoint, new MultipartFormDataContent()); response.EnsureSuccessStatusCode(); response.StatusCode.ShouldBe(HttpStatusCode.OK); var respStr = await response.Content.ReadAsStringAsync(); respStr.ShouldBe(ExpectedMessage); }