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.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   * 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 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 }