public async Task Should_cache_response_when_response_is_successful() { // arrange string cuit = "5050505050"; string rule = "2323232323"; CancellationToken cancellationToken = CancellationToken.None; SpecialRecordEntry expected = new SpecialRecordEntry() { Status = "activo" }; string url = $"SpecialRecord?key={cuit}&rule={rule}"; object value = null; this.CacheAccessorMock .Setup(x => x.TryGetValue(It.Is <string>(u => u.ToString() == url), out value)) .Returns(false); this.HttpMessageHandlerMock .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(expected)) }); // act var actual = await this.Sut.GetSpecialRecordsAsync(cuit, rule, cancellationToken); // assert actual.Should().BeEquivalentTo(expected); this.HttpMessageHandlerMock .Protected() .Verify( "SendAsync", Times.Once(), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ); this.CacheAccessorMock.Verify( x => x.Set( It.Is <string>(p => p == url), It.IsAny <object>(), this.CacheOptions.Value.EntityDataBasicExpiration ), Times.Once() ); }
public async Task Should_not_call_http_client_if_response_is_cached() { // arrange string cuit = "5050505050"; string rule = "2323232323"; CancellationToken cancellationToken = CancellationToken.None; SpecialRecordEntry expected = new SpecialRecordEntry() { Status = "activo" }; string url = $"SpecialRecord?key={cuit}&rule={rule}"; object value = expected; this.CacheAccessorMock .Setup(x => x.TryGetValue(It.Is <string>(u => u.ToString() == url), out value)) .Returns(true); // act var actual = await this.Sut.GetSpecialRecordsAsync(cuit, rule, cancellationToken); // assert actual.Should().BeEquivalentTo(expected); this.HttpMessageHandlerMock .Protected() .Verify( "SendAsync", Times.Never(), ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ); this.CacheAccessorMock.Verify( x => x.TryGetValue( It.IsAny <string>(), out value ), Times.Once() ); }
public async Task Should_call_http_client_with_correct_url_if_response_is_not_cached() { // arrange string cuit = "5050505050"; string rule = "2323232323"; CancellationToken cancellationToken = CancellationToken.None; SpecialRecordEntry expected = new SpecialRecordEntry() { Status = "activo" }; string url = $"SpecialRecord?key={cuit}&rule={rule}"; Uri expectedUri = new Uri($"{this.HttpClient.BaseAddress}{url}"); this.HttpMessageHandlerMock .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(expected)) }); // act var actual = await this.Sut.GetSpecialRecordsAsync(cuit, rule, cancellationToken); // assert actual.Should().BeEquivalentTo(expected); this.HttpMessageHandlerMock .Protected() .Verify( "SendAsync", Times.Once(), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get && req.RequestUri == expectedUri), ItExpr.IsAny <CancellationToken>() ); }