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); }
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); }
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 }; Assert.Throws <SolrConnectionException>(() => conn.Get("", new Dictionary <string, string>())); }
public void InvalidHostPost_ShouldThrowException() { var reqFactory = new Mocks.HttpWebRequestFactory { create = _ => new Mocks.HttpWebRequest { getRequestStream = () => { throw new WebException(); }, requestUri = () => new Uri("http://lalala:12345/update"), } }; var conn = new SolrConnection("http://lalala:12345") { HttpWebRequestFactory = reqFactory }; Assert.Throws <SolrConnectionException>(() => conn.Post("/update", "")); }
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 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); }
public void InvalidHostPost_ShouldThrowException() { var reqFactory = new Mocks.HttpWebRequestFactory { create = _ => new Mocks.HttpWebRequest { getRequestStream = () => { throw new WebException(); }, requestUri = () => new Uri("http://lalala:12345/update"), } }; var conn = new SolrConnection("http://lalala:12345") { HttpWebRequestFactory = reqFactory }; conn.Post("/update", ""); }
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>()); }