|
2 | 2 |
|
3 | 3 | import com.marklogic.client.DatabaseClient;
|
4 | 4 | import com.marklogic.client.DatabaseClientFactory;
|
| 5 | +import com.marklogic.client.ForbiddenUserException; |
5 | 6 | import com.marklogic.client.MarkLogicIOException;
|
6 | 7 | import com.marklogic.client.test.junit5.RequireSSLExtension;
|
7 | 8 | import org.junit.jupiter.api.Test;
|
8 | 9 | import org.junit.jupiter.api.extension.ExtendWith;
|
9 | 10 |
|
10 | 11 | import javax.net.ssl.SSLContext;
|
| 12 | +import javax.net.ssl.SSLHandshakeException; |
11 | 13 | import javax.net.ssl.TrustManager;
|
12 | 14 |
|
13 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
14 | 16 | import static org.junit.jupiter.api.Assertions.assertNull;
|
15 | 17 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
| 18 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
16 | 19 |
|
17 | 20 | @ExtendWith(RequireSSLExtension.class)
|
18 | 21 | class CheckSSLConnectionTest {
|
@@ -58,8 +61,34 @@ void defaultSslContext() throws Exception {
|
58 | 61 | .withSSLHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.ANY)
|
59 | 62 | .build();
|
60 | 63 |
|
61 |
| - assertThrows(MarkLogicIOException.class, () -> client.checkConnection(), |
| 64 | + MarkLogicIOException ex = assertThrows(MarkLogicIOException.class, () -> client.checkConnection(), |
62 | 65 | "The connection should fail because the JVM's default SSL Context does not have a CA certificate that " +
|
63 | 66 | "corresponds to the test-only certificate that the app server is using for this test");
|
| 67 | + |
| 68 | + assertTrue(ex.getCause() instanceof SSLHandshakeException, "Unexpected cause: " + ex.getCause()); |
| 69 | + String message = ex.getCause().getMessage(); |
| 70 | + assertTrue(message.contains("PKIX path building failed"), "The call should have failed because the JVM's " + |
| 71 | + "default SSL context does not have a CA certificate for the app server's certificate; " + |
| 72 | + "unexpected error: " + message); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void noSslContext() { |
| 77 | + DatabaseClient client = Common.newClientBuilder().build(); |
| 78 | + |
| 79 | + DatabaseClient.ConnectionResult result = client.checkConnection(); |
| 80 | + assertEquals("Forbidden", result.getErrorMessage(), "MarkLogic is expected to return a 403 Forbidden when the " + |
| 81 | + "user tries to access an HTTPS app server using HTTP"); |
| 82 | + assertEquals(403, result.getStatusCode()); |
| 83 | + |
| 84 | + ForbiddenUserException ex = assertThrows(ForbiddenUserException.class, |
| 85 | + () -> client.newServerEval().javascript("fn.currentDate()").evalAs(String.class)); |
| 86 | + |
| 87 | + assertEquals( |
| 88 | + "Local message: User is not allowed to apply resource at eval. Server Message: You have attempted to access an HTTPS server using HTTP.", |
| 89 | + ex.getMessage(), |
| 90 | + "The user should get a clear message on why the connection failed as opposed to the previous error " + |
| 91 | + "message of 'Server (not a REST instance?)'." |
| 92 | + ); |
64 | 93 | }
|
65 | 94 | }
|
0 commit comments