public async Task GetAsync_CallNotFoundUrl_NullReturned() { var sut = new SmartCacheClient(new SmartCacheHttpClientBuilder(new EmptyClientCertProvider(), new SmartCacheHttpClientFactory())); var result = await sut.GetAsync <Post>(new Uri("https://jsonplaceholder.typicode.com/posts/doesntexist")); Assert.Equal(null, result); }
public async Task GetAsync_CallJsonApiSuccessfully_DeserializedObjectReturned() { var sut = new SmartCacheClient(new SmartCacheHttpClientBuilder(new EmptyClientCertProvider(), new SmartCacheHttpClientFactory())); var result = await sut.GetAsync <Post>(new Uri("https://jsonplaceholder.typicode.com/posts/3")); Assert.NotNull(result); Assert.Equal(3, result.Id); }
public async Task GetAsync_404HttpResponse_NullReturned() { var uri = new Uri("http://example.com/smartcache/post/2"); httpClient.Setup(h => h.GetAsync(uri)).ReturnsAsync(new HttpResponseMessage(HttpStatusCode.NotFound)); var sut = new SmartCacheClient(httpClientBuilder.Object); var result = await sut.GetAsync <Post>(uri); Assert.Null(result); }
public async Task GetAsync_CallApiWithCustomHttpClientFactoryAndDelegatingHandler_ShouldUseTheCustomDeletagatingHandler() { var delegatingHandlerUsed = false; Action delegatingHandlerCallBack = () => { delegatingHandlerUsed = true; }; var sut = new SmartCacheClient(new SmartCacheHttpClientBuilder(new EmptyClientCertProvider(), new CustomSmartCacheHttpClientFactory(delegatingHandlerCallBack))); var result = await sut.GetAsync <Post>(new Uri("https://jsonplaceholder.typicode.com/posts/3")); Assert.NotNull(result); Assert.Equal(3, result.Id); Assert.True(delegatingHandlerUsed); }
public async Task GetAsync_JsonHttpResponseWithBoolContent_DeserializedBoolReturned() { // Arrange var uri = new Uri("http://example.com/smartcache/post/3"); httpClient.Setup(h => h.GetAsync(uri)).ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("true", Encoding.UTF8, "application/json") }); var sut = new SmartCacheClient(httpClientBuilder.Object); // Act var result = await sut.GetAsync <bool>(uri); // Assert Assert.True(result); }
public async Task GetAsync_ResponseIsHalPlusJson_ContentSuccessfullyDeserialized() { // Arrange var uri = new Uri("http://example.com/smartcache/post/3"); httpClient.Setup(h => h.GetAsync(uri)).ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{ \"userId\": 2, \"id\": 3, \"title\": \"TestTitle\", \"body\": \"TestBody\" }", Encoding.UTF8, "application/hal+json") }); var sut = new SmartCacheClient(httpClientBuilder.Object); // Act var result = await sut.GetAsync <Post>(uri); // Assert Assert.NotNull(result); Assert.Equal(2, result.UserId); Assert.Equal(3, result.Id); Assert.Equal("TestTitle", result.Title); Assert.Equal("TestBody", result.Body); }