Esempio n. 1
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!");
                }
        }
Esempio n. 2
0
        public async Task When_sending_a_delete_request_via_explicit_delete_with_string()
        {
            var endpoint = "http://localhost:21/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!");
                }
        }
Esempio n. 3
0
        public async Task When_sending_a_delete_request_via_explicit_delete_with_string_with_cancellation_token()
        {
            var endpoint = "http://localhost:23/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.DeleteAsync(endpoint, cts.Token));
                }
        }
Esempio n. 4
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));
                }
        }
Esempio n. 5
0
        public async Task When_sending_a_put_request_via_explicit_put_with_uri_with_cancellation()
        {
            var endpoint = new Uri("http://localhost:4/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.PutAsync(endpoint, new MultipartFormDataContent(), cts.Token));
                }
        }
Esempio n. 6
0
        public async Task When_sending_a_request_with_cancellation()
        {
            var endpoint = new Uri("http://localhost:25/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(1.Seconds());
                    Should.Throw <TaskCanceledException>(async() => await client.SendAsync(request, cts.Token));
                }
        }
Esempio n. 7
0
        public async Task When_sending_a_delete_request()
        {
            var endpoint = new Uri("http://localhost:19/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!");
                }
        }
Esempio 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(1.Seconds()).ContinueWith(_ => copy.CancelPendingRequests());
#pragma warning restore 4014
                    Should.Throw <TaskCanceledException>(async() => await client.SendAsync(request));
                }
        }