PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/rest-assured-itest-java/src/test/java/io/restassured/itest/java/SSLITest.java

http://github.com/jayway/rest-assured
Java | 261 lines | 202 code | 42 blank | 17 comment | 0 complexity | bb980c8e7561c458d476ee4b8adb1055 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2019 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.restassured.itest.java;
  17. import io.restassured.RestAssured;
  18. import io.restassured.authentication.CertificateAuthSettings;
  19. import io.restassured.builder.RequestSpecBuilder;
  20. import io.restassured.builder.ResponseSpecBuilder;
  21. import io.restassured.config.RestAssuredConfig;
  22. import io.restassured.config.SSLConfig;
  23. import io.restassured.itest.java.support.WithJetty;
  24. import io.restassured.specification.RequestSpecification;
  25. import io.restassured.specification.ResponseSpecification;
  26. import org.junit.Ignore;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import javax.net.ssl.SSLException;
  31. import javax.net.ssl.SSLHandshakeException;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.security.KeyStore;
  35. import static org.hamcrest.Matchers.containsString;
  36. import static org.hamcrest.Matchers.equalTo;
  37. public class SSLITest extends WithJetty {
  38. @Rule
  39. public ExpectedException exception = ExpectedException.none();
  40. public static ResponseSpecification helloWorldSpec() {
  41. return new ResponseSpecBuilder().
  42. expectBody("hello", equalTo("Hello Scalatra")).
  43. expectStatusCode(200).build();
  44. }
  45. @Test(expected = SSLException.class)
  46. public void throwsSSLExceptionWhenHostnameInCertDoesntMatch() throws Exception {
  47. RestAssured.get("https://localhost:8443/hello");
  48. }
  49. @Test
  50. public void givenTrustStoreDefinedStaticallyWhenSpecifyingJksKeyStoreFileWithCorrectPasswordAllowsToUseSSL() throws Exception {
  51. RestAssured.trustStore("jetty_localhost_client.jks", "test1234");
  52. try {
  53. RestAssured.expect().spec(helloWorldSpec()).when().get("https://localhost:8443/hello");
  54. } finally {
  55. RestAssured.reset();
  56. }
  57. }
  58. @Test(expected = SSLHandshakeException.class)
  59. public void whenEnablingAllowAllHostNamesVerifierWithoutActivatingAKeyStore() throws Exception {
  60. RestAssured.config = RestAssuredConfig.config().sslConfig(SSLConfig.sslConfig().allowAllHostnames());
  61. try {
  62. RestAssured.get("https://localhost:8443/hello").then().spec(helloWorldSpec());
  63. } finally {
  64. RestAssured.reset();
  65. }
  66. }
  67. @Test
  68. public void usingStaticallyConfiguredCertificateAuthenticationWorks() throws Exception {
  69. RestAssured.authentication = RestAssured.certificate("jetty_localhost_client.jks", "test1234", CertificateAuthSettings.certAuthSettings().allowAllHostnames());
  70. try {
  71. RestAssured.get("https://localhost:8443/hello").then().spec(helloWorldSpec());
  72. } finally {
  73. RestAssured.reset();
  74. }
  75. }
  76. @Test(expected = SSLException.class)
  77. public void usingStaticallyConfiguredCertificateAuthenticationWithIllegalHostNameInCertDoesntWork() throws Exception {
  78. RestAssured.authentication = RestAssured.certificate("truststore_mjvmobile.jks", "test4321");
  79. try {
  80. RestAssured.get("https://localhost:8443/hello").then().body(containsString("eurosport"));
  81. } finally {
  82. RestAssured.reset();
  83. }
  84. }
  85. @Test
  86. public void usingStaticallyConfiguredCertificateAuthenticationWithIllegalHostNameInCertWorksWhenSSLConfigIsConfiguredToAllowAllHostNames() throws Exception {
  87. RestAssured.config = RestAssuredConfig.newConfig().sslConfig(SSLConfig.sslConfig().allowAllHostnames());
  88. RestAssured.authentication = RestAssured.certificate("jetty_localhost_client.jks", "test1234");
  89. try {
  90. RestAssured.get("https://localhost:8443/hello").then().spec(helloWorldSpec());
  91. } finally {
  92. RestAssured.reset();
  93. }
  94. }
  95. @Test
  96. public void givenKeystoreDefinedUsingGivenWhenSpecifyingJksKeyStoreFileWithCorrectPasswordAllowsToUseSSL() throws Exception {
  97. RestAssured.given().trustStore("/jetty_localhost_client.jks", "test1234").then().expect().spec(helloWorldSpec()).when().get("https://localhost:8443/hello");
  98. }
  99. @Test
  100. public void throwsIOExceptionWhenPasswordIsIncorrect() throws Exception {
  101. exception.expect(IOException.class);
  102. exception.expectMessage("Keystore was tampered with, or password was incorrect");
  103. RestAssured.given().
  104. auth().certificate("jetty_localhost_client.jks", "test4333").
  105. when().
  106. get("https://localhost:8443/hello").
  107. then().
  108. body(containsString("eurosport"));
  109. }
  110. @Test
  111. public void certificateAuthenticationWorks() throws Exception {
  112. RestAssured.given().
  113. auth().certificate("jetty_localhost_client.jks", "test1234", CertificateAuthSettings.certAuthSettings().allowAllHostnames()).
  114. when().
  115. get("https://localhost:8443/hello").
  116. then().
  117. spec(helloWorldSpec());
  118. }
  119. @Test public void
  120. allows_specifying_trust_store_in_dsl() throws Exception {
  121. InputStream keyStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jetty_localhost_client.jks");
  122. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  123. keyStore.load(keyStoreStream, "test1234".toCharArray());
  124. RestAssured.given().config(RestAssuredConfig.config().sslConfig(SSLConfig.sslConfig().allowAllHostnames())).trustStore(keyStore).when().get("https://localhost:8443/hello").then().statusCode(200);
  125. }
  126. @Ignore("Temporary ignored but I think this ought to work. Perhaps some issues with config merging?")
  127. @Test public void
  128. allows_specifying_trust_store_statically() throws Exception {
  129. InputStream keyStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jetty_localhost_client.jks");
  130. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  131. keyStore.load(keyStoreStream, "test1234".toCharArray());
  132. RestAssured.trustStore(keyStore);
  133. try {
  134. RestAssured.given().config(RestAssuredConfig.config().sslConfig(SSLConfig.sslConfig().allowAllHostnames())).when().get("https://localhost:8443/hello").then().statusCode(200);
  135. } finally {
  136. RestAssured.reset();
  137. }
  138. }
  139. @Test public void
  140. allows_specifying_trust_store_and_allow_all_host_names_in_config_using_dsl() throws Exception {
  141. InputStream keyStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jetty_localhost_client.jks");
  142. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  143. keyStore.load(keyStoreStream, "test1234".toCharArray());
  144. RestAssured.given().config(RestAssuredConfig.config().sslConfig(SSLConfig.sslConfig().trustStore(keyStore).and().allowAllHostnames())).when().get("https://localhost:8443/hello").then().spec(helloWorldSpec());
  145. }
  146. @Test public void
  147. relaxed_https_validation_works_using_instance_config() {
  148. RestAssured.given().config(RestAssuredConfig.config().sslConfig(SSLConfig.sslConfig().relaxedHTTPSValidation())).when().get("https://localhost:8443/hello").then().spec(helloWorldSpec());
  149. }
  150. @Test public void
  151. relaxed_https_validation_works_using_instance_dsl() {
  152. RestAssured.given().relaxedHTTPSValidation().when().get("https://bunny.cloudamqp.com/api/").then().statusCode(200);
  153. }
  154. @Test public void
  155. relaxed_https_validation_works_when_defined_statically() {
  156. RestAssured.useRelaxedHTTPSValidation();
  157. try {
  158. RestAssured.get("https://bunny.cloudamqp.com/api/").then().statusCode(200);
  159. } finally {
  160. RestAssured.reset();
  161. }
  162. }
  163. @Test public void
  164. relaxed_https_validation_works_when_defined_statically_with_base_uri() {
  165. RestAssured.useRelaxedHTTPSValidation();
  166. RestAssured.baseURI = "https://bunny.cloudamqp.com";
  167. try {
  168. RestAssured.get("/api/").then().statusCode(200);
  169. } finally {
  170. RestAssured.reset();
  171. }
  172. }
  173. @Test public void
  174. truststore_works_with_static_base_uri() {
  175. RestAssured.baseURI = "https://localhost:8443/hello";
  176. try {
  177. RestAssured.given().trustStore("/jetty_localhost_client.jks", "test1234").when().get().then().spec(helloWorldSpec());
  178. } finally {
  179. RestAssured.reset();
  180. }
  181. }
  182. @Ignore("Temporary ignored since site has changed") @Test public void
  183. truststrore_works_with_static_base_uri() throws Exception{
  184. RestAssured.baseURI = "https://bunny.cloudamqp.com/";
  185. InputStream keyStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("truststore_cloudamqp.jks");
  186. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  187. keyStore.load(keyStoreStream, "cloud1234".toCharArray());
  188. try {
  189. RestAssured.given().trustStore(keyStore).when().get("/api/").then().statusCode(200);
  190. } finally {
  191. RestAssured.reset();
  192. }
  193. }
  194. @Test public void
  195. can_make_request_to_sites_that_with_valid_ssl_cert() {
  196. RestAssured.get("https://duckduckgo.com/").then().statusCode(200);
  197. }
  198. @Test public void
  199. allows_specifying_trust_store_statically_with_request_builder() throws Exception {
  200. // Load the trust store
  201. InputStream trustStoreStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("jetty_localhost_client.jks");
  202. KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
  203. trustStore.load(trustStoreStream, "test1234".toCharArray());
  204. // Set the truststore on the global config
  205. RestAssured.config = RestAssured.config().sslConfig(SSLConfig.sslConfig().trustStore(trustStore).and().allowAllHostnames());
  206. final RequestSpecification spec = new RequestSpecBuilder().build();
  207. RestAssured.given().spec(spec).get("https://localhost:8443/hello").then().spec(helloWorldSpec());
  208. }
  209. @Test public void
  210. supports_setting_truststore_in_request_specification() {
  211. final RequestSpecification spec = new RequestSpecBuilder().setTrustStore("/jetty_localhost_client.jks", "test1234").build();
  212. RestAssured.given().spec(spec).expect().spec(helloWorldSpec()).when().get("https://localhost:8443/hello");
  213. }
  214. @Test public void
  215. supports_overriding_truststore_in_request_specification() {
  216. final RequestSpecification spec = new RequestSpecBuilder().setTrustStore("/jetty_localhost_client.jks", "wrong pw").build();
  217. RestAssured.given().spec(spec).trustStore("/jetty_localhost_client.jks", "test1234").expect().spec(helloWorldSpec()).when().get("https://localhost:8443/hello");
  218. }
  219. }