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.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   * Client protocol handling tests.
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 }