Пример #1
0
        public void Creation_fails_for_invalid_CacheContent()
        {
            // Arrange
            var primaryKey   = new PrimaryCacheKey("https://localhost/test", "POST");
            var secondaryKey = TestHelper.CreateContentKey();
            var entry        = new CacheEntry(primaryKey, new string[0]);
            var response     = new HttpResponseMessage();

            var noPrimary = new CacheContent()
            {
                ContentKey = secondaryKey,
                Response   = response
            };

            var noContentKey = new CacheContent()
            {
                Response = response
            };

            var noResponse = new CacheContent()
            {
                PrimaryKey = primaryKey,
                ContentKey = secondaryKey,
            };

            // Act

            // Assert
            Assert.Throws <ArgumentException>(() => SterlingCacheContent.CreateAsync(noPrimary).Result);
            Assert.Throws <ArgumentException>(() => SterlingCacheContent.CreateAsync(noContentKey).Result);
            Assert.Throws <ArgumentException>(() => SterlingCacheContent.CreateAsync(noResponse).Result);
        }
Пример #2
0
        public SterlingCacheContentTest()
        {
            primaryKey   = new PrimaryCacheKey("https://localhost/test", "POST");
            secondaryKey = TestHelper.CreateContentKey();
            entry        = new CacheEntry(primaryKey, new string[0]);
            response     = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent("42")
            };

            response.Headers.CacheControl = new CacheControlHeaderValue()
            {
                MaxAge = TimeSpan.FromHours(1)
            };
            response.Content.Headers.ContentLanguage.Add("de");

            // We need to add this here, since the header will be added during reconstruction in any case
            // That is (hopefully) not a problem, however, since it does not contribute to the "content" as such.
            response.Content.Headers.ContentLength = 2;

            content = new CacheContent()
            {
                PrimaryKey = primaryKey,
                ContentKey = secondaryKey,
                Response   = response
            };
        }
Пример #3
0
        public void Serializes_a_PrimaryCacheKey()
        {
            var memory = new MemoryStream();

            try
            {
                // Arrange
                var key = new PrimaryCacheKey(
                    "https://test/uri",
                    "POST"
                    );

                // Act
                PrimaryCacheKey restored = null;
                using (var writer = new BinaryWriter(memory, Encoding.UTF8, true))
                {
                    Serializer.Serialize(key, writer);
                }
                memory.Seek(0, SeekOrigin.Begin);
                using (var reader = new BinaryReader(memory, Encoding.UTF8, true))
                {
                    restored = Serializer.Deserialize <PrimaryCacheKey>(reader);
                }

                // Assert
                Assert.True(Serializer.CanSerialize(typeof(PrimaryCacheKey)));
                Assert.NotNull(restored);
                Assert.Equal(key.Uri, restored.Uri);
                Assert.Equal(key.Method, restored.Method);
            }
            finally
            {
                memory.Dispose();
            }
        }
        public void Serializes_a_PrimaryCacheKey()
        {
            var memory = new MemoryStream();
            try
            {
                // Arrange
                var key = new PrimaryCacheKey(
                        "https://test/uri",
                        "POST"
                    );

                // Act
                PrimaryCacheKey restored = null;
                using (var writer = new BinaryWriter(memory, Encoding.UTF8, true))
                {
                    Serializer.Serialize(key, writer);
                }
                memory.Seek(0, SeekOrigin.Begin);
                using (var reader = new BinaryReader(memory, Encoding.UTF8, true))
                {
                    restored = Serializer.Deserialize<PrimaryCacheKey>(reader);
                }

                // Assert
                Assert.True(Serializer.CanSerialize(typeof(PrimaryCacheKey)));
                Assert.NotNull(restored);
                Assert.Equal(key.Uri, restored.Uri);
                Assert.Equal(key.Method, restored.Method);
            }
            finally
            {
                memory.Dispose();
            }
        }
        /// <summary>
        /// Constructs a new CacheEntry.
        /// </summary>
        /// <param name="key">The primary key information for this entry.</param>
        /// <param name="varyHeaders">The Vary Header values for this entry.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="key"/> is <c>null</c></exception>
        /// <exception cref="ArgumentNullException">If <paramref name="varyHeaders"/> is <c>null</c></exception>
        public CacheEntry(PrimaryCacheKey key, IEnumerable<string> varyHeaders)
        {
            Contract.Requires<ArgumentNullException>(key != null, "key");
            Contract.Requires<ArgumentNullException>(varyHeaders != null, "varyHeaders");

            Key = key;
            VaryHeaders = varyHeaders;
        }     
Пример #6
0
        public async Task Does_not_fail_on_nonexistent_key()
        {
            // Arrange
            var key = new PrimaryCacheKey("https://localhost/test", "POST").ToString();

            // Act
            var result = await Database.LoadAsync <SterlingCacheEntry>(key);

            // Assert
            Assert.Null(result);
        }
        public async Task Does_not_fail_on_nonexistent_key()
        {
            // Arrange
            var key = new PrimaryCacheKey("https://localhost/test", "POST").ToString();

            // Act
            var result = await Database.LoadAsync<SterlingCacheEntry>(key);

            // Assert
            Assert.Null(result);
        }
        public Task<CacheEntry> GetEntryAsync(PrimaryCacheKey cacheKey)
        {
            // NB: Task.FromResult doesn't exist in MS.BCL.Async
            TaskCompletionSource<CacheEntry> ret = new TaskCompletionSource<CacheEntry>();

            if (_responseCache.ContainsKey(cacheKey))
            {
                ret.SetResult(_responseCache[cacheKey].CacheEntry);
            }
            else
            {
                ret.SetResult(null);
            }

            return ret.Task;
        }
        public async Task Can_serialize_a_CacheEntry()
        {
            // Arrange
            var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:443/test");
            var response = new HttpResponseMessage(HttpStatusCode.OK) { RequestMessage = request };
            var key = new PrimaryCacheKey(request.RequestUri, request.Method);
            var entry = new SterlingCacheEntry(key, Enumerable.Empty<string>());

            // Act
            var objKey = await Database.SaveAsAsync(entry);
            var restored = await Database.LoadAsync<SterlingCacheEntry>(objKey);

            // Assert
            Assert.NotNull(objKey);
            Assert.NotNull(restored);
            Assert.Equal(restored.PrimaryKey, entry.PrimaryKey);
            Assert.Empty(restored.VaryHeaders);
        }
        public async Task Creation_does_not_fail_with_empty_message()
        {
            // Arrange
            var primaryKey = new PrimaryCacheKey("https://localhost/test", "POST");
            var secondaryKey = TestHelper.CreateContentKey();
            var entry = new CacheEntry(primaryKey, new string[0]);
            var response = new HttpResponseMessage();

            var content = new CacheContent()
            {
                PrimaryKey = primaryKey,
                ContentKey = secondaryKey,                
                Response = response
            };

            // Act
            var sterlingContent = await SterlingCacheContent.CreateAsync(content);

            // Assert
            Assert.NotNull(sterlingContent);
        }
Пример #11
0
        public async Task Creation_does_not_fail_with_empty_message()
        {
            // Arrange
            var primaryKey   = new PrimaryCacheKey("https://localhost/test", "POST");
            var secondaryKey = TestHelper.CreateContentKey();
            var entry        = new CacheEntry(primaryKey, new string[0]);
            var response     = new HttpResponseMessage();

            var content = new CacheContent()
            {
                PrimaryKey = primaryKey,
                ContentKey = secondaryKey,
                Response   = response
            };

            // Act
            var sterlingContent = await SterlingCacheContent.CreateAsync(content);

            // Assert
            Assert.NotNull(sterlingContent);
        }
Пример #12
0
        public async Task Can_serialize_a_CacheEntry()
        {
            // Arrange
            var request  = new HttpRequestMessage(HttpMethod.Get, "https://localhost:443/test");
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                RequestMessage = request
            };
            var key   = new PrimaryCacheKey(request.RequestUri, request.Method);
            var entry = new SterlingCacheEntry(key, Enumerable.Empty <string>());

            // Act
            var objKey = await Database.SaveAsAsync(entry);

            var restored = await Database.LoadAsync <SterlingCacheEntry>(objKey);

            // Assert
            Assert.NotNull(objKey);
            Assert.NotNull(restored);
            Assert.Equal(restored.PrimaryKey, entry.PrimaryKey);
            Assert.Empty(restored.VaryHeaders);
        }
        public SterlingCacheContentTest()
        {
            primaryKey = new PrimaryCacheKey("https://localhost/test", "POST");
            secondaryKey = TestHelper.CreateContentKey();
            entry = new CacheEntry(primaryKey, new string[0]);
            response = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent("42")
            };

            response.Headers.CacheControl = new CacheControlHeaderValue() { MaxAge = TimeSpan.FromHours(1) };
            response.Content.Headers.ContentLanguage.Add("de");

            // We need to add this here, since the header will be added during reconstruction in any case
            // That is (hopefully) not a problem, however, since it does not contribute to the "content" as such. 
            response.Content.Headers.ContentLength = 2;

            content = new CacheContent()
            {
                PrimaryKey = primaryKey,
                ContentKey = secondaryKey,
                Response = response
            };
        }
 public SterlingCacheEntry(PrimaryCacheKey key, IEnumerable<string> varyHeaders)
     : base(key, varyHeaders) { }
Пример #15
0
 public Task <CacheEntry> GetAsync(
     PrimaryCacheKey key)
 {
     throw new NotImplementedException();
 }
Пример #16
0
 public Task SetAsync(
     PrimaryCacheKey primaryCacheKey,
     CacheEntry cacheEntry)
 {
     throw new NotImplementedException();
 }
Пример #17
0
 public async Task <CacheEntry> GetEntryAsync(PrimaryCacheKey primaryKey, CacheEntryKey entryKey)
 {
     return(null);
 }
Пример #18
0
 public Task <IEnumerable <CacheEntry> > GetEntriesAsync(PrimaryCacheKey primaryKey)
 {
     throw new NotImplementedException();
 }
Пример #19
0
 public Task <ICacheContent> GetContentAsync(PrimaryCacheKey primaryKey, CacheContentKey contentKey)
 {
     throw new NotImplementedException();
 }
 public SterlingCacheEntry(PrimaryCacheKey key, IEnumerable <string> varyHeaders)
     : base(key, varyHeaders)
 {
 }
Пример #21
0
        public async Task StoreResponseAsync(HttpResponseMessage response)
        {
            var primaryCacheKey = new PrimaryCacheKey(response.RequestMessage.RequestUri, response.RequestMessage.Method);

            CacheEntry cacheEntry = await _contentStore.GetEntryAsync(primaryCacheKey) ?? new CacheEntry(primaryCacheKey, response.Headers.Vary);

            var content = cacheEntry.CreateContent(response);
            await _contentStore.UpdateEntryAsync(content);

        }
        public void Creation_fails_for_invalid_CacheContent()
        {
            // Arrange
            var primaryKey = new PrimaryCacheKey("https://localhost/test", "POST");
            var secondaryKey = TestHelper.CreateContentKey();
            var entry = new CacheEntry(primaryKey, new string[0]);
            var response = new HttpResponseMessage();

            var noPrimary = new CacheContent()
            {
                ContentKey = secondaryKey,
                Response = response
            };

            var noContentKey = new CacheContent()
            {
                Response = response
            };

            var noResponse = new CacheContent()
            {
                PrimaryKey = primaryKey,
                ContentKey = secondaryKey,
            };

            // Act

            // Assert
            Assert.Throws<ArgumentException>(() => SterlingCacheContent.CreateAsync(noPrimary).Result);
            Assert.Throws<ArgumentException>(() => SterlingCacheContent.CreateAsync(noContentKey).Result);
            Assert.Throws<ArgumentException>(() => SterlingCacheContent.CreateAsync(noResponse).Result);
        }