Skip to content

Commit 9a9fe41

Browse files
committed
Adapt integration test to use recent HttpClient version
See gh-1321
1 parent 36cf1dd commit 9a9fe41

File tree

2 files changed

+63
-66
lines changed

2 files changed

+63
-66
lines changed

spring-ws-support/build.gradle

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ dependencies {
2626
testImplementation("com.icegreen:greenmail")
2727
testImplementation("com.icegreen:greenmail-junit5")
2828
testImplementation("com.icegreen:greenmail-spring")
29-
testImplementation("commons-httpclient:commons-httpclient") {
30-
exclude(group: "commons-logging", module: "commons-logging")
31-
}
3229
testImplementation("jakarta.annotation:jakarta.annotation-api")
3330
testImplementation("org.apache.activemq:activemq-broker") {
3431
exclude(group: "commons-logging", module: "commons-logging")
3532
}
33+
testImplementation("org.apache.httpcomponents.client5:httpclient5")
3634
testImplementation("org.apache.logging.log4j:log4j-core")
3735
testImplementation("org.apache.logging.log4j:log4j-slf4j2-impl")
3836
testImplementation("org.assertj:assertj-core")

spring-ws-support/src/test/java/org/springframework/ws/transport/http/WebServiceHttpHandlerIntegrationTest.java

+62-63
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818

1919
import java.io.IOException;
2020

21-
import org.apache.commons.httpclient.HttpClient;
22-
import org.apache.commons.httpclient.methods.GetMethod;
23-
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
24-
import org.apache.commons.httpclient.methods.PostMethod;
25-
import org.junit.jupiter.api.BeforeEach;
21+
import org.apache.hc.client5.http.classic.methods.HttpGet;
22+
import org.apache.hc.client5.http.classic.methods.HttpPost;
23+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
24+
import org.apache.hc.client5.http.impl.classic.HttpClients;
25+
import org.apache.hc.core5.http.ClassicHttpRequest;
26+
import org.apache.hc.core5.http.ClassicHttpResponse;
27+
import org.apache.hc.core5.http.ContentType;
28+
import org.apache.hc.core5.http.HttpHeaders;
29+
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
30+
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
31+
import org.assertj.core.api.ThrowingConsumer;
2632
import org.junit.jupiter.api.Test;
2733
import org.junit.jupiter.api.extension.ExtendWith;
2834

@@ -39,90 +45,83 @@
3945
@ContextConfiguration("httpserver-applicationContext.xml")
4046
public class WebServiceHttpHandlerIntegrationTest {
4147

42-
private HttpClient client;
43-
4448
@Autowired
4549
private int port;
4650

47-
private String url;
48-
49-
@BeforeEach
50-
public void createHttpClient() {
51-
52-
this.client = new HttpClient();
53-
this.url = "http://localhost:" + this.port + "/service";
54-
}
55-
5651
@Test
57-
public void testInvalidMethod() throws IOException {
58-
59-
GetMethod getMethod = new GetMethod(this.url);
60-
this.client.executeMethod(getMethod);
61-
62-
assertThat(getMethod.getStatusCode()).isEqualTo(HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED);
63-
assertThat(getMethod.getResponseContentLength()).isEqualTo(0);
52+
public void testInvalidMethod() {
53+
HttpGet httpRequest = new HttpGet(serviceUrl());
54+
execute(httpRequest, response -> {
55+
assertThat(response.getCode()).isEqualTo(HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED);
56+
assertThat(response.containsHeader(HttpHeaders.CONTENT_LENGTH)).isTrue();
57+
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH).getValue()).isEqualTo("0");
58+
});
6459
}
6560

6661
@Test
6762
public void testNoResponse() throws IOException {
68-
69-
PostMethod postMethod = new PostMethod(this.url);
70-
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
71-
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
72-
"http://springframework.org/spring-ws/NoResponse");
63+
HttpPost httpRequest = new HttpPost(serviceUrl());
64+
httpRequest.addHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/NoResponse");
7365
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
74-
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
75-
76-
this.client.executeMethod(postMethod);
77-
78-
assertThat(postMethod.getStatusCode()).isEqualTo(HttpTransportConstants.STATUS_ACCEPTED);
79-
assertThat(postMethod.getResponseContentLength()).isEqualTo(0);
66+
httpRequest.setEntity(new InputStreamEntity(soapRequest.getInputStream(), ContentType.TEXT_XML));
67+
execute(httpRequest, response -> {
68+
assertThat(response.getCode()).isEqualTo(HttpTransportConstants.STATUS_ACCEPTED);
69+
assertThat(response.containsHeader(HttpHeaders.CONTENT_LENGTH)).isTrue();
70+
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH).getValue()).isEqualTo("0");
71+
});
8072
}
8173

8274
@Test
8375
public void testResponse() throws IOException {
84-
85-
PostMethod postMethod = new PostMethod(this.url);
86-
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
87-
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
88-
"http://springframework.org/spring-ws/Response");
76+
HttpPost httpRequest = new HttpPost(serviceUrl());
77+
httpRequest.addHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Response");
8978
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
90-
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
91-
this.client.executeMethod(postMethod);
92-
93-
assertThat(postMethod.getStatusCode()).isEqualTo(HttpTransportConstants.STATUS_OK);
94-
assertThat(postMethod.getResponseContentLength()).isGreaterThan(0);
79+
httpRequest.setEntity(new InputStreamEntity(soapRequest.getInputStream(), ContentType.TEXT_XML));
80+
execute(httpRequest, response -> {
81+
assertThat(response.getCode()).isEqualTo(HttpTransportConstants.STATUS_OK);
82+
assertThat(response.containsHeader(HttpHeaders.CONTENT_LENGTH)).isTrue();
83+
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH).getValue()).asInt().isGreaterThan(0);
84+
});
9585
}
9686

9787
@Test
9888
public void testNoEndpoint() throws IOException {
99-
100-
PostMethod postMethod = new PostMethod(this.url);
101-
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
102-
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
103-
"http://springframework.org/spring-ws/NoEndpoint");
89+
HttpPost httpRequest = new HttpPost(serviceUrl());
90+
httpRequest.addHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/NoEndpoint");
10491
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
105-
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
106-
107-
this.client.executeMethod(postMethod);
108-
109-
assertThat(postMethod.getStatusCode()).isEqualTo(HttpTransportConstants.STATUS_NOT_FOUND);
110-
assertThat(postMethod.getResponseContentLength()).isEqualTo(0);
92+
httpRequest.setEntity(new InputStreamEntity(soapRequest.getInputStream(), ContentType.TEXT_XML));
93+
execute(httpRequest, response -> {
94+
assertThat(response.getCode()).isEqualTo(HttpTransportConstants.STATUS_NOT_FOUND);
95+
assertThat(response.containsHeader(HttpHeaders.CONTENT_LENGTH)).isTrue();
96+
assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH).getValue()).isEqualTo("0");
97+
});
11198
}
11299

113100
@Test
114101
public void testFault() throws IOException {
115-
116-
PostMethod postMethod = new PostMethod(this.url);
117-
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
118-
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
119-
"http://springframework.org/spring-ws/Fault");
102+
HttpPost httpRequest = new HttpPost(serviceUrl());
103+
httpRequest.addHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Fault");
120104
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
121-
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
105+
httpRequest.setEntity(new InputStreamEntity(soapRequest.getInputStream(), ContentType.TEXT_XML));
106+
execute(httpRequest, response -> assertThat(response.getCode())
107+
.isEqualTo(HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR));
108+
}
122109

123-
this.client.executeMethod(postMethod);
110+
private String serviceUrl() {
111+
return "http://localhost:%s/service".formatted(this.port);
112+
}
124113

125-
assertThat(postMethod.getStatusCode()).isEqualTo(HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR);
114+
private void execute(ClassicHttpRequest request, ThrowingConsumer<ClassicHttpResponse> responseHandler) {
115+
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
116+
HttpClientResponseHandler<Object> rh = httpResponse -> {
117+
responseHandler.accept(httpResponse);
118+
return null;
119+
};
120+
httpclient.execute(request, rh);
121+
}
122+
catch (IOException ex) {
123+
throw new IllegalStateException(ex);
124+
}
126125
}
127126

128127
}

0 commit comments

Comments
 (0)