public void Add(string key, string data, TimeSpan expireIn, string eTag = null)
        {
            key = UrlEncodeString(key);

            if (string.IsNullOrWhiteSpace(eTag))
            {
                eTag = null;
            }

            var expiryDate = DateTime.Now.Add(expireIn);

            var    cachedResponse    = new CachedResponse(data, expiryDate, eTag);
            var    serizlizedReponse = JsonConvert.SerializeObject(cachedResponse);
            string formattedResponse = JToken.Parse(serizlizedReponse).ToString(Formatting.Indented);

            var cachedItemPath = Path.Combine(CacheLocation, key);

            try
            {
                File.WriteAllBytes(cachedItemPath, Encoding.ASCII.GetBytes(formattedResponse));
            }
            catch (Exception ex)
            {
                throw new CacheAccessException("An error occured when accessing the cache. More detail can be found in the inner exception.", ex);
            }
        }
        public void IsExpired_ReturnsTrue_WhenObject_IsConstructed_WithExpiryDate_ThatIs_InThePast()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", DateTime.Now.AddHours(-1));

            //Assert
            Assert.True(sut.IsExpired);
        }
        public void Content_ReturnsCorrectValue_FollowingObjectConstruction()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", DateTime.Now);

            //Assert
            Assert.Equal("Test Content", sut.Content);
        }
        public void HasEtag_ReturnsTrue_WhenEtag_IsSet_DuringConstruction()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", DateTime.Now, "my-test-etag");

            //Assert
            Assert.True(sut.HasEtag);
        }
        public void HasEtag_ReturnsFalse_WhenNoEtag_IsSet_DuringConstruction()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", DateTime.Now);

            //Assert
            Assert.False(sut.HasEtag);
        }
        public void Etag_ReturnsCorrectValue_GivenValue_At_Construction()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", DateTime.Now, "my-test-etag");

            //Assert
            Assert.Equal("my-test-etag", sut.Etag);
        }
        public void Etag_ReturnsNull_GivenNoValue_At_Construction()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", DateTime.Now);

            //Assert
            Assert.Null(sut.Etag);
        }
        public void ExpiryDate_ReturnsCorrectValue_FollowingObjectConstruction()
        {
            //Arrange
            var sut = new CachedResponse("Test Content", new DateTime(2018, 1, 10, 16, 15, 32));

            //Assert
            Assert.Equal(new DateTime(2018, 1, 10, 16, 15, 32), sut.ExpiryDate);
        }
Esempio n. 9
0
        public void GetExpiration_ReturnsCorrectDate_GivenUrl_AsKey()
        {
            //Arrange
            var expiryDate = DateTime.Now.AddDays(15);

            var cachedResponse     = new CachedResponse("my-test-cached-response", expiryDate);
            var serializedResponse = JsonConvert.SerializeObject(cachedResponse);
            var cachedItem         = File.Create(Path.Combine(_sut.CacheLocation, WebUtility.UrlEncode("my-test-cached-item")));

            cachedItem.Write(Encoding.ASCII.GetBytes(serializedResponse));
            cachedItem.Close();

            //Act
            var result = _sut.GetExpiration("my-test-cached-item");

            //Assert
            Assert.Equal(result, expiryDate);
        }