Пример #1
0
        private async Task WithObjectTestAsync()
        {
            var fakeResponseHandler = new FakeResponseHandler();
            var httpResponseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK
            };

            var person = new Person
            {
                DateOfBirth = DateTime.Now.Subtract(new TimeSpan(800, 1, 1, 1)),
                Firstname   = "First",
                Lastname    = "Person",
                PhoneNumber = "0123456789",
                SomeString  = "This is just a string"
            };

            var stringetje = JsonConvert.SerializeObject(person);

            httpResponseMessage.Content = new StringContent(stringetje);
            fakeResponseHandler.AddFakeResponse(new Uri("http://example.org/test"), httpResponseMessage);

            using (var client = new UncommonHttpClient(fakeResponseHandler, false))
            {
                var response = await client.GetJsonAsync <Person>("http://example.org/test");

                Assert.AreEqual(person.Firstname, response.Firstname);
                Assert.AreEqual(person.Lastname, response.Lastname);
                Assert.AreEqual(person.PhoneNumber, response.PhoneNumber);
            }
        }
Пример #2
0
        public async Task GetJsonAsyncNoGzipTestAsync()
        {
            using (ShimsContext.Create())
            {
                var person = new Person
                {
                    DateOfBirth = DateTime.Now.Subtract(new TimeSpan(800, 1, 1, 1)),
                    Firstname   = "First",
                    Lastname    = "Person",
                    PhoneNumber = "0123456789",
                    SomeString  = "This is just a string"
                };

                ShimHttpWebRequest.AllInstances.BeginGetResponseAsyncCallbackObject = (request, callback, arg3) =>
                {
                    return(ShimsContext.ExecuteWithoutShims(() => request.BeginGetResponse(callback, arg3)));
                };

                ShimHttpWebResponse.AllInstances.StatusCodeGet = response =>
                {
                    return(ShimsContext.ExecuteWithoutShims(() => response.StatusCode));
                };

                ShimHttpWebResponse.AllInstances.GetResponseStream = response =>
                {
                    var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(person));

                    var stream = new MemoryStream(bytes);

                    return(stream);
                };

                ShimHttpWebResponse.AllInstances.HeadersGet = response =>
                {
                    var headers = new WebHeaderCollection();
                    headers.Add(HttpRequestHeader.ContentType, "text/html; charset=utf-8");

                    //base = {Content-Encoding: gzip Content-Type: text/html; charset=utf-8 Content-Length: 155}

                    return(headers);
                };

                using (var client = new UncommonHttpClient())
                {
                    var result = await client.GetJsonAsync <Person>("http://www.xciles.com/");

                    Assert.AreEqual(person.Firstname, result.Firstname);
                    Assert.AreEqual(person.Lastname, result.Lastname);
                    Assert.AreEqual(person.PhoneNumber, result.PhoneNumber);
                }
            }
        }
Пример #3
0
        public async Task QuickDisposeTestsAsync()
        {
            var fakeResponseHandler = new FakeResponseHandler();

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

            using (var client = new UncommonHttpClient(fakeResponseHandler, false))
            {
                var response1 = await client.GetAsync("http://example.org/notthere");

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

                Assert.AreEqual(response1.StatusCode, HttpStatusCode.NotFound);
                Assert.AreEqual(response2.StatusCode, HttpStatusCode.OK);
            }
        }
Пример #4
0
        private async Task PostContentAsJsonStringTestAsync()
        {
            using (ShimsContext.Create())
            {
                ShimHttpClient.AllInstances.PostAsyncUriHttpContentCancellationToken = (client, uri, arg3, arg4) =>
                {
                    return(Task.FromResult(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK
                    }));
                };

                using (var client = new UncommonHttpClient())
                {
                    var result = await client.PostContentAsJsonAsync("http://www.xciles.com/", _person);

                    Assert.IsTrue(result.StatusCode == HttpStatusCode.OK);
                }
            }
        }
Пример #5
0
        private async Task PatchContentAsJsonUriTestAsync()
        {
            using (ShimsContext.Create())
            {
                ShimHttpClient.AllInstances.SendAsyncHttpRequestMessageCancellationToken = (client, message, token) =>
                {
                    return(Task.FromResult(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK
                    }));
                };

                using (var client = new UncommonHttpClient())
                {
                    var result = await client.PatchContentAsJsonAsync(new Uri("http://www.xciles.com/"), _person);

                    Assert.IsTrue(result.StatusCode == HttpStatusCode.OK);
                }
            }
        }
Пример #6
0
        private async Task PatchAsyncUriTestAsync()
        {
            using (ShimsContext.Create())
            {
                ShimHttpClient.AllInstances.SendAsyncHttpRequestMessage = (client, message) =>
                {
                    return(Task.FromResult(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK
                    }));
                };

                using (var client = new UncommonHttpClient())
                {
                    var content = new StringContent(JsonConvert.SerializeObject(_person));
                    var result  = await client.PatchAsync(new Uri("http://www.xciles.com/"), content);

                    Assert.IsTrue(result.StatusCode == HttpStatusCode.OK);
                }
            }
        }
Пример #7
0
        private async Task GetJsonAsyncUriTestAsync()
        {
            using (ShimsContext.Create())
            {
                ShimHttpClient.AllInstances.GetAsyncUriHttpCompletionOptionCancellationToken = (httpClient, uri, arg3, arg4) =>
                {
                    return(Task.FromResult(new HttpResponseMessage()
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(_person)),
                        StatusCode = HttpStatusCode.OK
                    }));
                };

                using (var client = new UncommonHttpClient())
                {
                    var result = await client.GetJsonAsync <Person>(new Uri("http://www.xciles.com/"));

                    Assert.AreEqual(_person.Firstname, result.Firstname);
                    Assert.AreEqual(_person.Lastname, result.Lastname);
                    Assert.AreEqual(_person.PhoneNumber, result.PhoneNumber);
                }
            }
        }