View Javadoc
1   package org.apache.maven.wagon.providers.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.http.Header;
26  
27  
28  import org.apache.http.client.config.RequestConfig;
29  import org.apache.http.client.methods.HttpHead;
30  import org.apache.maven.wagon.OutputData;
31  import org.apache.maven.wagon.TransferFailedException;
32  import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
33  import org.apache.maven.wagon.shared.http.ConfigurationUtils;
34  import org.apache.maven.wagon.shared.http.HttpConfiguration;
35  import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
36  
37  public class HttpClientWagonTest
38      extends TestCase
39  {
40  
41      public void testSetMaxRedirectsParamViaConfig()
42      {
43          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
44          int maxRedirects = 2;
45          methodConfig.addParam("http.protocol.max-redirects", "%i," + maxRedirects);
46  
47          HttpConfiguration config = new HttpConfiguration();
48          config.setAll( methodConfig );
49  
50          HttpHead method = new HttpHead();
51          RequestConfig.Builder builder = RequestConfig.custom();
52          ConfigurationUtils.copyConfig( config.getMethodConfiguration( method ), builder );
53          RequestConfig requestConfig = builder.build();
54  
55          assertEquals(2, requestConfig.getMaxRedirects());
56      }
57  
58      public void testDefaultHeadersUsedByDefault()
59      {
60          HttpConfiguration config = new HttpConfiguration();
61          config.setAll( new HttpMethodConfiguration() );
62  
63          TestWagon wagon = new TestWagon();
64          wagon.setHttpConfiguration( config );
65  
66          HttpHead method = new HttpHead();
67          wagon.setHeaders( method );
68  
69          // these are the default headers.
70          // method.addRequestHeader( "Cache-control", "no-cache" );
71          // method.addRequestHeader( "Pragma", "no-cache" );
72          // "Accept-Encoding" is automatically set by HttpClient at runtime
73  
74          Header header = method.getFirstHeader( "Cache-control" );
75          assertNotNull( header );
76          assertEquals( "no-cache", header.getValue() );
77  
78          header = method.getFirstHeader( "Pragma" );
79          assertNotNull( header );
80          assertEquals( "no-cache", header.getValue() );
81      }
82  
83      public void testTurnOffDefaultHeaders()
84      {
85          HttpConfiguration config = new HttpConfiguration();
86          config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
87  
88          TestWagon wagon = new TestWagon();
89          wagon.setHttpConfiguration( config );
90  
91          HttpHead method = new HttpHead();
92          wagon.setHeaders( method );
93  
94          // these are the default headers.
95          // method.addRequestHeader( "Cache-control", "no-cache" );
96          // method.addRequestHeader( "Pragma", "no-cache" );
97  
98          Header header = method.getFirstHeader( "Cache-control" );
99          assertNull( header );
100 
101         header = method.getFirstHeader( "Pragma" );
102         assertNull( header );
103     }
104 
105     private static final class TestWagon
106         extends AbstractHttpClientWagon
107     {
108         @Override
109         public void fillOutputData( OutputData outputData )
110             throws TransferFailedException
111         {
112 
113         }
114     }
115 
116 }