You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by si...@apache.org on 2012/04/20 13:39:35 UTC

svn commit: r1328332 - in /lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj: BasicHttpSolrServerTest.java impl/ impl/BasicHttpSolrServerTest.java

Author: siren
Date: Fri Apr 20 11:39:35 2012
New Revision: 1328332

URL: http://svn.apache.org/viewvc?rev=1328332&view=rev
Log:
SOLR-3380: add some more basic testing for HttpSolrServer

Added:
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java
      - copied, changed from r1328325, lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/BasicHttpSolrServerTest.java
Removed:
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/BasicHttpSolrServerTest.java

Copied: lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java (from r1328325, lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/BasicHttpSolrServerTest.java)
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java?p2=lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java&p1=lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/BasicHttpSolrServerTest.java&r1=1328325&r2=1328332&rev=1328332&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/BasicHttpSolrServerTest.java (original)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/impl/BasicHttpSolrServerTest.java Fri Apr 20 11:39:35 2012
@@ -15,13 +15,14 @@
  * limitations under the License.
  */
 
-package org.apache.solr.client.solrj;
+package org.apache.solr.client.solrj.impl;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.Socket;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Map;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -34,9 +35,13 @@ import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.solr.SolrJettyTestBase;
+import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrRequest.METHOD;
-import org.apache.solr.client.solrj.impl.HttpSolrServer;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.request.UpdateRequest;
 import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.util.ExternalPaths;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -62,15 +67,21 @@ public class BasicHttpSolrServerTest ext
   }
   
   public static class DebugServlet extends HttpServlet {
+    public static void clear() {
+      lastMethod = null;
+      headers = null;
+      parameters = null;
+    }
     
     public static String lastMethod = null;
     public static HashMap<String,String> headers = null;
+    public static Map<String,String[]> parameters = null;
     
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
       lastMethod = "get";
-      setHeaders(req);
+      recordRequest(req, resp);
     }
     
     private void setHeaders(HttpServletRequest req) {
@@ -81,12 +92,21 @@ public class BasicHttpSolrServerTest ext
         headers.put(name, req.getHeader(name));
       }
     }
-    
+
+    private void setParameters(HttpServletRequest req) {
+      parameters = req.getParameterMap();
+    }
+
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
       lastMethod = "post";
+      recordRequest(req, resp);
+    }
+    
+    private void recordRequest(HttpServletRequest req, HttpServletResponse resp) {
       setHeaders(req);
+      setParameters(req);
     }
   }
   
@@ -131,36 +151,192 @@ public class BasicHttpSolrServerTest ext
   }
   
   @Test
-  public void testMethods() throws Exception {
+  public void testQuery(){
+    DebugServlet.clear();
     HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:"
         + jetty.getLocalPort() + "/solr/debug/foo");
-    SolrQuery q = new SolrQuery("*:*");
+    SolrQuery q = new SolrQuery("foo");
+    q.setParam("a", "\u1234");
     try {
-      QueryResponse response = server.query(q, METHOD.GET);
+      server.query(q, METHOD.GET);
     } catch (Throwable t) {}
-    assertEquals("get", DebugServlet.lastMethod);
     
+    //default method
+    assertEquals("get", DebugServlet.lastMethod);
+    //agent
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    //default wt
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    //default version
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    //agent
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    //keepalive
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+    //content-type
+    assertEquals(null, DebugServlet.headers.get("Content-Type"));
+    //param encoding
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+
+    //POST
+    DebugServlet.clear();
     try {
-      QueryResponse response = server.query(q, METHOD.POST);
+      server.query(q, METHOD.POST);
+    } catch (Throwable t) {}
+    assertEquals("post", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+    assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
+    assertEquals("UTF-8", DebugServlet.headers.get("Content-Charset"));
+
+    //XML/GET
+    server.setParser(new XMLResponseParser());
+    DebugServlet.clear();
+    try {
+      server.query(q, METHOD.GET);
+    } catch (Throwable t) {}
+    assertEquals("get", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+
+    //XML/POST
+    server.setParser(new XMLResponseParser());
+    DebugServlet.clear();
+    try {
+      server.query(q, METHOD.POST);
     } catch (Throwable t) {}
     assertEquals("post", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+    assertEquals("application/x-www-form-urlencoded; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
+    assertEquals("UTF-8", DebugServlet.headers.get("Content-Charset"));
   }
 
   @Test
-  public void testAgent() throws Exception {
+  public void testDelete(){
+    DebugServlet.clear();
     HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:"
         + jetty.getLocalPort() + "/solr/debug/foo");
-    SolrQuery q = new SolrQuery("*:*");
     try {
-      server.query(q, METHOD.GET);
+      server.deleteById("id");
     } catch (Throwable t) {}
+    
+    //default method
+    assertEquals("post", DebugServlet.lastMethod);
+    //agent
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    //default wt
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    //default version
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    //agent
     assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    //keepalive
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
+
+    //XML
+    server.setParser(new XMLResponseParser());
     try {
-      server.query(q, METHOD.POST);
+      server.deleteByQuery("*:*");
     } catch (Throwable t) {}
+    
+    assertEquals("post", DebugServlet.lastMethod);
     assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals("keep-alive", DebugServlet.headers.get("Connection"));
   }
+  
+  @Test
+  public void testUpdate(){
+    DebugServlet.clear();
+    HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:"
+        + jetty.getLocalPort() + "/solr/debug/foo");
+    UpdateRequest req = new UpdateRequest();
+    req.add(new SolrInputDocument());
+    req.setParam("a", "\u1234");
+    try {
+      server.request(req);
+    } catch (Throwable t) {}
+    
+    //default method
+    assertEquals("post", DebugServlet.lastMethod);
+    //agent
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    //default wt
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    //default version
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    //content type
+    assertEquals("application/xml; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
+    //parameter encoding
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
 
+    //XML response
+    server.setParser(new XMLResponseParser());
+    try {
+      server.request(req);
+    } catch (Throwable t) {}
+    assertEquals("post", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals("application/xml; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+    
+    //javabin request
+    server.setParser(new BinaryResponseParser());
+    server.setRequestWriter(new BinaryRequestWriter());
+    DebugServlet.clear();
+    try {
+      server.request(req);
+    } catch (Throwable t) {}
+    assertEquals("post", DebugServlet.lastMethod);
+    assertEquals("Solr[" + org.apache.solr.client.solrj.impl.HttpSolrServer.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
+    assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
+    assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
+    assertEquals(server.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
+    assertEquals("application/octet-stream", DebugServlet.headers.get("Content-Type"));
+    assertEquals(1, DebugServlet.parameters.get("a").length);
+    assertEquals("\u1234", DebugServlet.parameters.get("a")[0]);
+  }
+  
   @Test
   public void testRedirect() throws Exception {
     HttpSolrServer server = new HttpSolrServer("http://127.0.0.1:"
@@ -188,6 +364,7 @@ public class BasicHttpSolrServerTest ext
     SolrQuery q = new SolrQuery("*:*");
     
     // verify request header gets set
+    DebugServlet.clear();
     try {
       server.query(q);
     } catch (Throwable t) {}