Sample Spring Test Client for Https

 

Sample Test Client for https:

 

@Test
	public void testAuth() throws Exception {

		TestRestTemplate restTemplate = new TestRestTemplate();
		restTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory(
			HttpClients.custom().setSSLSocketFactory(socketFactory()).build()));

		ResponseEntity httpsEntity = restTemplate.getForEntity("https://<your id>:<https port>/greeting",
				String.class);

		assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
	    assertThat(httpsEntity.getBody()).containsIgnoringCase("nahid");

	}

 

 

Sample Socket Factory example

 

	private SSLConnectionSocketFactory socketFactory() throws Exception {
		char[] password = "changeit".toCharArray();		
		KeyStore truststore = KeyStore.getInstance("JKS");
		truststore.load(new ClassPathResource("truststore.jks").getInputStream(), password);
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadKeyMaterial(truststore, password);
		builder.loadTrustMaterial(truststore, new TrustSelfSignedStrategy());
		return new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
	}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s