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