CodeSVby CA Technologies

HTTPs

< Back

Prerequisites

Using HTTPs protocol

CodeSV supports the HTTPs protocol that is easily enabled by using the usingHttps() method. There are three required parameters to be specified:

  • keystorePath points to the location of the keystore on the file system.
  • keystorePassword specifies a password for the provided keystore.
  • keyPassword specifies the password to a key.

Note: In the provided example, we are using our own custom trust manager to trust all communication with common name localhost (CN=localhost). In a real use case scenario you should use another trust manager.

HTTPs example

private static final String KEYSTORE_PATH = HttpsExample.class.getClassLoader()
      .getResource("ssl/keystore.jks").getPath();
  private static final String KEYSTORE_PASSWORD = "password";
  private static final String BODY_PLAIN_TEXT = "Success";
  private static final String BASE_URL_SSL = "https://localhost:8090/";
  private static final int HTTP_STATUS_OK_NUMB = 200;

  @Rule
  public VirtualServerRule vs = new VirtualServerRule();

  private TrustManager[] trustAllCerts;
  private SSLContext sslContext;

  /**
   * Preparing to trust everything with the common name localhost.
   */
  @Before
  public void setUp() throws Exception {
    trustAllCerts = new TrustManager[]{
        new X509TrustManager() {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
          }

          public void checkClientTrusted(
              java.security.cert.X509Certificate[] certs, String authType) {
          }

          public void checkServerTrusted(
              java.security.cert.X509Certificate[] certs, String authType) {
            if (certs.length != 1 || !certs[0].getIssuerX500Principal().getName()
                .contains("CN=localhost")) {
              throw new SecurityException("Invalid certificate");
            }
          }
        }
    };

    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustAllCerts, new SecureRandom());
  }

  @Test(timeout = 10000L)
  public void testHttpsConnection() throws IOException {
    forGet(BASE_URL_SSL)
        .usingHttps(
            withSecureProtocol("TLS")
                .keystorePath(KEYSTORE_PATH)
                .keystorePassword(KEYSTORE_PASSWORD)
                .keyPassword(KEYSTORE_PASSWORD))
        .doReturn(
            okMessage()
                .withStringBody(BODY_PLAIN_TEXT)
                .withContentType(HttpConstants.PLAIN_TEXT));

    URL u = new URL(BASE_URL_SSL);
    HttpsURLConnection connection = (HttpsURLConnection) u.openConnection();
    connection.setSSLSocketFactory(sslContext.getSocketFactory());
    
    assertEquals(HTTP_STATUS_OK_NUMB, connection.getResponseCode());
    assertEquals(BODY_PLAIN_TEXT,
        IOUtils.toString((InputStream) connection.getContent()));
  }

For a complete example see: HTTPs Example

Required KeyStore file: keystore.jks