public void When_sending_a_get_string_which_times_out()
        {
            var timeout  = 3.Seconds();
            var endpoint = new Uri("http://example.org/api/35");

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

            Should.Throw <TaskCanceledException>(
                async() => await client.GetStringAsync(endpoint.OriginalString, timeout));
        }
        public async Task When_sending_a_get_string_with_timeout()
        {
            var timeout = 3.Seconds();

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

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            (await client.GetStringAsync(endpoint, timeout)).ShouldBe(ExpectedMessage);
            (await client.GetStringAsync(endpoint.OriginalString, timeout)).ShouldBe(ExpectedMessage);
        }
Пример #3
0
        public async Task When_sending_a_get_string_request_with_string()
        {
            var endpoint = "http://localhost:27/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.GetStringAsync(endpoint);

                    response.ShouldBe("Hello There!");
                }
        }
Пример #4
0
        public async Task When_sending_a_request_with_base_address()
        {
            var endpoint1 = new Uri("http://foo.bar/api/1");
            var resp1     = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("A")
            };

            var endpoint2 = new Uri("http://foo.bar/api/2");
            var resp2     = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("B")
            };

            var endpoint3 = new Uri("http://foo.bar/api/3");
            var resp3     = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("C")
            };

            using (var handler = new FakeResponseHandler())
                using (resp1)
                    using (resp2)
                        using (resp3)
                        {
                            handler.AddFakeResponse(endpoint1, resp1);
                            handler.AddFakeResponse(endpoint2, resp2);
                            handler.AddFakeResponse(endpoint3, resp3);

                            var baseAddress = new Uri("http://foo.bar");
                            using (IRestClient client = new RestClient(handler: handler, baseAddress: baseAddress))
                            {
                                client.BaseAddress.ShouldBe(baseAddress);

                                (await client.GetStringAsync(endpoint1)).ShouldBe("A");
                                (await client.GetStringAsync(endpoint2)).ShouldBe("B");
                                (await client.GetStringAsync(endpoint3)).ShouldBe("C");

                                (await client.GetStringAsync(endpoint1.OriginalString)).ShouldBe("A");
                                (await client.GetStringAsync(endpoint2.OriginalString)).ShouldBe("B");
                                (await client.GetStringAsync(endpoint3.OriginalString)).ShouldBe("C");

                                (await client.GetStringAsync("api/1")).ShouldBe("A");
                                (await client.GetStringAsync("api/2")).ShouldBe("B");
                                (await client.GetStringAsync("api/3")).ShouldBe("C");
                            }
                        }
        }
        public async Task When_sending_a_get_string_with_uri()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/27");

            handler.AddFakeResponse(endpoint, expectedResponse);

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

            response.ShouldBe(ExpectedMessage);
        }