public void Cache_mocked()
        {
            var cache = new Mocks.MSolrCache();

            cache.get += url => {
                Assert.Equal("http://localhost:8983/solr/select/?q=*:*&version=2.2", url);
                return(new SolrCacheEntity(url, "", ""));
            };
            cache.add &= x => x.Stub();

            var response = new Mocks.HttpWebResponse {
                dispose = () => {},
                headers = () => new WebHeaderCollection {
                    { HttpResponseHeader.ETag, "123" },
                },
                getResponseStream = () => new MemoryStream(),
            };
            var getResponseCalls = 0;
            var conn             = new SolrConnection(solrURL)
            {
                Cache = cache,
                HttpWebRequestFactory = new Mocks.HttpWebRequestFactory {
                    create = _ => new Mocks.HttpWebRequest {
                        getResponse = () => {
                            getResponseCalls++;
                            if (getResponseCalls == 1)
                            {
                                return(response);
                            }
                            throw new Exception();
                        },
                        Headers = new WebHeaderCollection(),
                    },
                }
            };

            conn.Get("/select/", new Dictionary <string, string> {
                { "q", "*:*" },
            });

            conn.Get("/select/", new Dictionary <string, string> {
                { "q", "*:*" },
            });
        }
Beispiel #2
0
        public void WithWtJson_Get()
        {
            var expected = "https://pepe/?wt=json&version=2.2";

            var cache = new Mocks.MSolrCache();

            cache.get += url => new SolrCacheEntity(url, "", "");
            cache.add &= x => x.Stub();

            var headers = new WebHeaderCollection
            {
                { HttpResponseHeader.ETag, "etag" }, { HttpResponseHeader.CacheControl, "cache" }
            };
            var response = new Mocks.HttpWebResponse
            {
                dispose           = () => { },
                headers           = () => headers,
                getResponseStream = () => new MemoryStream(Encoding.UTF8.GetBytes("hello json world")),
            };

            var request = new Mocks.HttpWebRequest
            {
                getResponse = () => response,
                Headers     = new WebHeaderCollection()
            };

            var reqFactory = new Mocks.HttpWebRequestFactory
            {
                create = _ => request
            };
            var conn = new SolrConnection("https://pepe")
            {
                HttpWebRequestFactory = reqFactory,
                Cache = cache
            };

            var r = conn.Get("", new Dictionary <string, string> {
                { "wt", "json" }
            });
            var actual = conn.Cache[expected];

            Assert.Equal("hello json world", r);
            Assert.Equal(actual.Url, expected);
        }
Beispiel #3
0
        public void Cache_mocked() {
            var cache = new Mocks.MSolrCache();
            cache.get += url => {
                Assert.AreEqual("http://localhost:8983/solr/select/?q=*:*&version=2.2", url);
                return new SolrCacheEntity(url, "", "");
            };
            cache.add &= x => x.Stub();

            var response = new Mocks.HttpWebResponse {
                dispose = () => {},
                headers = () => new WebHeaderCollection {
                    {HttpResponseHeader.ETag, "123"},
                },
                getResponseStream = () => new MemoryStream(),
            };
            var getResponseCalls = 0;
            var conn = new SolrConnection(solrURL) {
                Cache = cache,
                HttpWebRequestFactory = new Mocks.HttpWebRequestFactory {
                    create = _ => new Mocks.HttpWebRequest {
                        getResponse = () => {
                            getResponseCalls++;
                            if (getResponseCalls == 1)
                                return response;
                            throw new Exception();
                        },
                        Headers = new WebHeaderCollection(),
                    },
                }
            };

            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });

            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });
        }
Beispiel #4
0
        public void CacheMaxAge_ShouldCallGetResponseWhenExpired()
        {
            var cache = new Mocks.MSolrCache();
            cache.get += url =>
            {
                Assert.AreEqual("http://localhost:8983/solr/select/?q=*:*&version=2.2", url);
                return new SolrCacheEntity(url, "", "", new DateTime(2013, 5, 12, 12, 30, 30));
            };
            cache.add &= x => x.Stub();

            var response = new Mocks.HttpWebResponse
            {
                dispose = () => { },
                headers = () => new WebHeaderCollection(),
                getResponseStream = () => new MemoryStream(),
            };
            var getResponseCalls = 0;
            var conn = new SolrConnection(solrURL)
            {
                Cache = cache,
                HttpWebRequestFactory = new Mocks.HttpWebRequestFactory
                {
                    create = _ => new Mocks.HttpWebRequest
                    {
                        getResponse = () =>
                        {
                            getResponseCalls++;
                            return response;
                        },
                        Headers = new WebHeaderCollection(),
                    },
                }
            };

            SystemTime.UtcNow = () => new DateTime(2013, 5, 12, 12, 30, 29);
            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });
            Assert.AreEqual(getResponseCalls, 0, "Should not call getResponse when valid expiration");

            SystemTime.UtcNow = () => new DateTime(2013, 5, 12, 12, 30, 35);
            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });

            Assert.AreEqual(getResponseCalls, 1, "Should call getResponse if the cache entry is expired");
        }