Ejemplo n.º 1
0
        public async Task When_sending_an_explicit_get_with_timeout()
        {
            var timeout = 3.Seconds();

            using (var handler = new FakeResponseHandler())
                using (var expectedResponse = StringResponseMessage)
                {
                    var endpoint = new Uri("http://example.org/api/33");
                    handler.AddFakeResponse(endpoint, expectedResponse);

                    using (IRestClient client = new RestClient(handler: handler))
                    {
                        var resp1 = await client.GetAsync(endpoint, timeout);

                        resp1.EnsureSuccessStatusCode();
                        resp1.StatusCode.ShouldBe(HttpStatusCode.OK);
                        (await resp1.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage);

                        var resp2 = await client.GetAsync(endpoint.OriginalString, timeout);

                        resp2.EnsureSuccessStatusCode();
                        resp2.StatusCode.ShouldBe(HttpStatusCode.OK);
                        (await resp2.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage);
                    }
                }
        }
        public void When_sending_an_explicit_get_which_times_out()
        {
            var timeout  = 3.Seconds();
            var endpoint = new Uri("http://example.org/api/34");

            using var handler        = new DelayedResponseHandler(10.Seconds());
            using IRestClient client = new RestClient(handler: handler);
            Should.Throw <TaskCanceledException>(
                async() => await client.GetAsync(endpoint, timeout));

            Should.Throw <TaskCanceledException>(
                async() => await client.GetAsync(endpoint.OriginalString, timeout));
        }
        public async Task When_calling_a_fake_handler()
        {
            using var handler = new FakeResponseHandler();
            handler.AddFakeResponse(
                new Uri("http://example.org/test"), new HttpResponseMessage(HttpStatusCode.OK));

            using IRestClient client = new RestClient(handler: handler);
            var response1 = await client.GetAsync("http://example.org/notthere");

            var response2 = await client.GetAsync("http://example.org/test");

            response1.StatusCode.ShouldBe(HttpStatusCode.NotFound);
            response2.StatusCode.ShouldBe(HttpStatusCode.OK);
        }
Ejemplo n.º 4
0
        public async Task When_sending_a_get_request_via_explicit_get_with_string_with_completion_option()
        {
            var endpoint = "http://localhost:16/api/";

            using (IRestClient client = new RestClient())
                using (var server = new SimpleHttpListener(new Uri(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 response = await client.GetAsync(endpoint, HttpCompletionOption.ResponseContentRead);

                    response.EnsureSuccessStatusCode();
                    var respStr = await response.Content.ReadAsStringAsync();

                    respStr.ShouldBe("Hello There!");
                }
        }
        public void When_sending_an_explicit_get_with_string_and_completion_option_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.GetAsync("http://example.org/api/18", HttpCompletionOption.ResponseContentRead, cts.Token));
        }
Ejemplo n.º 6
0
 public void When_sending_an_explicit_get_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.GetAsync("http://example.org/api/14", cts.Token));
         }
 }
Ejemplo n.º 7
0
        public async Task When_sending_a_get_request_via_explicit_get_with_string_with_completion_option_with_cancelation_token()
        {
            var endpoint = "http://localhost:18/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.GetAsync(endpoint, HttpCompletionOption.ResponseContentRead, cts.Token));
                }
        }
Ejemplo n.º 8
0
        public async Task When_sending_a_get_request_via_explicit_get_with_uri_with_cancellation_token()
        {
            var endpoint = new Uri("http://localhost:13/api/");

            using (IRestClient client = new RestClient())
                using (var server = new SimpleHttpListener(endpoint))
                {
                    await server.ListenAsync();

                    var cts = new CancellationTokenSource(1.Seconds());
                    Should.Throw <TaskCanceledException>(async() => await client.GetAsync(endpoint, cts.Token));
                }
        }
        public async Task When_sending_an_explicit_get_with_string_and_completion_option()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/16");

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            var response = await client.GetAsync(endpoint.OriginalString, HttpCompletionOption.ResponseContentRead);

            response.EnsureSuccessStatusCode();
            response.StatusCode.ShouldBe(HttpStatusCode.OK);

            var respStr = await response.Content.ReadAsStringAsync();

            respStr.ShouldBe(ExpectedMessage);
        }
Ejemplo n.º 10
0
        public async Task When_sending_an_explicit_get_with_uri()
        {
            using (var handler = new FakeResponseHandler())
                using (var expectedResponse = StringResponseMessage)
                {
                    var endpoint = new Uri("http://example.org/api/11");
                    handler.AddFakeResponse(endpoint, expectedResponse);

                    using (IRestClient client = new RestClient(handler: handler))
                    {
                        var response = await client.GetAsync(endpoint);

                        response.EnsureSuccessStatusCode();
                        response.StatusCode.ShouldBe(HttpStatusCode.OK);

                        var respStr = await response.Content.ReadAsStringAsync();

                        respStr.ShouldBe(ExpectedMessage);
                    }
                }
        }