Skip to content

Commit a60356f

Browse files
authored
Merge branch 'master' into master
2 parents 96f6372 + df3f609 commit a60356f

File tree

264 files changed

+3538
-1118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+3538
-1118
lines changed

apache-httpclient/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.io.IOException;
99
import java.net.ServerSocket;
10-
import java.net.URISyntaxException;
1110

1211
import org.apache.http.HttpStatus;
1312
import org.junit.jupiter.api.AfterAll;
@@ -18,22 +17,19 @@
1817
public class GetRequestMockServer {
1918

2019
public static ClientAndServer mockServer;
21-
public static String serviceOneUrl;
22-
public static String serviceTwoUrl;
2320

2421
public static int serverPort;
2522

2623
public static final String SERVER_ADDRESS = "127.0.0.1";
27-
public static final String PATH_ONE = "/test1";
28-
public static final String PATH_TWO = "/test2";
29-
public static final String METHOD = "GET";
24+
25+
public static final String SECURITY_PATH = "/spring-security-rest-basic-auth/api/foos/1";
26+
27+
public static final String UPLOAD_PATH = "/spring-mvc-java/stub/multipart";
3028

3129
@BeforeAll
3230
static void startServer() throws IOException {
3331
serverPort = getFreePort();
34-
System.out.println("Free port "+serverPort);
35-
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
36-
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
32+
System.out.println("Free port " + serverPort);
3733
mockServer = startClientAndServer(serverPort);
3834
mockGetRequest();
3935
}
@@ -44,33 +40,36 @@ static void stopServer() {
4440
}
4541

4642
private static void mockGetRequest() {
47-
new MockServerClient(SERVER_ADDRESS, serverPort)
48-
.when(
49-
request()
50-
.withPath(PATH_ONE)
51-
.withMethod(METHOD),
52-
exactly(5)
53-
)
54-
.respond(
55-
response()
56-
.withStatusCode(HttpStatus.SC_OK)
57-
.withBody("{\"status\":\"ok\"}")
58-
);
59-
new MockServerClient(SERVER_ADDRESS, serverPort)
60-
.when(
61-
request()
62-
.withPath(PATH_TWO)
63-
.withMethod(METHOD),
64-
exactly(1)
65-
)
66-
.respond(
67-
response()
68-
.withStatusCode(HttpStatus.SC_OK)
69-
.withBody("{\"status\":\"ok\"}")
70-
);
43+
44+
MockServerClient client = new MockServerClient(SERVER_ADDRESS, serverPort);
45+
46+
client.when(
47+
request()
48+
.withPath(SECURITY_PATH)
49+
.withMethod("GET"),
50+
exactly(1)
51+
)
52+
.respond(
53+
response()
54+
.withStatusCode(HttpStatus.SC_OK)
55+
.withBody("{\"status\":\"ok\"}")
56+
);
57+
58+
client.when(
59+
request()
60+
.withPath(UPLOAD_PATH)
61+
.withMethod("POST"),
62+
exactly(4)
63+
)
64+
.respond(
65+
response()
66+
.withStatusCode(HttpStatus.SC_OK)
67+
.withBody("{\"status\":\"ok\"}")
68+
.withHeader("Content-Type", "multipart/form-data")
69+
);
7170
}
7271

73-
private static int getFreePort () throws IOException {
72+
private static int getFreePort() throws IOException {
7473
try (ServerSocket serverSocket = new ServerSocket(0)) {
7574
return serverSocket.getLocalPort();
7675
}

apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientCancelRequestLiveTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HttpClientCancelRequestLiveTest {
1919
void whenRequestIsCanceled_thenCorrect() throws IOException {
2020
HttpGet request = new HttpGet(SAMPLE_URL);
2121
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
22-
httpClient.execute(request, response -> {
22+
httpClient.execute(request, response -> {
2323
HttpEntity entity = response.getEntity();
2424

2525
System.out.println("----------------------------------------");
@@ -28,6 +28,12 @@ void whenRequestIsCanceled_thenCorrect() throws IOException {
2828
System.out.println("Response content length: " + entity.getContentLength());
2929
}
3030
System.out.println("----------------------------------------");
31+
32+
if (entity != null) {
33+
// Closes this stream and releases any system resources
34+
entity.close();
35+
}
36+
3137
// Do not feel like reading the response body
3238
// Call abort on the request object
3339
request.abort();

apache-httpclient/src/test/java/com/baeldung/httpclient/HttpClientMultipartLiveTest.java

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.hamcrest.Matchers.equalTo;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7+
import org.apache.hc.core5.http.ParseException;
78
import org.junit.jupiter.api.BeforeEach;
89
import org.junit.jupiter.api.Test;
910

@@ -13,7 +14,6 @@
1314
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
1415
import org.apache.hc.client5.http.entity.mime.StringBody;
1516
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
16-
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
1717
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
1818

1919
import org.apache.hc.core5.http.ContentType;
@@ -28,9 +28,7 @@
2828
import java.io.InputStreamReader;
2929
import java.net.URL;
3030

31-
import com.baeldung.httpclient.handler.CustomHttpClientResponseHandler;
32-
33-
class HttpClientMultipartLiveTest {
31+
class HttpClientMultipartLiveTest extends GetRequestMockServer {
3432

3533
// No longer available
3634
// private static final String SERVER = "http://echo.200please.com";
@@ -45,13 +43,15 @@ class HttpClientMultipartLiveTest {
4543
@BeforeEach
4644
public void before() {
4745
post = new HttpPost(SERVER);
46+
String URL = "http://localhost:" + serverPort + "/spring-mvc-java/stub/multipart";
47+
post = new HttpPost(URL);
4848
}
4949

5050
@Test
5151
void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() throws IOException {
5252
final URL url = Thread.currentThread()
53-
.getContextClassLoader()
54-
.getResource("uploads/" + TEXTFILENAME);
53+
.getContextClassLoader()
54+
.getResource("uploads/" + TEXTFILENAME);
5555

5656
final File file = new File(url.getPath());
5757
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
@@ -66,27 +66,28 @@ void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() thro
6666
final HttpEntity entity = builder.build();
6767

6868
post.setEntity(entity);
69-
try(CloseableHttpClient client = HttpClientBuilder.create()
70-
.build();
71-
72-
CloseableHttpResponse response = (CloseableHttpResponse) client
73-
.execute(post, new CustomHttpClientResponseHandler())){
74-
final int statusCode = response.getCode();
75-
final String responseString = getContent(response.getEntity());
76-
final String contentTypeInHeader = getContentTypeHeader();
77-
78-
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
79-
assertTrue(contentTypeInHeader.contains("Content-Type: multipart/form-data;"));
80-
System.out.println(responseString);
81-
System.out.println("POST Content Type: " + contentTypeInHeader);
69+
try (CloseableHttpClient client = HttpClientBuilder.create()
70+
.build()) {
71+
72+
client.execute(post, response -> {
73+
final int statusCode = response.getCode();
74+
final String responseString = getContent(response.getEntity());
75+
final String contentTypeInHeader = getContentTypeHeader();
76+
77+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
78+
assertTrue(contentTypeInHeader.contains("multipart/form-data"));
79+
System.out.println(responseString);
80+
System.out.println("POST Content Type: " + contentTypeInHeader);
81+
return response;
82+
});
8283
}
8384
}
8485

8586
@Test
8687
void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExeption() throws IOException {
8788
final URL url = Thread.currentThread()
88-
.getContextClassLoader()
89-
.getResource("uploads/" + TEXTFILENAME);
89+
.getContextClassLoader()
90+
.getResource("uploads/" + TEXTFILENAME);
9091
final File file = new File(url.getPath());
9192
final String message = "This is a multipart post";
9293
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@@ -96,30 +97,31 @@ void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExepti
9697
final HttpEntity entity = builder.build();
9798
post.setEntity(entity);
9899

99-
try(CloseableHttpClient client = HttpClientBuilder.create()
100-
.build();
100+
try (CloseableHttpClient client = HttpClientBuilder.create()
101+
.build()) {
101102

102-
CloseableHttpResponse response = (CloseableHttpResponse) client
103-
.execute(post, new CustomHttpClientResponseHandler())){
103+
client.execute(post, response -> {
104104

105-
final int statusCode = response.getCode();
106-
final String responseString = getContent(response.getEntity());
107-
final String contentTypeInHeader = getContentTypeHeader();
108-
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
109-
assertTrue(contentTypeInHeader.contains("Content-Type: multipart/form-data;"));
110-
System.out.println(responseString);
111-
System.out.println("POST Content Type: " + contentTypeInHeader);
105+
final int statusCode = response.getCode();
106+
final String responseString = getContent(response.getEntity());
107+
final String contentTypeInHeader = getContentTypeHeader();
108+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
109+
assertTrue(contentTypeInHeader.contains("multipart/form-data"));
110+
System.out.println(responseString);
111+
System.out.println("POST Content Type: " + contentTypeInHeader);
112+
return response;
113+
});
112114
}
113115
}
114116

115117
@Test
116118
void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException() throws IOException {
117119
final URL url = Thread.currentThread()
118-
.getContextClassLoader()
119-
.getResource("uploads/" + ZIPFILENAME);
120+
.getContextClassLoader()
121+
.getResource("uploads/" + ZIPFILENAME);
120122
final URL url2 = Thread.currentThread()
121-
.getContextClassLoader()
122-
.getResource("uploads/" + IMAGEFILENAME);
123+
.getContextClassLoader()
124+
.getResource("uploads/" + IMAGEFILENAME);
123125
final InputStream inputStream = new FileInputStream(url.getPath());
124126
final File file = new File(url2.getPath());
125127
final String message = "This is a multipart post";
@@ -131,25 +133,25 @@ void givenFileAndInputStreamandText_whenUploadwithAddBinaryBodyandAddTextBody_Th
131133
final HttpEntity entity = builder.build();
132134
post.setEntity(entity);
133135

134-
try(CloseableHttpClient client = HttpClientBuilder.create()
135-
.build();
136-
137-
CloseableHttpResponse response = (CloseableHttpResponse) client
138-
.execute(post, new CustomHttpClientResponseHandler())){
139-
140-
final int statusCode = response.getCode();
141-
final String responseString = getContent(response.getEntity());
142-
final String contentTypeInHeader = getContentTypeHeader();
143-
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
144-
assertTrue(contentTypeInHeader.contains("Content-Type: multipart/form-data;"));
145-
System.out.println(responseString);
146-
System.out.println("POST Content Type: " + contentTypeInHeader);
147-
inputStream.close();
136+
try (CloseableHttpClient client = HttpClientBuilder.create()
137+
.build()) {
138+
139+
client.execute(post, response -> {
140+
final int statusCode = response.getCode();
141+
final String responseString = getContent(response.getEntity());
142+
final String contentTypeInHeader = getContentTypeHeader();
143+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
144+
assertTrue(contentTypeInHeader.contains("multipart/form-data;"));
145+
System.out.println(responseString);
146+
System.out.println("POST Content Type: " + contentTypeInHeader);
147+
inputStream.close();
148+
return response;
149+
});
148150
}
149151
}
150152

151153
@Test
152-
void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException() throws IOException {
154+
void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoException() throws IOException, ParseException {
153155
final String message = "This is a multipart post";
154156
final byte[] bytes = "binary code".getBytes();
155157
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@@ -159,21 +161,20 @@ void givenCharArrayandText_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExcep
159161
final HttpEntity entity = builder.build();
160162
post.setEntity(entity);
161163

162-
try(CloseableHttpClient client = HttpClientBuilder.create()
163-
.build();
164-
165-
CloseableHttpResponse response = (CloseableHttpResponse) client
166-
.execute(post, new CustomHttpClientResponseHandler())){
167-
168-
final int statusCode = response.getCode();
169-
final String responseString = getContent(response.getEntity());
170-
final String contentTypeInHeader = getContentTypeHeader();
171-
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
172-
assertTrue(contentTypeInHeader.contains("Content-Type: multipart/form-data;"));
173-
System.out.println(responseString);
174-
System.out.println("POST Content Type: " + contentTypeInHeader);
164+
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
165+
.build()) {
166+
167+
httpClient.execute(post, response -> {
168+
final int statusCode = response.getCode();
169+
final String responseString = getContent(response.getEntity());
170+
final String contentTypeInHeader = getContentTypeHeader();
171+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
172+
assertTrue(contentTypeInHeader.contains("multipart/form-data;"));
173+
System.out.println(responseString);
174+
System.out.println("POST Content Type: " + contentTypeInHeader);
175+
return response;
176+
});
175177
}
176-
177178
}
178179

179180
// UTIL

apache-httpclient/src/test/java/com/baeldung/httpclient/base/HttpClientLiveTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final void after() throws IllegalStateException, IOException {
4343

4444
@Test(expected = ConnectTimeoutException.class)
4545
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws IOException {
46-
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
46+
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5).setConnectTimeout(5).setSocketTimeout(2).build();
4747
final HttpGet request = new HttpGet(SAMPLE_URL);
4848
request.setConfig(requestConfig);
4949
response = instance.execute(request);

apache-httpclient/src/test/java/com/baeldung/httpclient/base/HttpClientSandboxLiveTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.httpclient.base;
22

3+
import com.baeldung.httpclient.GetRequestMockServer;
34
import com.baeldung.httpclient.ResponseUtil;
45
import org.apache.http.auth.AuthScope;
56
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -9,14 +10,14 @@
910
import org.apache.http.impl.client.BasicCredentialsProvider;
1011
import org.apache.http.impl.client.CloseableHttpClient;
1112
import org.apache.http.impl.client.HttpClientBuilder;
12-
import org.junit.Test;
13+
import org.junit.jupiter.api.Test;
1314

1415
import java.io.IOException;
1516

1617
/*
1718
* NOTE : Need module spring-security-rest-basic-auth to be running
1819
*/
19-
public class HttpClientSandboxLiveTest {
20+
public class HttpClientSandboxLiveTest extends GetRequestMockServer {
2021

2122
@Test
2223
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
@@ -26,7 +27,7 @@ public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectSt
2627

2728
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
2829

29-
final HttpGet httpGet = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1");
30+
final HttpGet httpGet = new HttpGet("http://localhost:" + serverPort + "/spring-security-rest-basic-auth/api/foos/1");
3031
final CloseableHttpResponse response = client.execute(httpGet);
3132

3233
System.out.println(response.getStatusLine());

apache-httpclient/src/test/java/com/baeldung/httpclient/handler/CustomHttpClientResponseHandler.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)