-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathSslTest.java
More file actions
65 lines (54 loc) · 2.32 KB
/
Copy pathSslTest.java
File metadata and controls
65 lines (54 loc) · 2.32 KB
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
package org.webbitserver.handler;
import org.junit.BeforeClass;
import org.junit.Test;
import org.webbitserver.WebServer;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import static org.junit.Assert.assertEquals;
import static org.webbitserver.WebServers.createWebServer;
import static org.webbitserver.testutil.HttpClient.contents;
import static org.webbitserver.testutil.HttpClient.httpsGet;
public class SslTest {
@Test
public void setsSecureHttpsServerHeader() throws Exception {
InputStream keystore = getClass().getResourceAsStream("/ssl/keystore");
WebServer webServer = createWebServer(10443)
.setupSsl(keystore, "webbit")
.add(new ServerHeaderHandler("My Server"))
.add(new StringHttpHandler("text/plain", "body"));
keystore.close();
webServer.start();
try {
HttpsURLConnection urlConnection = httpsGet(webServer, "/");
assertEquals("My Server", urlConnection.getHeaderField("Server"));
assertEquals("body", contents(urlConnection));
} finally {
webServer.stop().get();
}
}
@BeforeClass
public static void disableCertValidationSetUp() throws NoSuchAlgorithmException, KeyManagementException {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
}