예제 #1
0
        public async void ReturnsTrueForNonSuccessOrNoContentResponse(int statusCode)
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = null,
                Response       = new ApiResponseInfo
                {
                    StatusCode     = statusCode,
                    ResponseObject = null
                }
            };

            var processed = await context.ProcessHttpResponseCaching(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(2);
            context.Response.Headers[0].Name.Should().Be("Cache-Control");
            context.Response.Headers[0].Value.Should().Be("no-store, max-age=0");
            context.Response.Headers[1].Name.Should().Be("Expires");

            var now     = DateTime.UtcNow;
            var expires = DateTimeOffset.Parse(context.Response.Headers[1].Value);

            expires.Should().BeOnOrAfter(now.AddSeconds(-2));
        }
예제 #2
0
        public async void ReturnsTrueAndCorrectHeadersForCachableRequestMethodButNoRoutingItemConfigCacheDirective(string method)
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    Method = method
                },
                Response = new ApiResponseInfo
                {
                    StatusCode     = 200,
                    ResponseObject = null
                },
                Configuration = new DeepSleepRequestConfiguration
                {
                    CacheDirective = null
                }
            };

            var processed = await context.ProcessHttpResponseCaching(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(2);
            context.Response.Headers[0].Name.Should().Be("Cache-Control");
            context.Response.Headers[0].Value.Should().Be("no-store, max-age=0");
            context.Response.Headers[1].Name.Should().Be("Expires");

            var now     = DateTime.UtcNow;
            var expires = DateTimeOffset.Parse(context.Response.Headers[1].Value);

            expires.Should().BeOnOrAfter(now.AddSeconds(-2));
        }
예제 #3
0
        public async void ReturnsTrueAndCorrectHeadersForNonCachableRequestMethod(string method)
        {
            var mockRouteInfo = new Mock <ApiRoutingInfo>();

            mockRouteInfo.Setup(m => m.Route).Throws(new Exception("test"));

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    Method = method
                },
                Response = new ApiResponseInfo
                {
                    StatusCode = 200
                },
                Routing = mockRouteInfo.Object
            };

            var processed = await context.ProcessHttpResponseCaching(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(2);
            context.Response.Headers[0].Name.Should().Be("Cache-Control");
            context.Response.Headers[0].Value.Should().Be("no-store, max-age=0");
            context.Response.Headers[1].Name.Should().Be("Expires");

            var now     = DateTime.UtcNow;
            var expires = DateTimeOffset.Parse(context.Response.Headers[1].Value);

            expires.Should().BeOnOrAfter(now.AddSeconds(-2));
        }
예제 #4
0
        public async void ReturnsTrueForCancelledRequest()
        {
            var mockResponseInfo = new Mock <ApiResponseInfo>();

            mockResponseInfo.Setup(m => m.ResponseObject).Throws(new Exception("test"));

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(true),
                Response       = mockResponseInfo.Object
            };

            var processed = await context.ProcessHttpResponseCaching(null).ConfigureAwait(false);

            processed.Should().BeTrue();
        }
예제 #5
0
        public async void ReturnsTrueAndCorrectHeadersForCachableRequestMethodAndPrivateCacheLocation(string method)
        {
            var cacheDirective = new ApiCacheDirectiveConfiguration
            {
                Cacheability      = HttpCacheType.Cacheable,
                CacheLocation     = HttpCacheLocation.Private,
                ExpirationSeconds = 1,
                VaryHeaderValue   = "Accept, Accept-Encoding, Accept-Language"
            };

            var expiresDate = DateTime.Parse(DateTime.UtcNow.AddSeconds(cacheDirective.ExpirationSeconds.Value).ToString("r"));

            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    Method = method
                },
                Response = new ApiResponseInfo
                {
                    StatusCode     = 200,
                    ResponseObject = null
                },
                Configuration = new DeepSleepRequestConfiguration
                {
                    CacheDirective = cacheDirective
                }
            };

            var processed = await context.ProcessHttpResponseCaching(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(3);
            context.Response.Headers[0].Name.Should().Be("Cache-Control");
            context.Response.Headers[0].Value.Should().Be($"{cacheDirective.CacheLocation.ToString().ToLower()}, max-age={cacheDirective.ExpirationSeconds}");
            context.Response.Headers[1].Name.Should().Be("Expires");

            var expires = DateTime.Parse(context.Response.Headers[1].Value);

            expires.Should().BeOnOrAfter(expiresDate);

            context.Response.Headers[2].Name.Should().Be("Vary");
            context.Response.Headers[2].Value.Should().Be("Accept, Accept-Encoding, Accept-Language");
        }
예제 #6
0
        public async void ReturnsTrueAndCorrectHeadersForCachableRequestMethodButCacheDirectiveSecondsIsZero(string method)
        {
            var context = new ApiRequestContext
            {
                RequestAborted = new System.Threading.CancellationToken(false),
                Request        = new ApiRequestInfo
                {
                    Method = method
                },
                Response = new ApiResponseInfo
                {
                    StatusCode     = 200,
                    ResponseObject = null
                },
                Configuration = new DeepSleepRequestConfiguration
                {
                    CacheDirective = new ApiCacheDirectiveConfiguration
                    {
                        Cacheability      = HttpCacheType.Cacheable,
                        ExpirationSeconds = 0
                    }
                }
            };

            var processed = await context.ProcessHttpResponseCaching(null).ConfigureAwait(false);

            processed.Should().BeTrue();

            context.Response.Headers.Should().NotBeNull();
            context.Response.Headers.Should().HaveCount(3);
            context.Response.Headers[0].Name.Should().Be("Cache-Control");
            context.Response.Headers[0].Value.Should().Be("private, max-age=0");
            context.Response.Headers[1].Name.Should().Be("Expires");
            context.Response.Headers[2].Name.Should().Be("Vary");
            context.Response.Headers[2].Value.Should().Be("Accept, Accept-Encoding, Accept-Language");

            var now     = DateTime.UtcNow;
            var expires = DateTimeOffset.Parse(context.Response.Headers[1].Value);

            expires.Should().BeOnOrAfter(now.AddSeconds(-2));
        }