aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/jdisc/LoggingRequestHandlerTestCase.java
blob: 94158b277c405507d8e1b1b643fdd7e88d9eef73 (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc;

import com.google.inject.Key;
import com.yahoo.container.handler.Coverage;
import com.yahoo.container.handler.Timing;
import com.yahoo.container.logging.HitCounts;
import com.yahoo.jdisc.Container;
import com.yahoo.jdisc.References;
import com.yahoo.jdisc.ResourceReference;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.service.CurrentContainer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.fail;

/**
 * Test contracts in LoggingRequestHandler.
 *
 * @author Steinar Knutsen
 */
public class LoggingRequestHandlerTestCase {

    AccessLogTestHandler handler;
    ExecutorService executor;

    public static final class NoTimingResponse extends ExtendedResponse {

        public NoTimingResponse() {
            super(200);
        }


        @Override
        public HitCounts getHitCounts() {
            return new HitCounts(1, 1, 1, 1, 1,
                    getCoverage().toLoggingCoverage());
        }

        @Override
        public Timing getTiming() {
            return null;
        }

        @Override
        public Coverage getCoverage() {
            return new Coverage(1, 1, true);
        }


        @Override
        public void render(OutputStream output, ContentChannel networkChannel,
                CompletionHandler handler) throws IOException {
            networkChannel.close(handler);
        }
    }

    static class CloseableContentChannel implements ContentChannel {

        @Override
        public void write(ByteBuffer buf, CompletionHandler handler) {
            if (handler != null) {
                handler.completed();
            }
        }

        @Override
        public void close(CompletionHandler handler) {
            if (handler != null) {
                handler.completed();
            }
        }

    }

    public static final class MockResponseHandler implements ResponseHandler {
        public final ContentChannel channel = new CloseableContentChannel();

        @Override
        public ContentChannel handleResponse(
                final com.yahoo.jdisc.Response response) {
            return channel;
        }
    }

    static final class AccessLogTestHandler extends ThreadedHttpRequestHandler {

        public AccessLogTestHandler(Executor executor) {
            super(executor);
        }

        @Override
        public HttpResponse handle(HttpRequest request) {
            return new NoTimingResponse();
        }

    }

    @BeforeEach
    public void setUp() throws Exception {
        executor = Executors.newCachedThreadPool();
        handler = new AccessLogTestHandler(executor);
    }

    @AfterEach
    public void tearDown() throws Exception {
        handler = null;
        executor.shutdown();
        executor = null;
    }

    public static com.yahoo.jdisc.http.HttpRequest createRequest() {
        return createRequest("http://localhost/search/?query=geewhiz");
    }

    public static com.yahoo.jdisc.http.HttpRequest createRequest(String uri) {
        com.yahoo.jdisc.http.HttpRequest request = null;
        try {
            request = com.yahoo.jdisc.http.HttpRequest.newClientRequest(new com.yahoo.jdisc.Request(new MockCurrentContainer(), new URI(uri)), new URI(uri),
                                                   com.yahoo.jdisc.http.HttpRequest.Method.GET, com.yahoo.jdisc.http.HttpRequest.Version.HTTP_1_1);
            request.setRemoteAddress(new InetSocketAddress(0));
        } catch (URISyntaxException e) {
            fail("Illegal URI string in test?");
        }
        return request;
    }

    private static class MockCurrentContainer implements CurrentContainer {
        @Override
        public Container newReference(java.net.URI uri) {
            return new Container() {

                @Override
                public RequestHandler resolveHandler(com.yahoo.jdisc.Request request) {
                    return null;
                }

                @Override
                public <T> T getInstance(Class<T> tClass) {
                    return null;
                }

                @Override
                public ResourceReference refer() {
                    return References.NOOP_REFERENCE;
                }

                @Override
                public void release() {
                    // NOP
                }

                @Override
                public long currentTimeMillis() {
                    return 37;
                }
            };
        }
    }

}