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( "Cache-store", "no-store" );
72          // method.addRequestHeader( "Pragma", "no-cache" );
73          // method.addRequestHeader( "Expires", "0" );
74          // method.addRequestHeader( "Accept-Encoding", "gzip" );
75  
76          Header header = method.getFirstHeader( "Cache-control" );
77          assertNotNull( header );
78          assertEquals( "no-cache", header.getValue() );
79  
80          header = method.getFirstHeader( "Cache-store" );
81          assertNotNull( header );
82          assertEquals( "no-store", header.getValue() );
83  
84          header = method.getFirstHeader( "Pragma" );
85          assertNotNull( header );
86          assertEquals( "no-cache", header.getValue() );
87  
88          header = method.getFirstHeader( "Expires" );
89          assertNotNull( header );
90          assertEquals( "0", header.getValue() );
91  
92          header = method.getFirstHeader( "Accept-Encoding" );
93          assertNotNull( header );
94          assertEquals( "gzip", header.getValue() );
95      }
96  
97      public void testTurnOffDefaultHeaders()
98      {
99          HttpConfiguration config = new HttpConfiguration();
100         config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
101 
102         TestWagon wagon = new TestWagon();
103         wagon.setHttpConfiguration( config );
104 
105         HttpHead method = new HttpHead();
106         wagon.setHeaders( method );
107 
108         // these are the default headers.
109         // method.addRequestHeader( "Cache-control", "no-cache" );
110         // method.addRequestHeader( "Cache-store", "no-store" );
111         // method.addRequestHeader( "Pragma", "no-cache" );
112         // method.addRequestHeader( "Expires", "0" );
113         // method.addRequestHeader( "Accept-Encoding", "gzip" );
114 
115         Header header = method.getFirstHeader( "Cache-control" );
116         assertNull( header );
117 
118         header = method.getFirstHeader( "Cache-store" );
119         assertNull( header );
120 
121         header = method.getFirstHeader( "Pragma" );
122         assertNull( header );
123 
124         header = method.getFirstHeader( "Expires" );
125         assertNull( header );
126 
127         header = method.getFirstHeader( "Accept-Encoding" );
128         assertNull( header );
129     }
130 
131     private static final class TestWagon
132         extends AbstractHttpClientWagon
133     {
134         @Override
135         public void fillOutputData( OutputData outputData )
136             throws TransferFailedException
137         {
138 
139         }
140     }
141 
142 }