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

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

            Should.Throw <TaskCanceledException>(
                async() => await client.GetStreamAsync(endpoint.OriginalString, timeout));
        }
Esempio n. 2
0
        public async Task When_sending_a_get_stream_request_with_string()
        {
            var endpoint = "http://localhost:29/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.GetStreamAsync(endpoint);

                    using (var streamReader = new StreamReader(response, Encoding.UTF8))
                    {
                        var responseString = await streamReader.ReadToEndAsync();

                        responseString.ShouldBe("Hello There!");
                    }
                }
        }
        public async Task When_sending_a_get_stream_with_timeout_string()
        {
            var timeout = 3.Seconds();

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

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            var resp = await client.GetStreamAsync(endpoint.OriginalString, timeout);

            using var streamReader = new StreamReader(resp, Encoding.UTF8);
            (await streamReader.ReadToEndAsync()).ShouldBe(ExpectedMessage);
        }
        public async Task When_sending_a_get_stream_with_uri()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StreamResponseMessage;
            var endpoint = new Uri("http://example.org/api/29");

            handler.AddFakeResponse(endpoint, expectedResponse);

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

            using var streamReader = new StreamReader(response, Encoding.UTF8);
            var responseString = await streamReader.ReadToEndAsync();

            responseString.ShouldBe(ExpectedMessage);
        }