Exemplo n.º 1
0
        public async Task When_sending_a_get_byte_array_request_with_string()
        {
            const string 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!");
                }
        }
Exemplo n.º 2
0
        public async Task When_sending_a_delete_request_via_explicit_delete_with_string()
        {
            const string Endpoint = "http://localhost:1004/api/";

            using (IRestClient client = new RestClient())
                using (var server = new SimpleHttpListener(new Uri(Endpoint)))
                {
                    server.OnRequest += (sender, context) =>
                    {
                        if (context.Request.HttpMethod != HttpMethod.Delete.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.DeleteAsync(Endpoint);

                    response.EnsureSuccessStatusCode();

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

                    respStr.ShouldBe("Hello There!");
                }
        }
Exemplo n.º 3
0
        public async Task When_sending_a_get_request_via_explicit_get_with_uri_with_completion_option()
        {
            var endpoint = new Uri("http://localhost:15/api/");

            using (IRestClient client = new RestClient())
                using (var server = new SimpleHttpListener(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!");
                }
        }
Exemplo n.º 4
0
        public async Task When_sending_a_get_request_via_explicit_get_with_string_with_cancellation_token()
        {
            const string Endpoint = "http://localhost:14/api/";

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

                    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
                    Should.Throw <TaskCanceledException>(async() => await client.GetAsync(Endpoint, cts.Token));
                }
        }
Exemplo n.º 5
0
        public async Task When_sending_a_delete_request_via_explicit_delete_with_uri_with_cancellation_token()
        {
            var endpoint = new Uri("http://localhost:34/api/");

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

                    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
                    Should.Throw <TaskCanceledException>(
                        async() => await client.DeleteAsync(endpoint, cts.Token));
                }
        }
Exemplo n.º 6
0
        public async Task When_sending_a_delete_request()
        {
            var endpoint = new Uri("http://localhost:1002/api/");

            using (IRestClient client = new RestClient())
                using (var server = new SimpleHttpListener(endpoint))
                {
                    server.OnRequest += (sender, context) =>
                    {
                        if (context.Request.HttpMethod != HttpMethod.Delete.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 request = new HttpRequestMessage
                    {
                        Method     = HttpMethod.Delete,
                        RequestUri = endpoint
                    };
                    request.Headers.Add("Foo", "Bar");

                    var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);

                    response.EnsureSuccessStatusCode();

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

                    respStr.ShouldBe("Hello There!");
                }
        }
Exemplo n.º 7
0
        public async Task When_sending_a_request_with_cancellation()
        {
            var endpoint = new Uri("http://localhost:999/api/");

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

                    var request = new HttpRequestMessage
                    {
                        Method     = HttpMethod.Get,
                        RequestUri = endpoint
                    };
                    request.Headers.Add("Foo", "Bar");

                    var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
                    Should.Throw <TaskCanceledException>(
                        async() => await client.SendAsync(request, cts.Token));
                }
        }
Exemplo n.º 8
0
        public async Task When_sending_a_request_then_cancelling_all_pending_requests()
        {
            var endpoint = new Uri("http://localhost:26/api/");

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

                    var request = new HttpRequestMessage
                    {
                        Method     = HttpMethod.Get,
                        RequestUri = endpoint
                    };
                    request.Headers.Add("Foo", "Bar");

                    var copy = client;
#pragma warning disable 4014
                    Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(_ => copy.CancelPendingRequests());
#pragma warning restore 4014
                    Should.Throw <TaskCanceledException>(async() => await client.SendAsync(request));
                }
        }