View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.MinimalHttpClient;
36  import org.apache.hc.client5.http.protocol.HttpClientContext;
37  import org.apache.hc.client5.testing.sync.extension.TestClientResources;
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.apache.hc.core5.testing.classic.ClassicTestServer;
51  import org.apache.hc.core5.util.Timeout;
52  import org.junit.jupiter.api.Assertions;
53  import org.junit.jupiter.api.Test;
54  import org.junit.jupiter.api.extension.RegisterExtension;
55  
56  /**
57   * Client protocol handling tests.
58   */
59  public class TestMinimalClientRequestExecution {
60  
61      public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
62  
63      @RegisterExtension
64      private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
65  
66      private static class SimpleService implements HttpRequestHandler {
67  
68          public SimpleService() {
69              super();
70          }
71  
72          @Override
73          public void handle(
74                  final ClassicHttpRequest request,
75                  final ClassicHttpResponse response,
76                  final HttpContext context) throws HttpException, IOException {
77              response.setCode(HttpStatus.SC_OK);
78              final StringEntity entity = new StringEntity("Whatever");
79              response.setEntity(entity);
80          }
81      }
82  
83      @Test
84      public void testNonCompliantURIWithContext() throws Exception {
85          final ClassicTestServer server = testResources.startServer(null, null, null);
86          server.registerHandler("*", new SimpleService());
87          final HttpHost target = testResources.targetHost();
88  
89          final MinimalHttpClient client = testResources.startMinimalClient();
90  
91          final HttpClientContext context = HttpClientContext.create();
92          for (int i = 0; i < 10; i++) {
93              final HttpGet request = new HttpGet("/");
94              client.execute(target, request, context, response -> {
95                  EntityUtils.consume(response.getEntity());
96                  Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
97                  return null;
98              });
99  
100             final HttpRequest reqWrapper = context.getRequest();
101             Assertions.assertNotNull(reqWrapper);
102 
103             final Header[] headers = reqWrapper.getHeaders();
104             final Set<String> headerSet = new HashSet<>();
105             for (final Header header: headers) {
106                 headerSet.add(header.getName().toLowerCase(Locale.ROOT));
107             }
108             Assertions.assertEquals(3, headerSet.size());
109             Assertions.assertTrue(headerSet.contains("connection"));
110             Assertions.assertTrue(headerSet.contains("host"));
111             Assertions.assertTrue(headerSet.contains("user-agent"));
112         }
113     }
114 
115     @Test
116     public void testNonCompliantURIWithoutContext() throws Exception {
117         final ClassicTestServer server = testResources.startServer(null, null, null);
118         server.registerHandler("*", new SimpleService());
119         final HttpHost target = testResources.targetHost();
120 
121         final MinimalHttpClient client = testResources.startMinimalClient();
122 
123         for (int i = 0; i < 10; i++) {
124             final HttpGet request = new HttpGet("/");
125             client.execute(target, request, response -> {
126                 EntityUtils.consume(response.getEntity());
127                 Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
128                 return null;
129             });
130         }
131     }
132 
133 }