|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 |
|
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; |
26 | 32 | import org.junit.jupiter.api.Test; |
27 | 33 | import org.junit.jupiter.api.extension.ExtendWith; |
28 | 34 |
|
|
39 | 45 | @ContextConfiguration("httpserver-applicationContext.xml") |
40 | 46 | public class WebServiceHttpHandlerIntegrationTest { |
41 | 47 |
|
42 | | - private HttpClient client; |
43 | | - |
44 | 48 | @Autowired |
45 | 49 | private int port; |
46 | 50 |
|
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 | | - |
56 | 51 | @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 | + }); |
64 | 59 | } |
65 | 60 |
|
66 | 61 | @Test |
67 | 62 | 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"); |
73 | 65 | 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 | + }); |
80 | 72 | } |
81 | 73 |
|
82 | 74 | @Test |
83 | 75 | 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"); |
89 | 78 | 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 | + }); |
95 | 85 | } |
96 | 86 |
|
97 | 87 | @Test |
98 | 88 | 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"); |
104 | 91 | 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 | + }); |
111 | 98 | } |
112 | 99 |
|
113 | 100 | @Test |
114 | 101 | 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"); |
120 | 104 | 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 | + } |
122 | 109 |
|
123 | | - this.client.executeMethod(postMethod); |
| 110 | + private String serviceUrl() { |
| 111 | + return "http://localhost:%s/service".formatted(this.port); |
| 112 | + } |
124 | 113 |
|
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 | + } |
126 | 125 | } |
127 | 126 |
|
128 | 127 | } |
0 commit comments