aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/servlet/ServletTestBase.java
blob: 54bfe8c026d8bd4d5e31d28cedc097b631f9021b (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty.servlet;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.handler.AbstractRequestHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.jdisc.http.ServletPathsConfig;
import com.yahoo.jdisc.http.ServletPathsConfig.Servlets.Builder;
import com.yahoo.jdisc.http.server.jetty.SimpleHttpClient.RequestExecutor;
import com.yahoo.jdisc.http.server.jetty.TestDriver;
import org.eclipse.jetty.servlet.ServletHolder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Tony Vaagenes
 * @author bakksjo
 */
public class ServletTestBase {

    private static class ServletInstance {
        final ComponentId componentId; final String path; final HttpServlet instance;

        ServletInstance(ComponentId componentId, String path, HttpServlet instance) {
            this.componentId = componentId;
            this.path = path;
            this.instance = instance;
        }
    }

    private final List<ServletInstance> servlets = List.of(
            new ServletInstance(TestServlet.ID, TestServlet.PATH, new TestServlet()),
            new ServletInstance(NoContentTestServlet.ID, NoContentTestServlet.PATH, new NoContentTestServlet()));

    protected RequestExecutor httpGet(TestDriver testDriver, String path) {
        return testDriver.client().newGet("/" + path);
    }

    protected ServletPathsConfig createServletPathConfig() {
        ServletPathsConfig.Builder configBuilder = new ServletPathsConfig.Builder();

        servlets.forEach(servlet ->
                configBuilder.servlets(
                        servlet.componentId.stringValue(),
                        new Builder().path(servlet.path)));

        return new ServletPathsConfig(configBuilder);
    }

    protected ComponentRegistry<ServletHolder> servlets() {
        ComponentRegistry<ServletHolder> result = new ComponentRegistry<>();

        servlets.forEach(servlet ->
                result.register(servlet.componentId, new ServletHolder(servlet.instance)));

        result.freeze();
        return result;
    }

    protected Module guiceModule() {
        return new AbstractModule() {
            @Override
            protected void configure() {
                bind(new TypeLiteral<ComponentRegistry<ServletHolder>>(){}).toInstance(servlets());
                bind(ServletPathsConfig.class).toInstance(createServletPathConfig());
            }
        };
    }

    protected static class TestServlet extends HttpServlet {
        static final String PATH = "servlet/test-servlet";
        static final ComponentId ID = ComponentId.fromString("test-servlet");
        static final String RESPONSE_CONTENT = "Response from " + TestServlet.class.getSimpleName();

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/plain");
            PrintWriter writer = response.getWriter();
            writer.write(RESPONSE_CONTENT);
            writer.close();
        }
    }

    @WebServlet(asyncSupported = true)
    protected static class NoContentTestServlet extends HttpServlet {
        static final String HEADER_ASYNC = "HEADER_ASYNC";

        static final String PATH = "servlet/no-content-test-servlet";
        static final ComponentId ID = ComponentId.fromString("no-content-test-servlet");

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            if (request.getHeader(HEADER_ASYNC) != null) {
                asyncGet(request);
            }
        }

        private void asyncGet(HttpServletRequest request) {
            request.startAsync().start(() -> {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    log("Interrupted", e);
                } finally {
                    request.getAsyncContext().complete();
                }
            });
        }
    }


    protected static final RequestHandler dummyRequestHandler = new AbstractRequestHandler() {
        @Override
        public ContentChannel handleRequest(Request request, ResponseHandler handler) {
            throw new UnsupportedOperationException();
        }
    };
}