summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/test/ServerTestDriver.java
blob: 17a2b6ee6ee896a6ae74a1cf622fbb9e113cc27b (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.test;

import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.yahoo.jdisc.application.BindingRepository;
import com.yahoo.jdisc.application.ContainerActivator;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.jdisc.http.filter.RequestFilter;
import com.yahoo.jdisc.http.filter.ResponseFilter;
import com.yahoo.jdisc.http.server.jetty.JettyHttpServer;
import com.yahoo.jdisc.http.ssl.SslKeyStore;
import com.yahoo.jdisc.test.TestDriver;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
 */
public class ServerTestDriver {

    private final TestDriver driver;
    private final JettyHttpServer server;
    private final RemoteClient client;

    private ServerTestDriver(TestDriver driver, JettyHttpServer server, RemoteClient client) {
        this.driver = driver;
        this.server = server;
        this.client = client;
    }

    public boolean close() throws IOException {
        client.close();
        server.close();
        server.release();
        return driver.close();
    }

    public TestDriver parent() {
        return driver;
    }

    public ContainerActivator containerActivator() {
        return driver;
    }

    public JettyHttpServer server() {
        return server;
    }

    public RemoteClient client() {
        return client;
    }

    public HttpRequest newRequest(HttpRequest.Method method, String uri, HttpRequest.Version version) {
        return HttpRequest.newServerRequest(driver, newRequestUri(uri), method, version);
    }

    public URI newRequestUri(String uri) {
        return newRequestUri(URI.create(uri));
    }

    public URI newRequestUri(URI uri) {
        try {
            return new URI("http", null, "locahost",
                           server.getListenPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static ServerTestDriver newInstance(RequestHandler requestHandler, Module... guiceModules) throws IOException {
        return newInstance(requestHandler, Arrays.asList(guiceModules));
    }

    public static ServerTestDriver newInstance(RequestHandler requestHandler, Iterable<Module> guiceModules)
            throws IOException {
        List<Module> lst = new LinkedList<>();
        lst.add(newDefaultModule());
        for (Module module : guiceModules) {
            lst.add(module);
        }
        TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(lst.toArray(new Module[lst.size()]));
        ContainerBuilder builder = driver.newContainerBuilder();
        builder.serverBindings().bind("*://*/*", requestHandler);
        JettyHttpServer server = builder.guiceModules().getInstance(JettyHttpServer.class);
        return newInstance(null, driver, builder, server);
    }

    private static ServerTestDriver newInstance(SslKeyStore clientTrustStore, TestDriver driver, ContainerBuilder builder,
                                                JettyHttpServer server) throws IOException {
        builder.serverProviders().install(server);
        driver.activateContainer(builder);
        try {
            server.start();
        } catch (RuntimeException e) {
            server.release();
            driver.close();
            throw e;
        }
        RemoteClient client;
        if (clientTrustStore == null) {
            client = RemoteClient.newInstance(server);
        } else {
            client = RemoteClient.newSslInstance(server, clientTrustStore);
        }
        return new ServerTestDriver(driver, server, client);
    }

    public static Module newDefaultModule() {
        return new AbstractModule() {

            @Override
            protected void configure() {
                bind(new TypeLiteral<BindingRepository<RequestFilter>>() { })
                        .toInstance(new BindingRepository<>());
                bind(new TypeLiteral<BindingRepository<ResponseFilter>>() { })
                        .toInstance(new BindingRepository<>());
            }
        };
    }

    public static Module newFilterModule(final BindingRepository<RequestFilter> requestFilters,
                                         final BindingRepository<ResponseFilter> responseFilters) {
        return new AbstractModule() {

            @Override
            protected void configure() {
                if (requestFilters != null) {
                    bind(new TypeLiteral<BindingRepository<RequestFilter>>() { }).toInstance(requestFilters);
                }
                if (responseFilters != null) {
                    bind(new TypeLiteral<BindingRepository<ResponseFilter>>() { }).toInstance(responseFilters);
                }
            }
        };
    }
}