1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.testing.fluent;
28
29 import java.io.File;
30 import java.net.URI;
31 import java.nio.charset.StandardCharsets;
32
33 import org.apache.hc.client5.http.ClientProtocolException;
34 import org.apache.hc.client5.http.HttpResponseException;
35 import org.apache.hc.client5.http.fluent.Content;
36 import org.apache.hc.client5.http.fluent.Request;
37 import org.apache.hc.client5.testing.sync.extension.TestClientResources;
38 import org.apache.hc.core5.http.ContentType;
39 import org.apache.hc.core5.http.HttpEntity;
40 import org.apache.hc.core5.http.HttpHost;
41 import org.apache.hc.core5.http.HttpStatus;
42 import org.apache.hc.core5.http.URIScheme;
43 import org.apache.hc.core5.http.io.entity.EntityUtils;
44 import org.apache.hc.core5.http.io.entity.StringEntity;
45 import org.apache.hc.core5.testing.classic.ClassicTestServer;
46 import org.apache.hc.core5.util.Timeout;
47 import org.junit.jupiter.api.Assertions;
48 import org.junit.jupiter.api.BeforeEach;
49 import org.junit.jupiter.api.Test;
50 import org.junit.jupiter.api.extension.RegisterExtension;
51
52 public class TestFluent {
53
54 public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
55
56 @RegisterExtension
57 private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
58
59 public HttpHost targetHost() {
60 return testResources.targetHost();
61 }
62
63 @BeforeEach
64 public void setUp() throws Exception {
65 final ClassicTestServer server = testResources.startServer(null, null, null);
66 server.registerHandler("/", (request, response, context) ->
67 response.setEntity(new StringEntity("All is well", ContentType.TEXT_PLAIN)));
68 server.registerHandler("/echo", (request, response, context) -> {
69 HttpEntity responseEntity = null;
70 final HttpEntity requestEntity = request.getEntity();
71 if (requestEntity != null) {
72 final String contentTypeStr = requestEntity.getContentType();
73 final ContentType contentType = contentTypeStr == null ? ContentType.DEFAULT_TEXT : ContentType.parse(contentTypeStr);
74 if (ContentType.TEXT_PLAIN.getMimeType().equals(contentType.getMimeType())) {
75 responseEntity = new StringEntity(
76 EntityUtils.toString(requestEntity), ContentType.TEXT_PLAIN);
77 }
78 }
79 if (responseEntity == null) {
80 responseEntity = new StringEntity("echo", ContentType.TEXT_PLAIN);
81 }
82 response.setEntity(responseEntity);
83 });
84
85
86 server.registerHandler("/large-message", (request, response, context) -> {
87 final String largeContent = generateLargeString(10000);
88 response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN));
89 });
90
91
92 server.registerHandler("/large-message-error", (request, response, context) -> {
93 final String largeContent = generateLargeString(10000);
94 response.setCode(HttpStatus.SC_REDIRECTION);
95 response.setEntity(new StringEntity(largeContent, ContentType.TEXT_PLAIN));
96 });
97
98 }
99
100 @Test
101 public void testGetRequest() throws Exception {
102 final HttpHost target = targetHost();
103 final String baseURL = "http://localhost:" + target.getPort();
104 final String message = Request.get(baseURL + "/").execute().returnContent().asString();
105 Assertions.assertEquals("All is well", message);
106 }
107
108 @Test
109 public void testGetRequestByName() throws Exception {
110 final HttpHost target = targetHost();
111 final String baseURL = "http://localhost:" + target.getPort();
112 final String message = Request.create("GET", baseURL + "/").execute().returnContent().asString();
113 Assertions.assertEquals("All is well", message);
114 }
115
116 @Test
117 public void testGetRequestByNameWithURI() throws Exception {
118 final HttpHost target = targetHost();
119 final String baseURL = "http://localhost:" + target.getPort();
120 final String message = Request.create("GET", new URI(baseURL + "/")).execute().returnContent().asString();
121 Assertions.assertEquals("All is well", message);
122 }
123
124 @Test
125 public void testGetRequestFailure() throws Exception {
126 final HttpHost target = targetHost();
127 final String baseURL = "http://localhost:" + target.getPort();
128 Assertions.assertThrows(ClientProtocolException.class, () ->
129 Request.get(baseURL + "/boom").execute().returnContent().asString());
130 }
131
132 @Test
133 public void testPostRequest() throws Exception {
134 final HttpHost target = targetHost();
135 final String baseURL = "http://localhost:" + target.getPort();
136 final String message1 = Request.post(baseURL + "/echo")
137 .bodyString("what is up?", ContentType.TEXT_PLAIN)
138 .execute().returnContent().asString();
139 Assertions.assertEquals("what is up?", message1);
140 final String message2 = Request.post(baseURL + "/echo")
141 .bodyByteArray(new byte[]{1, 2, 3}, ContentType.APPLICATION_OCTET_STREAM)
142 .execute().returnContent().asString();
143 Assertions.assertEquals("echo", message2);
144 }
145
146 @Test
147 public void testContentAsStringWithCharset() throws Exception {
148 final HttpHost target = targetHost();
149 final String baseURL = "http://localhost:" + target.getPort();
150 final Content content = Request.post(baseURL + "/echo").bodyByteArray("Ü".getBytes(StandardCharsets.UTF_8)).execute()
151 .returnContent();
152 Assertions.assertEquals((byte)-61, content.asBytes()[0]);
153 Assertions.assertEquals((byte)-100, content.asBytes()[1]);
154 Assertions.assertEquals("Ü", content.asString(StandardCharsets.UTF_8));
155 }
156
157 @Test
158 public void testConnectionRelease() throws Exception {
159 final HttpHost target = targetHost();
160 final String baseURL = "http://localhost:" + target.getPort();
161 for (int i = 0; i < 20; i++) {
162 Request.get(baseURL + "/").execute().returnContent();
163 Request.get(baseURL + "/").execute().returnResponse();
164 Request.get(baseURL + "/").execute().discardContent();
165 Request.get(baseURL + "/").execute().handleResponse(response -> null);
166 final File tmpFile = File.createTempFile("test", ".bin");
167 try {
168 Request.get(baseURL + "/").execute().saveContent(tmpFile);
169 } finally {
170 tmpFile.delete();
171 }
172 }
173 }
174
175 private String generateLargeString(final int size) {
176 final StringBuilder sb = new StringBuilder(size);
177 for (int i = 0; i < size; i++) {
178 sb.append("x");
179 }
180 return sb.toString();
181 }
182
183 @Test
184 public void testLargeResponse() throws Exception {
185
186 final HttpHost target = targetHost();
187 final String baseURL = "http://localhost:" + target.getPort();
188
189 final Content content = Request.get(baseURL + "/large-message").execute().returnContent();
190 Assertions.assertEquals(10000, content.asBytes().length);
191 }
192
193 @Test
194 public void testLargeResponseError() throws Exception {
195 final HttpHost target = targetHost();
196 final String baseURL = "http://localhost:" + target.getPort();
197
198 try {
199 Request.get(baseURL + "/large-message-error").execute().returnContent();
200 Assertions.fail("Expected an HttpResponseException to be thrown");
201 } catch (final HttpResponseException e) {
202
203 final byte[] contentBytes = e.getContentBytes();
204 Assertions.assertNotNull(contentBytes, "Content bytes should not be null");
205 Assertions.assertTrue(contentBytes.length <= 256, "Content length should be less or equal to 256 bytes");
206 }
207 }
208
209 }