aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/federation/http/HttpTestCase.java
blob: b4f3c13b8e2ac5aac5c0796da58afac2dfad0f5e (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. 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 org.junit.After;
import org.junit.Before;
import org.junit.Test;

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;

import static org.junit.Assert.assertEquals;

/**
 * Rudimentary http searcher test.
 *
 * @author bratseth
 */
public class HttpTestCase {

    private StupidSingleThreadedHttpServer httpServer;
    private TestHTTPClientSearcher searcher;

    @Test
    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()));
    }

    @Test
    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;
    }

    @Before
    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();
    }

    @After
    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;
        }

    }

}