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.HttpGet;
41  import org.apache.http.client.protocol.HttpClientContext;
42  import org.apache.http.entity.StringEntity;
43  import org.apache.http.impl.client.HttpClients;
44  import org.apache.http.localserver.LocalServerTestBase;
45  import org.apache.http.protocol.HttpContext;
46  import org.apache.http.protocol.HttpRequestHandler;
47  import org.apache.http.util.EntityUtils;
48  import org.junit.Assert;
49  import org.junit.Test;
50  
51  /**
52   * Client protocol handling tests.
53   */
54  public class TestMinimalClientRequestExecution extends LocalServerTestBase {
55  
56      private static class SimpleService implements HttpRequestHandler {
57  
58          public SimpleService() {
59              super();
60          }
61  
62          @Override
63          public void handle(
64                  final HttpRequest request,
65                  final HttpResponse response,
66                  final HttpContext context) throws HttpException, IOException {
67              response.setStatusCode(HttpStatus.SC_OK);
68              final StringEntity entity = new StringEntity("Whatever");
69              response.setEntity(entity);
70          }
71      }
72  
73      @Test
74      public void testNonCompliantURI() throws Exception {
75          this.serverBootstrap.registerHandler("*", new SimpleService());
76          this.httpclient = HttpClients.createMinimal();
77          final HttpHost target = start();
78  
79          final HttpClientContext context = HttpClientContext.create();
80          for (int i = 0; i < 10; i++) {
81              final HttpGet request = new HttpGet("/");
82              final HttpResponse response = this.httpclient.execute(target, request, context);
83              EntityUtils.consume(response.getEntity());
84              Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
85  
86              final HttpRequest reqWrapper = context.getRequest();
87              Assert.assertNotNull(reqWrapper);
88  
89              final Header[] headers = reqWrapper.getAllHeaders();
90              final Set<String> headerSet = new HashSet<String>();
91              for (final Header header: headers) {
92                  headerSet.add(header.getName().toLowerCase(Locale.ROOT));
93              }
94              Assert.assertEquals(3, headerSet.size());
95              Assert.assertTrue(headerSet.contains("connection"));
96              Assert.assertTrue(headerSet.contains("host"));
97              Assert.assertTrue(headerSet.contains("user-agent"));
98          }
99      }
100 
101 }