cjstehno / ersatz

🤖 A simulated HTTP server for testing client code with configurable responses.
https://cjstehno.github.io/ersatz
Apache License 2.0
47 stars 5 forks source link

Https support #8

Closed cjstehno closed 7 years ago

cjstehno commented 7 years ago

Add support for configuring HTTPS endpoints either via extension or other means.

cjstehno commented 7 years ago

After much pain... I have a working example of HTTPS in Undertow:

@Grapes(
    @Grab('io.undertow:undertow-core:1.4.7.Final')
)

// based on: http://stackoverflow.com/questions/26595745/undertow-https-listener
// create keystore with: https://tomcat.apache.org/tomcat-8.5-doc/ssl-howto.html

import io.undertow.*
import io.undertow.util.*
import io.undertow.server.*

import javax.net.ssl.*
import java.security.*

class UndertowHttpsTest {

    static void main(String[] args) throws Exception {

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(getKeyManagers(), null, null); 

        Undertow.builder()
                .addHttpListener(8080, "localhost")
                .addHttpsListener(443, 'localhost', sslContext)
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServerExchange exchange)throws Exception {
                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE,"text/plain");
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build().start();
    }

    private static KeyManager[] getKeyManagers() {
        try {
            KeyStore keyStore = KeyStore.getInstance("JKS");
            keyStore.load(new FileInputStream("c:/Users/stehnoc/.keystore"), "underpass".toCharArray());

            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(keyStore, "underpass".toCharArray());
            return keyManagerFactory.getKeyManagers();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

I should be able to base the HTTPS support on this example.

cjstehno commented 7 years ago

Implemented in ssl-support branch.