Пример #1
0
 public void Cache()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory());
     var response1 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
     var response2 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
 }
Пример #2
0
		public void ActualConnection() {
            var conn = new SolrConnection(solrURL) { HttpWebRequestFactory = new HttpWebRequestFactory() };
			var p = new Dictionary<string, string>();
			p["version"] = "2.1";
			p["indent"] = "on";
			p["q"] = "+video +price:[* TO 400]";
			Console.WriteLine(conn.Get("/select/", p));
		}
Пример #3
0
 public void ActualInvalidFieldException()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory());
     var p = new Dictionary<string, string>();
     p["version"] = "2.1";
     p["indent"] = "on";
     p["q"] = "idq:123";
     Console.WriteLine(conn.Get("/select/", p));
 }
Пример #4
0
		public void ActualConnectionWithException() {
            var conn = new SolrConnection(solrURL);
			var p = new Dictionary<string, string>();
			p["version"] = "2.1";
			p["indent"] = "on";
			p["q"] = "idq:123";
            try {
                conn.Get("/select/", p);
                Assert.Fail("Should have thrown");
            } catch (SolrConnectionException e) {
                Console.WriteLine(e);
                Console.WriteLine(e.Url);
            }
		}
Пример #5
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);
		}
Пример #6
0
 public void Cache_mocked()
 {
     var conn = new SolrConnection(solrURL, new HttpWebRequestFactory());
     var cache = MockRepository.GenerateMock<ISolrCache>();
     cache.Expect(x => x["http://localhost:8983/solr/select/?q=*:*"])
         .Repeat.Once()
         .Return(null);
     cache.Expect(x => x.Add(null)).Repeat.Once();
     conn.Cache = cache;
     var response1 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
     var response2 = conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
 }
Пример #7
0
 public void UndefinedFieldQueryError_ShouldThrow()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         var r = new WebResponseStub {StatusCode = HttpStatusCode.BadRequest};
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Throw(new WebException("(400) Bad Request", new ApplicationException(), WebExceptionStatus.ProtocolError, r));
     }).Verify(delegate {
         var conn = new SolrConnection("https://pepe", reqFactory);
         conn.Get("", new Dictionary<string, string>());
     });
 }
Пример #8
0
 public void InvalidHostGet_ShouldThrowException()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Throw(new WebException());
     }).Verify(delegate {
         var conn = new SolrConnection("http://lalala:12345", reqFactory);
         conn.Get("", new Dictionary<string, string>());
     });
 }
Пример #9
0
 public void GetWithNullParameters_ShouldAcceptNull()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     var response = mocks.DynamicMock<IHttpWebResponse>();
     With.Mocks(mocks).Expecting(delegate {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         Expect.Call(response.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Return(response);
         Expect.Call(response.GetResponseStream())
             .Repeat.Once()
             .Return(new MemoryStream());
     }).Verify(delegate {
         var conn = new SolrConnection("https://pepe", reqFactory);
         conn.Get("", new Dictionary<string, string>());
     });
 }
Пример #10
0
 public void Get_Compressed_Gzip()
 {
     var mocks = new MockRepository();
     var reqFactory = mocks.StrictMock<IHttpWebRequestFactory>();
     var request = mocks.DynamicMock<IHttpWebRequest>();
     var response = mocks.DynamicMock<IHttpWebResponse>();
     With.Mocks(mocks).Expecting(delegate
     {
         Expect.Call(reqFactory.Create(new UriBuilder().Uri))
             .IgnoreArguments()
             .Repeat.Once()
             .Return(request);
         Expect.Call(request.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(response.ContentEncoding)
             .Repeat.Any()
             .Return("gzip");
         Expect.Call(response.Headers)
             .Repeat.Any()
             .Return(new WebHeaderCollection());
         Expect.Call(request.GetResponse())
             .Repeat.Once()
             .Return(response);
         Expect.Call(response.GetResponseStream())
             .Repeat.Once()
             .Return(CompressionUtils.GzipCompressStream("Testing compression"));
     }).Verify(delegate {
         var conn = new SolrConnection("http://localhost") { HttpWebRequestFactory = reqFactory };
         Assert.AreEqual("Testing compression", conn.Get("", new Dictionary<string, string>()));
     });
 }
Пример #11
0
		public void InvalidHostGet_ShouldThrowException() {
		    var reqFactory = new Mocks.HttpWebRequestFactory {
		        create = _ => new Mocks.HttpWebRequest {
		            getResponse = () => { throw new WebException();}
		        }
		    };
            var conn = new SolrConnection("http://lalala:12345") { HttpWebRequestFactory = reqFactory };
            conn.Get("", new Dictionary<string, string>());
		}
Пример #12
0
 public void Cache() {
     var conn = new SolrConnection(solrURL);
     conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
     conn.Get("/select/", new Dictionary<string, string> {
         {"q", "*:*"},
     });
 }
Пример #13
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", "*:*"},
            });
        }
Пример #14
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");
        }