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.HttpClients;
36 import org.apache.hc.client5.http.protocol.HttpClientContext;
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.HttpException;
41 import org.apache.hc.core5.http.HttpHost;
42 import org.apache.hc.core5.http.HttpRequest;
43 import org.apache.hc.core5.http.HttpStatus;
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.Assert;
49 import org.junit.Test;
50
51
52
53
54 public class TestMinimalClientRequestExecution extends LocalServerTestBase {
55
56 private static class SimpleService implements HttpRequestHandler {
57
58 public SimpleService() {
59 super();
60 }
61
62 @Override
63 public void handle(
64 final ClassicHttpRequest request,
65 final ClassicHttpResponse response,
66 final HttpContext context) throws HttpException, IOException {
67 response.setCode(HttpStatus.SC_OK);
68 final StringEntity entity = new StringEntity("Whatever");
69 response.setEntity(entity);
70 }
71 }
72
73 @Test
74 public void testNonCompliantURI() throws Exception {
75 this.server.registerHandler("*", new SimpleService());
76 this.httpclient = HttpClients.createMinimal();
77 final HttpHost target = start();
78
79 final HttpClientContext context = HttpClientContext.create();
80 for (int i = 0; i < 10; i++) {
81 final HttpGet request = new HttpGet("/");
82 final ClassicHttpResponse response = this.httpclient.execute(target, request, context);
83 EntityUtils.consume(response.getEntity());
84 Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
85
86 final HttpRequest reqWrapper = context.getRequest();
87 Assert.assertNotNull(reqWrapper);
88
89 final Header[] headers = reqWrapper.getHeaders();
90 final Set<String> headerSet = new HashSet<>();
91 for (final Header header: headers) {
92 headerSet.add(header.getName().toLowerCase(Locale.ROOT));
93 }
94 Assert.assertEquals(3, headerSet.size());
95 Assert.assertTrue(headerSet.contains("connection"));
96 Assert.assertTrue(headerSet.contains("host"));
97 Assert.assertTrue(headerSet.contains("user-agent"));
98 }
99 }
100
101 }