コード例 #1
0
        public void PatchAsync_Sad_Test()
        {
            //Arrange
            var testObject = new SimpleTestObject {
                TestProperty = "Test Value", TestProperty2 = 2
            };
            var httpClientResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError)
            {
                Content = null
            };
            var httpClient = new Mock <IHttpClient>();

            httpClient.Setup(x => x.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(httpClientResponse);
            var config     = new TestRestConfig();
            var restClient = new TestRestClient(config, httpClient.Object);

            //Act
            var responseTask = restClient.PatchAsync <SimpleTestObject, SimpleTestObject>("TestObject", testObject);
            var response     = responseTask.GetAwaiter().GetResult();

            //Assert
            Assert.IsFalse(response.IsSuccessStatusCode);
            Assert.IsNull(response.Data);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.InternalServerError);
        }
コード例 #2
0
        public void GetAsync_Happy_Test()
        {
            //Arrange
            var testObject = new SimpleTestObject {
                TestProperty = "Test Value", TestProperty2 = 2
            };
            var content            = new StringContent(testObject.ToJsonString());
            var httpClientResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = content
            };
            var httpClient = new Mock <IHttpClient>();

            httpClient.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(httpClientResponse);
            var config = new TestRestConfig();
            var client = new TestRestClient(config, httpClient.Object);

            //Act
            var responseTask = client.GetAsync <SimpleTestObject>("TestObject");
            var response     = responseTask.GetAwaiter().GetResult();

            //Assert
            Assert.IsTrue(response.IsSuccessStatusCode);
            Assert.AreEqual(response.Data.TestProperty, testObject.TestProperty);
        }
コード例 #3
0
        public void Standard_Constructor_Test()
        {
            //Arrange
            var config = new TestRestConfig {
                BaseUrl = "http://example.com/api"
            };

            //Act
            var client = new TestRestClient(config);

            //Assert
            Assert.AreEqual(config.BaseUrl, client.HttpClient.BaseAddress.ToString());
        }
コード例 #4
0
        public void DeleteAsync_No_Response_Sad_Test()
        {
            //Arrange
            var httpClientResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            var httpClient         = new Mock <IHttpClient>();

            httpClient.Setup(x => x.DeleteAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(httpClientResponse);
            var config = new TestRestConfig();
            var client = new TestRestClient(config, httpClient.Object);

            //Act
            var responseTask = client.DeleteAsync("TestObject");
            var response     = responseTask.GetAwaiter().GetResult();

            //Assert
            Assert.IsFalse(response.IsSuccessStatusCode);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.InternalServerError);
        }
コード例 #5
0
        public void GetStringAsync_Happy_Test()
        {
            //Arrange
            var testObject = new SimpleTestObject {
                TestProperty = "Test Value", TestProperty2 = 2
            };
            var httpClient = new Mock <IHttpClient>();

            httpClient.Setup(x => x.GetStringAsync(It.IsAny <string>()))
            .ReturnsAsync(testObject.ToJsonString());
            var config = new TestRestConfig();
            var client = new TestRestClient(config, httpClient.Object);

            //Act
            var responseTask = client.GetStringAsync("TestObject");
            var response     = responseTask.GetAwaiter().GetResult();

            //Assert
            Assert.AreEqual(response, testObject.ToJsonString());
        }