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