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.sync;
28
29 import java.io.IOException;
30 import java.util.HashSet;
31 import java.util.Locale;
32 import java.util.Set;
33
34 import org.apache.hc.client5.http.classic.methods.HttpGet;
35 import org.apache.hc.client5.http.impl.classic.MinimalHttpClient;
36 import org.apache.hc.client5.http.protocol.HttpClientContext;
37 import org.apache.hc.client5.testing.sync.extension.TestClientResources;
38 import org.apache.hc.core5.http.ClassicHttpRequest;
39 import org.apache.hc.core5.http.ClassicHttpResponse;
40 import org.apache.hc.core5.http.Header;
41 import org.apache.hc.core5.http.HttpException;
42 import org.apache.hc.core5.http.HttpHost;
43 import org.apache.hc.core5.http.HttpRequest;
44 import org.apache.hc.core5.http.HttpStatus;
45 import org.apache.hc.core5.http.URIScheme;
46 import org.apache.hc.core5.http.io.HttpRequestHandler;
47 import org.apache.hc.core5.http.io.entity.EntityUtils;
48 import org.apache.hc.core5.http.io.entity.StringEntity;
49 import org.apache.hc.core5.http.protocol.HttpContext;
50 import org.apache.hc.core5.testing.classic.ClassicTestServer;
51 import org.apache.hc.core5.util.Timeout;
52 import org.junit.jupiter.api.Assertions;
53 import org.junit.jupiter.api.Test;
54 import org.junit.jupiter.api.extension.RegisterExtension;
55
56
57
58
59 public abstract class TestMinimalClientRequestExecution {
60
61 public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
62
63 @RegisterExtension
64 private TestClientResources testResources;
65
66 protected TestMinimalClientRequestExecution(final URIScheme scheme) {
67 this.testResources = new TestClientResources(scheme, TIMEOUT);
68 }
69
70 public URIScheme scheme() {
71 return testResources.scheme();
72 }
73 private static class SimpleService implements HttpRequestHandler {
74
75 public SimpleService() {
76 super();
77 }
78
79 @Override
80 public void handle(
81 final ClassicHttpRequest request,
82 final ClassicHttpResponse response,
83 final HttpContext context) throws HttpException, IOException {
84 response.setCode(HttpStatus.SC_OK);
85 final StringEntity entity = new StringEntity("Whatever");
86 response.setEntity(entity);
87 }
88 }
89
90 @Test
91 public void testNonCompliantURIWithContext() throws Exception {
92 final ClassicTestServer server = testResources.startServer(null, null, null);
93 server.registerHandler("*", new SimpleService());
94 final HttpHost target = testResources.targetHost();
95
96 final MinimalHttpClient client = testResources.startMinimalClient();
97
98 final HttpClientContext context = HttpClientContext.create();
99 for (int i = 0; i < 10; i++) {
100 final HttpGet request = new HttpGet("/");
101 client.execute(target, request, context, response -> {
102 EntityUtils.consume(response.getEntity());
103 Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
104 return null;
105 });
106
107 final HttpRequest reqWrapper = context.getRequest();
108 Assertions.assertNotNull(reqWrapper);
109
110 final Header[] headers = reqWrapper.getHeaders();
111 final Set<String> headerSet = new HashSet<>();
112 for (final Header header: headers) {
113 headerSet.add(header.getName().toLowerCase(Locale.ROOT));
114 }
115 Assertions.assertEquals(3, headerSet.size());
116 Assertions.assertTrue(headerSet.contains("connection"));
117 Assertions.assertTrue(headerSet.contains("host"));
118 Assertions.assertTrue(headerSet.contains("user-agent"));
119 }
120 }
121
122 @Test
123 public void testNonCompliantURIWithoutContext() throws Exception {
124 final ClassicTestServer server = testResources.startServer(null, null, null);
125 server.registerHandler("*", new SimpleService());
126 final HttpHost target = testResources.targetHost();
127
128 final MinimalHttpClient client = testResources.startMinimalClient();
129
130 for (int i = 0; i < 10; i++) {
131 final HttpGet request = new HttpGet("/");
132 client.execute(target, request, response -> {
133 EntityUtils.consume(response.getEntity());
134 Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
135 return null;
136 });
137 }
138 }
139
140 }