summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java')
-rw-r--r--container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java b/container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java
new file mode 100644
index 00000000000..c59dffb9cb7
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java
@@ -0,0 +1,117 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.search.federation.http;
+
+import com.yahoo.component.ComponentId;
+import com.yahoo.search.Query;
+import com.yahoo.search.Result;
+import com.yahoo.search.StupidSingleThreadedHttpServer;
+import com.yahoo.search.result.Hit;
+import com.yahoo.search.result.HitGroup;
+import com.yahoo.search.searchchain.Execution;
+import com.yahoo.statistics.Statistics;
+import com.yahoo.text.Utf8;
+
+import javax.xml.bind.JAXBException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Rudimentary http searcher test.
+ *
+ * @author <a href="mailto:bratseth@yahoo-inc.com">Jon Bratseth</a>
+ */
+public class HttpTestCase extends junit.framework.TestCase {
+
+ private StupidSingleThreadedHttpServer httpServer;
+ private TestHTTPClientSearcher searcher;
+
+ public void testSearcher() throws JAXBException {
+ Result result = searchUsingLocalhost();
+
+ assertEquals("ok", result.getQuery().properties().get("gotResponse"));
+ assertEquals(0, result.getQuery().errors().size());
+ }
+
+ private Result searchUsingLocalhost() {
+ searcher = new TestHTTPClientSearcher("test","localhost",getPort());
+ Query query = new Query("/?query=test");
+
+ query.setWindow(0,10);
+ return searcher.search(query, new Execution(searcher, Execution.Context.createContextStub()));
+ }
+
+ public void test_that_ip_address_set_on_meta_hit() {
+ Result result = searchUsingLocalhost();
+ Hit metaHit = getFirstMetaHit(result.hits());
+ String ip = (String) metaHit.getField(HTTPSearcher.LOG_IP_ADDRESS);
+
+ assertEquals(ip, "127.0.0.1");
+ }
+
+ private Hit getFirstMetaHit(HitGroup hits) {
+ for (Iterator<Hit> i = hits.unorderedDeepIterator(); i.hasNext();) {
+ Hit hit = i.next();
+ if (hit.isMeta())
+ return hit;
+ }
+ return null;
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ httpServer = new StupidSingleThreadedHttpServer(0, 0) {
+ @Override
+ protected byte[] getResponse(String request) {
+ return Utf8.toBytes("HTTP/1.1 200 OK\r\n" +
+ "Content-Type: text/xml; charset=UTF-8\r\n" +
+ "Connection: close\r\n" +
+ "Content-Length: 5\r\n" +
+ "\r\n" +
+ "hello");
+ }
+ };
+ httpServer.start();
+ }
+
+ private int getPort() {
+ return httpServer.getServerPort();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ httpServer.stop();
+ if (searcher != null) {
+ searcher.shutdownConnectionManagers();
+ }
+ }
+
+ private static class TestHTTPClientSearcher extends HTTPClientSearcher {
+
+ public TestHTTPClientSearcher(String id, String hostName, int port) {
+ super(new ComponentId(id), toConnections(hostName,port), "", Statistics.nullImplementation);
+ }
+
+ private static List<Connection> toConnections(String hostName,int port) {
+ List<Connection> connections=new ArrayList<>();
+ connections.add(new Connection(hostName,port));
+ return connections;
+ }
+
+ @Override
+ public Query handleResponse(InputStream inputStream, long contentLength, Query query) throws IOException {
+ query.properties().set("gotResponse","ok");
+ return query;
+ }
+
+ @Override
+ public Map<String, String> getCacheKey(Query q) {
+ return null;
+ }
+
+ }
+
+}