1- package com .baeldung .java9 .httpclient ;
2-
3-
1+ package com .baeldung .java9 .httpclient ;
42
53import static java .net .HttpURLConnection .HTTP_OK ;
64import static org .junit .Assert .assertTrue ;
2826
2927public class SimpleHttpRequestsTest {
3028
31- private URI httpURI ;
32-
29+ private URI httpURI ;
30+
3331 @ Before
3432 public void init () throws URISyntaxException {
3533 httpURI = new URI ("http://www.baeldung.com/" );
3634 }
3735
3836 @ Test
3937 public void quickGet () throws IOException , InterruptedException , URISyntaxException {
40- HttpRequest request = HttpRequest .create ( httpURI ).GET ();
38+ HttpRequest request = HttpRequest .create (httpURI ).GET ();
4139 HttpResponse response = request .response ();
4240 int responseStatusCode = response .statusCode ();
4341 String responseBody = response .body (HttpResponse .asString ());
44- assertTrue ("Get response status code is bigger then 400" , responseStatusCode < 400 );
42+ assertTrue ("Get response status code is bigger then 400" , responseStatusCode < 400 );
4543 }
46-
44+
4745 @ Test
48- public void asynchronousGet () throws URISyntaxException , IOException , InterruptedException , ExecutionException {
46+ public void asynchronousGet () throws URISyntaxException , IOException , InterruptedException , ExecutionException {
4947 HttpRequest request = HttpRequest .create (httpURI ).GET ();
5048 long before = System .currentTimeMillis ();
5149 CompletableFuture <HttpResponse > futureResponse = request .responseAsync ();
52- futureResponse .thenAccept ( response -> {
50+ futureResponse .thenAccept (response -> {
5351 String responseBody = response .body (HttpResponse .asString ());
54- });
52+ });
5553 HttpResponse resp = futureResponse .get ();
5654 HttpHeaders hs = resp .headers ();
57- assertTrue ("There should be more then 1 header." , hs .map ().size () >1 );
55+ assertTrue ("There should be more then 1 header." , hs .map ().size () > 1 );
5856 }
59-
57+
6058 @ Test
6159 public void postMehtod () throws URISyntaxException , IOException , InterruptedException {
6260 HttpRequest .Builder requestBuilder = HttpRequest .create (httpURI );
@@ -66,61 +64,63 @@ public void postMehtod() throws URISyntaxException, IOException, InterruptedExce
6664 int statusCode = response .statusCode ();
6765 assertTrue ("HTTP return code" , statusCode == HTTP_OK );
6866 }
69-
67+
7068 @ Test
71- public void configureHttpClient () throws NoSuchAlgorithmException , URISyntaxException , IOException , InterruptedException {
69+ public void configureHttpClient () throws NoSuchAlgorithmException , URISyntaxException , IOException , InterruptedException {
7270 CookieManager cManager = new CookieManager ();
7371 cManager .setCookiePolicy (CookiePolicy .ACCEPT_ORIGINAL_SERVER );
74-
75- SSLParameters sslParam = new SSLParameters (new String [] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String [] { "TLSv1.2" });
76-
72+
73+ SSLParameters sslParam = new SSLParameters (new String [] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String [] { "TLSv1.2" });
74+
7775 HttpClient .Builder hcBuilder = HttpClient .create ();
7876 hcBuilder .cookieManager (cManager ).sslContext (SSLContext .getDefault ()).sslParameters (sslParam );
7977 HttpClient httpClient = hcBuilder .build ();
8078 HttpRequest .Builder reqBuilder = httpClient .request (new URI ("https://www.facebook.com" ));
81-
79+
8280 HttpRequest request = reqBuilder .followRedirects (HttpClient .Redirect .ALWAYS ).GET ();
8381 HttpResponse response = request .response ();
8482 int statusCode = response .statusCode ();
8583 assertTrue ("HTTP return code" , statusCode == HTTP_OK );
8684 }
87-
88- SSLParameters getDefaultSSLParameters () throws NoSuchAlgorithmException {
85+
86+ SSLParameters getDefaultSSLParameters () throws NoSuchAlgorithmException {
8987 SSLParameters sslP1 = SSLContext .getDefault ().getSupportedSSLParameters ();
90- String [] proto = sslP1 .getApplicationProtocols ();
91- String [] cifers = sslP1 .getCipherSuites ();
88+ String [] proto = sslP1 .getApplicationProtocols ();
89+ String [] cifers = sslP1 .getCipherSuites ();
9290 System .out .println (printStringArr (proto ));
9391 System .out .println (printStringArr (cifers ));
9492 return sslP1 ;
9593 }
96-
97- String printStringArr (String ... args ) {
98- if (args == null ){
94+
95+ String printStringArr (String ... args ) {
96+ if (args == null ) {
9997 return null ;
10098 }
10199 StringBuilder sb = new StringBuilder ();
102- for (String s : args ){
100+ for (String s : args ) {
103101 sb .append (s );
104102 sb .append ("\n " );
105103 }
106104 return sb .toString ();
107105 }
108-
109- String printHeaders (HttpHeaders h ){
110- if (h == null ){
106+
107+ String printHeaders (HttpHeaders h ) {
108+ if (h == null ) {
111109 return null ;
112110 }
113-
114- StringBuilder sb = new StringBuilder ();
115- Map <String , List <String >> hMap = h .map ();
116- for (String k : hMap .keySet ()){
117- sb .append (k ).append (":" );
118- List <String > l = hMap .get (k );
119- if ( l != null ){
120- l .forEach ( s -> { sb .append (s ).append ("," ); } );
121- }
122- sb .append ("\n " );
123- }
124- return sb .toString ();
111+
112+ StringBuilder sb = new StringBuilder ();
113+ Map <String , List <String >> hMap = h .map ();
114+ for (String k : hMap .keySet ()) {
115+ sb .append (k ).append (":" );
116+ List <String > l = hMap .get (k );
117+ if (l != null ) {
118+ l .forEach (s -> {
119+ sb .append (s ).append ("," );
120+ });
121+ }
122+ sb .append ("\n " );
123+ }
124+ return sb .toString ();
125125 }
126126}
0 commit comments