public async Task SendAsync_ProxyRevalidateAndFresh_CachedResponseIsReturned() { var freshResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(TenMinutesAgo) .Expires(_testDate.AddHours(2)) .WithProxyRevalidate() .Build(); var content = await freshResponse.Content.ReadAsByteArrayAsync(); _fakeMessageHandler.Response = freshResponse; _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns((StoredHttpResponseMessage)null); _cacheMock.Setup(x => x.Put(It.IsAny <string>(), It.IsAny <StoredHttpResponseMessage>())); var response = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Miss, response.Headers.GetCashewStatusHeader()); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(freshResponse, content)); var secondResponse = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.Equal(CacheStatus.Hit, secondResponse.Headers.GetCashewStatusHeader()); _cacheMock.Reset(); }
public async Task SendAsync_MustRevalidateAndResponseIsModified_NewResponseUsedAndReplacedOldCached() { var etag = "\"awesomeetag\""; var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK) .Created(TenMinutesAgo) .WithMustRevalidate() .WithETag(etag) .Expires(_testDate.Subtract(TimeSpan.FromMinutes(5))) .Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(cachedResponse, content)); var freshResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(_testDate).Expires(_testDate.Add(TimeSpan.FromMinutes(10))).Build(); _fakeMessageHandler.Response = freshResponse; var request = RequestBuilder.Request(HttpMethod.Get, Url).Build(); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); Assert.Equal(freshResponse, response); // _cacheMock.Verify(x => x.Put(It.IsAny<string>(), It.Is<StoredHttpResponseMessage>(message => message.Headers.Select(h=>h.Item2).ToArray().Equals(freshResponse.Headers.Select(s=>s.Key).ToArray()))), Times.Once); _cacheMock.Reset(); }
public async Task SendAsync_ProxyRevalidateAndStale_ResponseIsRevalidatedOnSubsequentRequest() { var firstResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(TenMinutesAgo) .WithMaxAge(60) .WithProxyRevalidate() .Build(); var content = await firstResponse.Content.ReadAsByteArrayAsync(); _fakeMessageHandler.Response = firstResponse; var revalidatedResponse = ResponseBuilder.Response(HttpStatusCode.NotModified).Created(_testDate).Build(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns((StoredHttpResponseMessage)null); _cacheMock.Setup(x => x.Put(It.IsAny <string>(), It.IsAny <StoredHttpResponseMessage>())); //save put var response = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Miss, response.Headers.GetCashewStatusHeader()); Assert.Equal(firstResponse, response); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(firstResponse, content)); _fakeMessageHandler.Response = revalidatedResponse; var secondResponse = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.Equal(CacheStatus.Revalidated, secondResponse.Headers.GetCashewStatusHeader()); Assert.Equal(firstResponse, secondResponse, new ResponseEqualityComparer()); _cacheMock.Verify(x => x.GetResponse(It.IsAny <string>()), Times.Exactly(2)); _cacheMock.Verify(x => x.Put(It.IsAny <string>(), It.IsAny <StoredHttpResponseMessage>()), Times.Exactly(2)); _cacheMock.Reset(); }
public async Task SendAsync_SharedMaxAgeInResponseAndAgeLessThanSharedMaxAge_CachedResponseIsFresh() { var serverResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(_testDate.Subtract(TimeSpan.FromMinutes(30))).WithSharedMaxAge(3600).Build(); var content = await serverResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns((StoredHttpResponseMessage)null); _fakeMessageHandler.Response = serverResponse; var response = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.Equal(CacheStatus.Miss, response.Headers.GetCashewStatusHeader()); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(serverResponse, content)); var secondResponse = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.Equal(CacheStatus.Hit, secondResponse.Headers.GetCashewStatusHeader()); }
public async Task SendAsync_StaleResponseWithinMaxStaleLimit_CachedResponseIsReturned() { var request = RequestBuilder.Request(HttpMethod.Get, Url).WithMaxStaleLimit(3600).Build(); var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(_testDate.Subtract(TimeSpan.FromMinutes(20))).Expires(TenMinutesAgo).Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(() => StoredHttpResponseMessage.Create(cachedResponse, content)); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Stale, response.Headers.GetCashewStatusHeader()); Assert.Equal(cachedResponse, response, new ResponseEqualityComparer()); _cacheMock.Verify(x => x.GetResponse(It.IsAny <string>()), Times.Once); _cacheMock.Reset(); }
public async Task SendAsync_MinFreshOneMinuteCachedResponseStillFreshForTwentySeconds_CacheIsRevalidated() { var request = RequestBuilder.Request(HttpMethod.Get, Url).WithMinFresh(60).Build(); var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK) .Created(TenMinutesAgo) .Expires(_testDate.AddSeconds(20)) .Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(() => StoredHttpResponseMessage.Create(cachedResponse, content)); _fakeMessageHandler.Response = ResponseBuilder.Response(HttpStatusCode.OK).Build(); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); _cacheMock.Verify(x => x.GetResponse(It.IsAny <string>()), Times.Once); _cacheMock.Reset(); }
public async Task SendAsync_OnlyIfCachedAndCachedResponseIsFresh_CachedResponseIsReturned() { var request = RequestBuilder.Request(HttpMethod.Get, Url).WithOnlyIfCached().Build(); var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK) .Created(TenMinutesAgo) .Expires(_testDate.AddHours(1)) .Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(() => StoredHttpResponseMessage.Create(cachedResponse, content)); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(cachedResponse, response, new ResponseEqualityComparer()); Assert.Equal(CacheStatus.Hit, response.Headers.GetCashewStatusHeader()); _cacheMock.Verify(x => x.GetResponse(It.IsAny <string>()), Times.Once); _cacheMock.Reset(); }
public async Task SendAsync_MustRevalidateAndLastModifiedInResponse_IfModifiedSinceAddedToRequest() { var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK) .Created(TenMinutesAgo) .WithMustRevalidate() .LastModified(TenMinutesAgo) .Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(cachedResponse, content)); _fakeMessageHandler.Response = ResponseBuilder.Response(HttpStatusCode.NotModified).Created(_testDate).Build(); var request = RequestBuilder.Request(HttpMethod.Get, Url).Build(); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); Assert.Equal(TenMinutesAgo, request.Headers.IfModifiedSince.Value); _cacheMock.Reset(); }
public async Task SendAsync_StaleResponseOverMaxStaleLimit_ResponseIsRevalidated() { var request = RequestBuilder.Request(HttpMethod.Get, Url).WithMaxStaleLimit(60).Build(); var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK).Expires(TenMinutesAgo).Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(() => StoredHttpResponseMessage.Create(cachedResponse, content)); var freshResponse = ResponseBuilder.Response(HttpStatusCode.NotModified).Created(_testDate).Build(); _fakeMessageHandler.Response = freshResponse; var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); Assert.Equal(cachedResponse, response, new ResponseEqualityComparer()); _cacheMock.Verify(x => x.GetResponse(It.IsAny <string>()), Times.Once); _cacheMock.Reset(); }
public async Task SendAsync_MaxAgeAndStaleWithStaleNotAcceptable_ResponseIsRevalidated() { var etag = "\"awesomeetag\""; var request = RequestBuilder.Request(HttpMethod.Get, Url).WithMaxAge(3600).Build(); var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(_testDate.Subtract(TimeSpan.FromHours(2))).WithMaxAge(3600).WithETag(etag).Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); var fakeResponse = ResponseBuilder.Response(HttpStatusCode.NotModified).Created(_testDate).Build(); _fakeMessageHandler.Response = fakeResponse; _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(cachedResponse, content)); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); Assert.Equal(cachedResponse, response, new ResponseEqualityComparer()); _cacheMock.Verify(x => x.GetResponse(It.IsAny <string>()), Times.Once); _cacheMock.Reset(); }
public async Task SendAsync_NoCacheInResponse_CachedResponseIsRevalidated() { _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(() => null); var freshResponse = ResponseBuilder.Response(HttpStatusCode.OK).Created(_testDate).WithNoCache().Build(); _fakeMessageHandler.Response = freshResponse; var firstResponse = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.NotNull(firstResponse.RequestMessage); var content = await firstResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(freshResponse, content)); var secondResponse = await _client.SendAsync(RequestBuilder.Request(HttpMethod.Get, Url).Build()); Assert.NotNull(secondResponse.RequestMessage); Assert.Equal(firstResponse, secondResponse, new ResponseEqualityComparer()); Assert.Equal(CacheStatus.Revalidated, secondResponse.Headers.GetCashewStatusHeader()); _cacheMock.Reset(); }
public void StoredHttpResponse_Serializes_With_BinaryFormatter() { var sample = Sample(); var serializer = new BinaryFormatter(); var stored = StoredHttpResponseMessage.Create(sample).Result; byte[] serialized; using (var writeStream = new MemoryStream()) { serializer.Serialize(writeStream, stored); serialized = writeStream.ToArray(); } StoredHttpResponseMessage restored; using (var readStream = new MemoryStream(serialized)) { restored = (StoredHttpResponseMessage)serializer.Deserialize(readStream); } Check(sample, restored.CopyIntoResponseMessage(new HttpRequestMessage())); }
public async Task SendAsync_MustRevalidateAndResponseIsNotModified_CachedResponseIsUpdatedAndUsed() { var etag = "\"awesomeetag\""; var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK) .Created(TenMinutesAgo) .WithMustRevalidate() .WithETag(etag) .Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(cachedResponse, content)); _fakeMessageHandler.Response = ResponseBuilder.Response(HttpStatusCode.NotModified).Created(_testDate).Build(); var request = RequestBuilder.Request(HttpMethod.Get, Url).Build(); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); Assert.Equal(cachedResponse, response, new ResponseEqualityComparer()); Assert.True(response.Headers.Date > TenMinutesAgo); _cacheMock.Reset(); }
public async Task SendAsync_MustRevalidateAndETagInResponse_IfNoneMatchAddedToRequest() { var etag = "\"awesomeetag\""; var cachedResponse = ResponseBuilder.Response(HttpStatusCode.OK) .Created(TenMinutesAgo) .WithMustRevalidate() .WithETag(etag) .Expires(_testDate.Subtract(TimeSpan.FromMinutes(5))) .Build(); var content = await cachedResponse.Content.ReadAsByteArrayAsync(); _cacheMock.Setup(x => x.GetResponse(It.IsAny <string>())).Returns(StoredHttpResponseMessage.Create(cachedResponse, content)); _fakeMessageHandler.Response = ResponseBuilder.Response(HttpStatusCode.NotModified).Created(_testDate).Build(); var request = RequestBuilder.Request(HttpMethod.Get, Url).Build(); var response = await _client.SendAsync(request); Assert.NotNull(response.RequestMessage); Assert.Equal(CacheStatus.Revalidated, response.Headers.GetCashewStatusHeader()); Assert.Equal(etag, request.Headers.IfNoneMatch.First().Tag); _cacheMock.Reset(); }
public void StoredHttpResponse_Serializes_With_Json() { var sample = Sample(); var serializer = new JsonSerializer { TypeNameHandling = TypeNameHandling.Objects }; var stored = StoredHttpResponseMessage.Create(sample).Result; string serialized; using (var writer = new StringWriter()) { serializer.Serialize(writer, stored); serialized = writer.ToString(); } StoredHttpResponseMessage restored; using (var reader = new StringReader(serialized)) { restored = (StoredHttpResponseMessage)serializer.Deserialize(reader, typeof(object)); } Check(sample, restored.CopyIntoResponseMessage(new HttpRequestMessage())); }