コード例 #1
0
        public void When_sending_a_post_which_times_out()
        {
            var timeout  = 3.Seconds();
            var endpoint = new Uri("http://example.org/api/42");

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

            Should.Throw <TaskCanceledException>(
                async() => await client.PostAsync(endpoint.OriginalString, new MultipartFormDataContent(), timeout));
        }
コード例 #2
0
ファイル: RestClientTests.cs プロジェクト: dave-q/Easy.Common
        public async Task When_sending_a_post_request_via_explicit_post_with_string()
        {
            var endpoint = "http://localhost:8/api/";

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

                    var response = await client.PostAsync(endpoint, content);

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

                    respStr.ShouldBe("Hello There!");
                }
        }
コード例 #3
0
        public void When_sending_an_explicit_post_with_string_and_cancellation()
        {
            using var handler        = new DelayedResponseHandler(5.Seconds());
            using IRestClient client = new RestClient(handler: handler);
            var cts = new CancellationTokenSource(1.Seconds());

            Should.Throw <TaskCanceledException>(
                async() => await client.PostAsync("http://example.org/api/10", new MultipartFormDataContent(), cts.Token));
        }
コード例 #4
0
        public async Task When_sending_a_post_with_timeout()
        {
            var timeout = 3.Seconds();

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

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            var resp1 = await client.PostAsync(endpoint, new MultipartFormDataContent(), timeout);

            resp1.EnsureSuccessStatusCode();
            (await resp1.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage);

            var resp2 = await client.PostAsync(endpoint.OriginalString, new MultipartFormDataContent(), timeout);

            resp2.EnsureSuccessStatusCode();
            (await resp2.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage);
        }
コード例 #5
0
ファイル: RestClientTests.cs プロジェクト: dave-q/Easy.Common
        public async Task When_sending_a_post_request_via_explicit_post_with_string_with_cancellation_token()
        {
            var endpoint = "http://localhost:10/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.PostAsync(endpoint, new MultipartFormDataContent(), cts.Token));
                }
        }
コード例 #6
0
        public async Task When_sending_an_explicit_post_with_string()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/8");

            handler.AddFakeResponse(endpoint, expectedResponse);

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

            response.EnsureSuccessStatusCode();
            response.StatusCode.ShouldBe(HttpStatusCode.OK);

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

            respStr.ShouldBe(ExpectedMessage);
        }