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