Exemplo n.º 1
0
        public void Does_cache_duplicate_requests()
        {
            ServerCacheOnly.Count = 0;
            var request = new ServerCacheOnly {
                Id = 1, Value = "foo"
            };

            var response = Config.ListeningOn.CombineWith(request.ToGetUrl())
                           .GetJsonFromUrl(responseFilter: res => {
                Assert.That(res.GetHeader(HttpHeaders.ContentType), Does.StartWith(MimeTypes.Json));
                Assert.That(res.GetHeader(HttpHeaders.CacheControl), Is.Null);
            })
                           .FromJson <ServerCacheOnly>();

            Assert.That(ServerCacheOnly.Count, Is.EqualTo(1));
            AssertEquals(response, request);

            response = Config.ListeningOn.CombineWith(request.ToGetUrl())
                       .GetJsonFromUrl(responseFilter: res =>
            {
                Assert.That(res.GetHeader(HttpHeaders.ContentType), Does.StartWith(MimeTypes.Json));
                Assert.That(res.GetHeader(HttpHeaders.CacheControl), Is.Null);
            })
                       .FromJson <ServerCacheOnly>();

            Assert.That(ServerCacheOnly.Count, Is.EqualTo(1));
            AssertEquals(response, request);

            var client = CreateClient();

            response = client.Get <ServerCacheOnly>(request);
            Assert.That(ServerCacheOnly.Count, Is.EqualTo(1));
            AssertEquals(response, request);
        }
Exemplo n.º 2
0
        public void Does_vary_cache_by_QueryString()
        {
            ServerCacheOnly.Count = 0;
            var request = new ServerCacheOnly {
                Id = 2, Value = "foo"
            };

            var response = Config.ListeningOn.CombineWith(request.ToGetUrl())
                           .GetJsonFromUrl()
                           .FromJson <ServerCacheOnly>();

            Assert.That(ServerCacheOnly.Count, Is.EqualTo(1));
            AssertEquals(response, request);

            response = Config.ListeningOn.CombineWith(new ServerCacheOnly {
                Id = 1
            }.ToGetUrl())
                       .GetJsonFromUrl()
                       .FromJson <ServerCacheOnly>();

            Assert.That(ServerCacheOnly.Count, Is.EqualTo(2));
            Assert.That(response.Id, Is.EqualTo(1));
            Assert.That(response.Value, Is.Null);
        }
Exemplo n.º 3
0
        public void Does_cache_different_content_types_and_encoding()
        {
            ServerCacheOnly.Count = 0;
            var request = new ServerCacheOnly {
                Id = 4, Value = "bar"
            };
            var url = Config.ListeningOn.CombineWith(request.ToGetUrl());

            ServerCacheOnly response;

            //JSON + Deflate
            response = url.GetJsonFromUrl(responseFilter: res =>
            {
                Assert.That(res.GetHeader(HttpHeaders.ContentType), Does.StartWith(MimeTypes.Json));
            })
                       .FromJson <ServerCacheOnly>();

            Assert.That(ServerCacheOnly.Count, Is.EqualTo(1));
            AssertEquals(response, request);

            //JSON + No Accept-Encoding
            var webReq = WebRequest.CreateHttp(url);

            webReq.Accept = MimeTypes.Json;
#if !NETCORE
            webReq.AutomaticDecompression = DecompressionMethods.None;
#endif
            var webRes = webReq.GetResponse();
            Assert.That(webRes.ContentType, Does.StartWith(MimeTypes.Json));
            response = webRes.GetResponseStream().ReadToEnd()
                       .FromJson <ServerCacheOnly>();
            Assert.That(ServerCacheOnly.Count, Is.EqualTo(1)); //Uses plain json cache from #1
            AssertEquals(response, request);

            //JSON + GZip
            webReq        = WebRequest.CreateHttp(url);
            webReq.Accept = MimeTypes.Json;
            webReq.Headers[HttpHeaders.AcceptEncoding] = CompressionTypes.GZip;
#if !NETCORE
            webReq.AutomaticDecompression = DecompressionMethods.GZip;
#endif
            webRes = webReq.GetResponse();
            Assert.That(webRes.ContentType, Does.StartWith(MimeTypes.Json));
            var responseGzip = webRes.GetResponseStream().ReadFully();
#if !NETCORE
            response = responseGzip.FromUtf8Bytes().FromJson <ServerCacheOnly>();
#else
            response = responseGzip.Decompress("gzip").FromJson <ServerCacheOnly>();
#endif
            Assert.That(ServerCacheOnly.Count, Is.EqualTo(2)); //New encoding new cache
            AssertEquals(response, request);

            //XML + Deflate
            response = url.GetXmlFromUrl(responseFilter: res => {
                Assert.That(res.GetHeader(HttpHeaders.ContentType), Does.StartWith(MimeTypes.Xml));
            })
                       .FromXml <ServerCacheOnly>();
            Assert.That(ServerCacheOnly.Count, Is.EqualTo(3));
            AssertEquals(response, request);

            //HTML + Deflate
            var html = url.GetStringFromUrl(requestFilter: req => req.With(c => c.Accept = MimeTypes.Html));
            Assert.That(ServerCacheOnly.Count, Is.EqualTo(4));
            Assert.That(html, Does.StartWith("<!doctype html>"));
            html = url.GetStringFromUrl(requestFilter: req => req.With(c => c.Accept = MimeTypes.Html));
            Assert.That(ServerCacheOnly.Count, Is.EqualTo(4));
            Assert.That(html, Does.StartWith("<!doctype html>"));
        }
Exemplo n.º 4
0
 public object Any(ServerCacheOnly request)
 {
     Interlocked.Increment(ref ServerCacheOnly.Count);
     return(request);
 }