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

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

            Should.Throw <TaskCanceledException>(
                async() => await client.GetByteArrayAsync(endpoint.OriginalString, timeout));
        }
예제 #2
0
        public async Task When_sending_a_get_byte_array_request_with_string()
        {
            var endpoint = "http://localhost:31/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.GetByteArrayAsync(endpoint);

                    var responseString = Encoding.UTF8.GetString(response);
                    responseString.ShouldBe("Hello There!");
                }
        }
        public async Task When_sending_a_get_byte_array_with_timeout()
        {
            var timeout = 3.Seconds();

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

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            var expectedBytes = Encoding.UTF8.GetBytes(ExpectedMessage);

            (await client.GetByteArrayAsync(endpoint, timeout)).ShouldBe(expectedBytes);

            (await client.GetByteArrayAsync(endpoint.OriginalString, timeout)).ShouldBe(expectedBytes);
        }
        public async Task When_sending_a_get_byte_array_with_string()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = BytesResponseMessage;
            var endpoint = new Uri("http://example.org/api/32");

            handler.AddFakeResponse(endpoint, expectedResponse);

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

            var responseString = Encoding.UTF8.GetString(response);

            responseString.ShouldBe(ExpectedMessage);
        }