コード例 #1
0
        public async Task Deserialize_UnspecifiedFormat_Throws()
        {
            // arrange
            var json    = "{\"FirstName\" : \"Jon\", \"Age\" : 16, \"BirthDate\" : \"2/1/1201 12:00:00 AM\"}";
            var content = new StringContent(json);
            var handler = new FakeHandler(HttpStatusCode.OK, true);
            var client  = new RestClient();

            client.WithScheme(Scheme.Http).WithHost("www.google.com");
            client.SetHandler(handler);

            var result = await client.PostAsync(content);

            // act

            // assert

            try
            {
                result.Deserialize <DeserializationTestObject>(BodyFormat.Unspecified);
            }
            catch (AggregateException ex)
            {
                Assert.True(ex.InnerExceptions.Any(e => e is ArgumentOutOfRangeException));
                return;
            }

            Assert.False(true);
        }
コード例 #2
0
        public async Task SendAsync_ReturnsResponse()
        {
            // arrange
            var handler = new FakeHandler(HttpStatusCode.OK);
            var client  = new WebClient(handler);
            var request = GetWebRequest(HttpMethod.Get, false);

            // act
            var response = await client.SendAsync(request);

            // assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
コード例 #3
0
        public async Task SendAsyncT_SuccessNullBody_ReturnsNull()
        {
            // arrange
            var responseBody = "";
            var handler      = new FakeHandler(HttpStatusCode.OK, new StringContent(responseBody));
            var client       = new WebClient(handler);
            var request      = GetWebRequest(HttpMethod.Get, false);

            // act
            var result = await client.SendAsync <FakeClass1>(request);

            // assert
            Assert.Null(result);
        }
コード例 #4
0
        public async Task SendAsyncT_WithMapperDelegate_NonSuccess_ReturnsNull()
        {
            // arrange
            var testError = new StringContent("Test error");
            var handler   = new FakeHandler(HttpStatusCode.BadRequest, testError);
            var client    = new WebClient(handler);
            var request   = GetWebRequest(HttpMethod.Get, false);

            // act
            var result = await client.SendAsync(request, (input) => new FakeClass1());

            // assert
            Assert.Null(result);
        }
コード例 #5
0
        public async Task SendAsyncT_SuccessWithBody_DeserializesBody()
        {
            // arrange
            var responseBody = "{}";
            var handler      = new FakeHandler(HttpStatusCode.OK, new StringContent(responseBody));
            var client       = new WebClient(handler);
            var request      = GetWebRequest(HttpMethod.Get, false);

            // act
            var result = await client.SendAsync <FakeClass1>(request);

            // assert
            Assert.NotNull(result);
            Assert.IsType(typeof(FakeClass1), result);
        }
コード例 #6
0
        public async Task SendAsyncT_NonSuccess_ReturnNullObject()
        {
            // arrange
            var errorBody = new StringContent("Test error");
            var handler   = new FakeHandler(HttpStatusCode.BadRequest, errorBody);

            var client  = new WebClient(handler);
            var request = GetWebRequest(HttpMethod.Get, false);

            // act
            var result = await client.SendAsync <FakeClass1>(request);

            // assert
            Assert.Null(result);
        }
コード例 #7
0
        public async Task SendAsyncT_WithMapperDelegate_RunsDelegate()
        {
            // arrange
            var returnBody = new StringContent("{}");
            var handler    = new FakeHandler(HttpStatusCode.OK, returnBody);
            var client     = new WebClient(handler);
            var request    = GetWebRequest(HttpMethod.Get, false);
            var wasRun     = false;

            // act
            var result = await client.SendAsync(request, (input) =>
            {
                wasRun = true;
                return(new FakeClass1());
            });

            // assert
            Assert.NotNull(result);
            Assert.True(wasRun);
        }
コード例 #8
0
        public async Task Deserialize_Json_NoErrors()
        {
            // arrange
            var json    = "{\"FirstName\" : \"Jon\", \"Age\" : 16, \"BirthDate\" : \"2/1/1201 12:00:00 AM\"}";
            var content = new StringContent(json);
            var handler = new FakeHandler(HttpStatusCode.OK, true);
            var client  = new RestClient();

            client.WithScheme(Scheme.Http).WithHost("www.google.com");
            client.SetHandler(handler);

            var result = await client.PostAsync(content);

            // act
            var asObj = result.Deserialize <DeserializationTestObject>(BodyFormat.Json);

            // assert
            Assert.Equal("Jon", asObj.FirstName);
            Assert.Equal(16, asObj.Age);
            Assert.Equal(new DateTime(1201, 2, 1), asObj.BirthDate);
        }