Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 211 lines (192 sloc) 6.816 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
#region license
// Copyright (c) 2007-2010 Mauricio Scheffer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using HttpWebAdapters;
using MbUnit.Framework;
using Moroco;
using SolrNet.Exceptions;
using SolrNet.Impl;

namespace SolrNet.Tests {
[TestFixture]
public class SolrConnectionTests {
        private const string solrURL = "http://localhost:8983/solr";

[Test]
[Category("Integration")]
[Ignore]
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));
}

[Test]
[Category("Integration")]
[Ignore]
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);
            }
}

[Test]
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);
}

[Test]
[ExpectedException(typeof (SolrConnectionException))]
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>());
}

[Test]
[ExpectedException(typeof (SolrConnectionException))]
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", "");
}

[Test]
[ExpectedException(typeof (InvalidURLException))]
public void InvalidUrl_ShouldThrowException() {
new SolrConnection("http:/locl");
}

[Test]
public void UrlHttp_ShouldntThrowException() {
new SolrConnection("http://pepe");
}

[Test]
public void UrlHttps_ShouldntThrowException() {
new SolrConnection("https://pepe");
}

[Test]
[ExpectedException(typeof (InvalidURLException))]
public void UrlNotHttp_ShouldThrowException() {
new SolrConnection("ftp://pepe");
}

[Test]
[Ignore("need to mock WebException!")]
        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", "*:*"},
            });
        }

[Test]
[Ignore]
        public void Cache() {
            var conn = new SolrConnection(solrURL);
            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });
            conn.Get("/select/", new Dictionary<string, string> {
                {"q", "*:*"},
            });
        }

[Test]
[Ignore]
        public void Cache_performance() {
            var conn = new SolrConnection(solrURL) {
                Cache = new HttpRuntimeCache(),
            };
            TestCache(conn);
        }

[Test]
[Ignore]
        public void NoCache_performance() {
            var conn = new SolrConnection(solrURL) {
                Cache = new NullCache(),
            };
            TestCache(conn);
        }

        public void TestCache(ISolrConnection conn) {
            foreach (var i in Enumerable.Range(0, 1000)) {
                conn.Get("/select/", new Dictionary<string, string> {
                    {"q", "*:*"},
                });
            }
        }
}
}
Something went wrong with that request. Please try again.