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.http.impl.client.integration;
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.http.Header;
35 import org.apache.http.HttpException;
36 import org.apache.http.HttpHost;
37 import org.apache.http.HttpRequest;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.HttpStatus;
40 import org.apache.http.client.methods.CloseableHttpResponse;
41 import org.apache.http.client.methods.HttpGet;
42 import org.apache.http.client.protocol.HttpClientContext;
43 import org.apache.http.entity.StringEntity;
44 import org.apache.http.impl.client.HttpClients;
45 import org.apache.http.localserver.LocalServerTestBase;
46 import org.apache.http.protocol.HttpContext;
47 import org.apache.http.protocol.HttpRequestHandler;
48 import org.apache.http.util.EntityUtils;
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 HttpRequest request,
66 final HttpResponse response,
67 final HttpContext context) throws HttpException, IOException {
68 response.setStatusCode(HttpStatus.SC_OK);
69 final StringEntity entity = new StringEntity("Whatever");
70 response.setEntity(entity);
71 }
72 }
73
74 @Test
75 public void testNonCompliantURI() throws Exception {
76 this.serverBootstrap.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 final CloseableHttpResponse response = this.httpclient.execute(target, request, context);
84 try {
85 EntityUtils.consume(response.getEntity());
86 Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
87 } finally {
88 response.close();
89 }
90 final HttpRequest reqWrapper = context.getRequest();
91 Assert.assertNotNull(reqWrapper);
92
93 final Header[] headers = reqWrapper.getAllHeaders();
94 final Set<String> headerSet = new HashSet<String>();
95 for (final Header header : headers) {
96 headerSet.add(header.getName().toLowerCase(Locale.ROOT));
97 }
98 Assert.assertEquals(3, headerSet.size());
99 Assert.assertTrue(headerSet.contains("connection"));
100 Assert.assertTrue(headerSet.contains("host"));
101 Assert.assertTrue(headerSet.contains("user-agent"));
102 }
103 }
104
105 @Test
106 public void testNonCompliantURIWithoutContext() throws Exception {
107 this.serverBootstrap.registerHandler("*", new SimpleService());
108 this.httpclient = HttpClients.createMinimal();
109 final HttpHost target = start();
110
111 for (int i = 0; i < 10; i++) {
112 final HttpGet request = new HttpGet("/");
113 final CloseableHttpResponse response = this.httpclient.execute(target, request);
114 try {
115 EntityUtils.consume(response.getEntity());
116 Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
117 } finally {
118 response.close();
119 }
120 }
121 }
122
123 }