Beispiel #1
0
 public void ReplaceHttpWebRequestFactory()
 {
     var builder = new ContainerBuilder();
     var getResponseCalls = 0;
     var response = new Mocks.HttpWebResponse
     {
         dispose = () => { },
         headers = () => new WebHeaderCollection {
             {HttpResponseHeader.ETag, "123"},
         },
         getResponseStream = () => 
             // If we don't give back at least the basic XML, we get an XmlParseException
             new MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response />")),
     };
     IHttpWebRequestFactory factory = new Mocks.HttpWebRequestFactory {
         create = _ => new Mocks.HttpWebRequest {
             getResponse = () => {
                 getResponseCalls++;
                 return response;
             },
             Headers = new WebHeaderCollection(),
         },
     };
     builder.RegisterModule(new SolrNetModule("http://localhost:8983/solr") { HttpWebRequestFactory = factory });
     var container = builder.Build();
     var operations = container.Resolve<ISolrOperations<Dictionary<string, object>>>();
     var results = operations.Query(new SolrQuery("q:*"));
     Assert.IsNotNull(results);
     Assert.AreEqual(1, getResponseCalls);
 }
Beispiel #2
0
        public void ReplaceHttpWebRequestFactory()
        {
            var builder          = new ContainerBuilder();
            var getResponseCalls = 0;
            var response         = new Mocks.HttpWebResponse
            {
                dispose = () => { },
                headers = () => new WebHeaderCollection {
                    { HttpResponseHeader.ETag, "123" },
                },
                getResponseStream = () =>
                                    // If we don't give back at least the basic XML, we get an XmlParseException
                                    new MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response />")),
            };
            IHttpWebRequestFactory factory = new Mocks.HttpWebRequestFactory {
                create = _ => new Mocks.HttpWebRequest {
                    getResponse = () => {
                        getResponseCalls++;
                        return(response);
                    },
                    Headers = new WebHeaderCollection(),
                },
            };

            builder.RegisterModule(new SolrNetModule("http://localhost:8983/solr")
            {
                HttpWebRequestFactory = factory
            });
            var container  = builder.Build();
            var operations = container.Resolve <ISolrOperations <Dictionary <string, object> > >();
            var results    = operations.Query(new SolrQuery("q:*"));

            Assert.IsNotNull(results);
            Assert.AreEqual(1, getResponseCalls);
        }
Beispiel #3
0
		public void Get() {
		    var response = new Mocks.HttpWebResponse {
		        dispose = () => {},
                headers = () => new WebHeaderCollection(),
                getResponseStream = () => new MemoryStream(Encoding.UTF8.GetBytes("hello world")),
		    };
		    var request = new Mocks.HttpWebRequest {
		        getResponse = () => response
		    };
		    var reqFactory = new Mocks.HttpWebRequestFactory {
		        create = _ => request
		    };
            var conn = new SolrConnection("https://pepe") {
                HttpWebRequestFactory = reqFactory,
            };
		    var r = conn.Get("", new Dictionary<string, string>());
            Assert.AreEqual("hello world", r);
		}
        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 #5
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);
        }
        public void Get()
        {
            var response = new Mocks.HttpWebResponse {
                dispose           = () => {},
                headers           = () => new WebHeaderCollection(),
                getResponseStream = () => new MemoryStream(Encoding.UTF8.GetBytes("hello world")),
            };
            var request = new Mocks.HttpWebRequest {
                getResponse = () => response
            };
            var reqFactory = new Mocks.HttpWebRequestFactory {
                create = _ => request
            };
            var conn = new SolrConnection("https://pepe")
            {
                HttpWebRequestFactory = reqFactory,
            };
            var r = conn.Get("", new Dictionary <string, string>());

            Assert.Equal("hello world", r);
        }
Beispiel #7
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 #8
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");
        }