示例#1
0
        public async Task CallFakeRequest()
        {
            var fakeResponseHandler = new FakeResponseHandler();

            fakeResponseHandler.AddFakeResponse(
                new Uri("http://example.org/test"), new HttpResponseMessage(HttpStatusCode.OK));

            var httpClient = new HttpClient(fakeResponseHandler);

            var response1 = await httpClient.GetAsync("http://example.org/notthere");

            var response2 = await httpClient.GetAsync("http://example.org/test");

            response1.StatusCode.ShouldBe(HttpStatusCode.NotFound);
            response2.StatusCode.ShouldBe(HttpStatusCode.OK);
        }
示例#2
0
        public async Task When_sending_a_get_string_with_string()
        {
            using (var handler = new FakeResponseHandler())
                using (var expectedResponse = StringResponseMessage)
                {
                    var endpoint = new Uri("http://example.org/api/28");
                    handler.AddFakeResponse(endpoint, expectedResponse);

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

                        response.ShouldBe(ExpectedMessage);
                    }
                }
        }
        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);
        }
示例#4
0
        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);
                    }
                }
        }
        public async Task When_sending_an_explicit_delete_with_string()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/21");

            handler.AddFakeResponse(endpoint, expectedResponse);

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

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

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

            respStr.ShouldBe(ExpectedMessage);
        }
        public async Task When_sending_an_explicit_get_with_uri_and_completion_option()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/15");

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            var response = await client.GetAsync(endpoint, HttpCompletionOption.ResponseContentRead);

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

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

            respStr.ShouldBe(ExpectedMessage);
        }
        public async Task When_sending_an_explicit_post_with_uri()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/7");

            handler.AddFakeResponse(endpoint, expectedResponse);

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

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

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

            respStr.ShouldBe(ExpectedMessage);
        }
示例#8
0
        public async Task When_sending_a_get_string_with_timeout()
        {
            var timeout = 3.Seconds();

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

                    using (IRestClient client = new RestClient(handler: handler))
                    {
                        (await client.GetStringAsync(endpoint, timeout)).ShouldBe(ExpectedMessage);

                        (await client.GetStringAsync(endpoint.OriginalString, timeout)).ShouldBe(ExpectedMessage);
                    }
                }
        }
示例#9
0
        public async Task When_calling_a_fake_handler()
        {
            using (var handler = new FakeResponseHandler())
            {
                handler.AddFakeResponse(
                    new Uri("http://example.org/test"), new HttpResponseMessage(HttpStatusCode.OK));

                using (IRestClient client = new RestClient(handler: handler))
                {
                    var response1 = await client.GetAsync("http://example.org/notthere");

                    var response2 = await client.GetAsync("http://example.org/test");

                    response1.StatusCode.ShouldBe(HttpStatusCode.NotFound);
                    response2.StatusCode.ShouldBe(HttpStatusCode.OK);
                }
            }
        }
        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);
        }
示例#11
0
        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);
                        }
                    }
                }
        }
示例#12
0
        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);
                        }
                    }
                }
        }
        public async Task When_sending_a_delete_request()
        {
            using var handler          = new FakeResponseHandler();
            using var expectedResponse = StringResponseMessage;
            var endpoint = new Uri("http://example.org/api/19");

            handler.AddFakeResponse(endpoint, expectedResponse);

            using IRestClient client = new RestClient(handler: handler);
            var request = new HttpRequestMessage {
                Method = HttpMethod.Delete, RequestUri = endpoint
            };

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

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

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

            respStr.ShouldBe(ExpectedMessage);
        }
        public async Task When_sending_a_request_with_base_address_and_route()
        {
            var endpoint1 = new Uri("http://foo.bar/api/1");
            var resp1     = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("A")
            };

            var endpoint2 = new Uri("http://foo.bar/api/2");
            var resp2     = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("B")
            };

            var endpoint3 = new Uri("http://foo.bar/api/3");
            var resp3     = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("C")
            };

            using var handler = new FakeResponseHandler();
            using (resp1)
                using (resp2)
                    using (resp3)
                    {
                        handler.AddFakeResponse(endpoint1, resp1);
                        handler.AddFakeResponse(endpoint2, resp2);
                        handler.AddFakeResponse(endpoint3, resp3);

                        var baseAddress = new Uri("http://foo.bar");
                        using IRestClient client = new RestClient(handler: handler, baseAddress: baseAddress);
                        client.BaseAddress.ShouldBe(baseAddress);

                        (await client.GetStringAsync("api/1")).ShouldBe("A");
                        (await client.GetStringAsync("api/2")).ShouldBe("B");
                        (await client.GetStringAsync("api/3")).ShouldBe("C");
                    }
        }
示例#15
0
        public async Task When_sending_a_delete_with_timeout()
        {
            var timeout = 3.Seconds();

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

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

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

                        var resp2 = await client.DeleteAsync(endpoint.OriginalString, timeout);

                        resp2.EnsureSuccessStatusCode();
                        (await resp2.Content.ReadAsStringAsync()).ShouldBe(ExpectedMessage);
                    }
                }
        }