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.fluent.Content;
35 import org.apache.hc.client5.http.fluent.Request;
36 import org.apache.hc.client5.testing.sync.extension.TestClientResources;
37 import org.apache.hc.core5.http.ContentType;
38 import org.apache.hc.core5.http.HttpEntity;
39 import org.apache.hc.core5.http.HttpHost;
40 import org.apache.hc.core5.http.URIScheme;
41 import org.apache.hc.core5.http.io.entity.EntityUtils;
42 import org.apache.hc.core5.http.io.entity.StringEntity;
43 import org.apache.hc.core5.testing.classic.ClassicTestServer;
44 import org.apache.hc.core5.util.Timeout;
45 import org.junit.jupiter.api.Assertions;
46 import org.junit.jupiter.api.BeforeEach;
47 import org.junit.jupiter.api.Test;
48 import org.junit.jupiter.api.extension.RegisterExtension;
49
50 public class TestFluent {
51
52 public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
53
54 @RegisterExtension
55 private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
56
57 public HttpHost targetHost() {
58 return testResources.targetHost();
59 }
60
61 @BeforeEach
62 public void setUp() throws Exception {
63 final ClassicTestServer server = testResources.startServer(null, null, null);
64 server.registerHandler("/", (request, response, context) ->
65 response.setEntity(new StringEntity("All is well", ContentType.TEXT_PLAIN)));
66 server.registerHandler("/echo", (request, response, context) -> {
67 HttpEntity responseEntity = null;
68 final HttpEntity requestEntity = request.getEntity();
69 if (requestEntity != null) {
70 final String contentTypeStr = requestEntity.getContentType();
71 final ContentType contentType = contentTypeStr == null ? ContentType.DEFAULT_TEXT : ContentType.parse(contentTypeStr);
72 if (ContentType.TEXT_PLAIN.getMimeType().equals(contentType.getMimeType())) {
73 responseEntity = new StringEntity(
74 EntityUtils.toString(requestEntity), ContentType.TEXT_PLAIN);
75 }
76 }
77 if (responseEntity == null) {
78 responseEntity = new StringEntity("echo", ContentType.TEXT_PLAIN);
79 }
80 response.setEntity(responseEntity);
81 });
82 }
83
84 @Test
85 public void testGetRequest() throws Exception {
86 final HttpHost target = targetHost();
87 final String baseURL = "http://localhost:" + target.getPort();
88 final String message = Request.get(baseURL + "/").execute().returnContent().asString();
89 Assertions.assertEquals("All is well", message);
90 }
91
92 @Test
93 public void testGetRequestByName() throws Exception {
94 final HttpHost target = targetHost();
95 final String baseURL = "http://localhost:" + target.getPort();
96 final String message = Request.create("GET", baseURL + "/").execute().returnContent().asString();
97 Assertions.assertEquals("All is well", message);
98 }
99
100 @Test
101 public void testGetRequestByNameWithURI() throws Exception {
102 final HttpHost target = targetHost();
103 final String baseURL = "http://localhost:" + target.getPort();
104 final String message = Request.create("GET", new URI(baseURL + "/")).execute().returnContent().asString();
105 Assertions.assertEquals("All is well", message);
106 }
107
108 @Test
109 public void testGetRequestFailure() throws Exception {
110 final HttpHost target = targetHost();
111 final String baseURL = "http://localhost:" + target.getPort();
112 Assertions.assertThrows(ClientProtocolException.class, () ->
113 Request.get(baseURL + "/boom").execute().returnContent().asString());
114 }
115
116 @Test
117 public void testPostRequest() throws Exception {
118 final HttpHost target = targetHost();
119 final String baseURL = "http://localhost:" + target.getPort();
120 final String message1 = Request.post(baseURL + "/echo")
121 .bodyString("what is up?", ContentType.TEXT_PLAIN)
122 .execute().returnContent().asString();
123 Assertions.assertEquals("what is up?", message1);
124 final String message2 = Request.post(baseURL + "/echo")
125 .bodyByteArray(new byte[]{1, 2, 3}, ContentType.APPLICATION_OCTET_STREAM)
126 .execute().returnContent().asString();
127 Assertions.assertEquals("echo", message2);
128 }
129
130 @Test
131 public void testContentAsStringWithCharset() throws Exception {
132 final HttpHost target = targetHost();
133 final String baseURL = "http://localhost:" + target.getPort();
134 final Content content = Request.post(baseURL + "/echo").bodyByteArray("Ü".getBytes(StandardCharsets.UTF_8)).execute()
135 .returnContent();
136 Assertions.assertEquals((byte)-61, content.asBytes()[0]);
137 Assertions.assertEquals((byte)-100, content.asBytes()[1]);
138 Assertions.assertEquals("Ü", content.asString(StandardCharsets.UTF_8));
139 }
140
141 @Test
142 public void testConnectionRelease() throws Exception {
143 final HttpHost target = targetHost();
144 final String baseURL = "http://localhost:" + target.getPort();
145 for (int i = 0; i < 20; i++) {
146 Request.get(baseURL + "/").execute().returnContent();
147 Request.get(baseURL + "/").execute().returnResponse();
148 Request.get(baseURL + "/").execute().discardContent();
149 Request.get(baseURL + "/").execute().handleResponse(response -> null);
150 final File tmpFile = File.createTempFile("test", ".bin");
151 try {
152 Request.get(baseURL + "/").execute().saveContent(tmpFile);
153 } finally {
154 tmpFile.delete();
155 }
156 }
157 }
158
159 }