BAEL-5483 - Java HttpClient with SSL (#12144)

* BAEL-5483 - Java HttpClient with SSL

* BAEL-5483 - Java HttpClient with SSL

* BAEL-5483 - Java HttpClient with SSL - changing test case url

* BAEL-5483 - Two space indentation for line continuation

Co-authored-by: Abhinav Pandey <Abhinav2.Pandey@airtel.com>
This commit is contained in:
Abhinav Pandey 2022-05-08 10:41:49 +05:30 committed by GitHub
parent 1ce8895891
commit d8b4f64525
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.httpclient.ssl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Properties;
public class HttpClientSSLBypassUnitTest {
@Test
public void whenHttpsRequest_thenCorrect() throws IOException, InterruptedException {
final Properties props = System.getProperties();
props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.TRUE.toString());
HttpClient httpClient = HttpClient.newBuilder()
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.testingmcafeesites.com/"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
props.setProperty("jdk.internal.httpclient.disableHostnameVerification", Boolean.FALSE.toString());
Assertions.assertEquals(200, response.statusCode());
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.httpclient.ssl;
import org.junit.Test;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import static org.junit.Assert.assertEquals;
public class HttpClientSSLUnitTest {
@Test
public void whenValidHttpsRequest_thenCorrect() throws URISyntaxException, IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://www.google.com/"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
}
@Test(expected = SSLHandshakeException.class)
public void whenInvalidHttpsRequest_thenInCorrect() throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://expired.badssl.com/"))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
}
}