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  import org.apache.http.client.config.RequestConfig;
27  import org.apache.http.client.methods.HttpHead;
28  import org.apache.maven.wagon.OutputData;
29  import org.apache.maven.wagon.TransferFailedException;
30  
31  public class HttpClientWagonTest
32      extends TestCase
33  {
34  
35      public void testSetMaxRedirectsParamViaConfig()
36      {
37          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
38          int maxRedirects = 2;
39          methodConfig.addParam("http.protocol.max-redirects", "%i," + maxRedirects);
40  
41          HttpConfiguration config = new HttpConfiguration();
42          config.setAll(methodConfig);
43  
44          HttpHead method = new HttpHead();
45          RequestConfig.Builder builder = RequestConfig.custom();
46          ConfigurationUtils.copyConfig( config.getMethodConfiguration( method ), builder );
47          RequestConfig requestConfig = builder.build();
48  
49          assertEquals(2, requestConfig.getMaxRedirects());
50      }
51  
52      public void testDefaultHeadersUsedByDefault()
53      {
54          HttpConfiguration config = new HttpConfiguration();
55          config.setAll( new HttpMethodConfiguration() );
56  
57          TestWagon wagon = new TestWagon();
58          wagon.setHttpConfiguration( config );
59  
60          HttpHead method = new HttpHead();
61          wagon.setHeaders( method );
62  
63          // these are the default headers.
64          // method.addRequestHeader( "Cache-control", "no-cache" );
65          // method.addRequestHeader( "Cache-store", "no-store" );
66          // method.addRequestHeader( "Pragma", "no-cache" );
67          // method.addRequestHeader( "Expires", "0" );
68          // method.addRequestHeader( "Accept-Encoding", "gzip" );
69  
70          Header header = method.getFirstHeader( "Cache-control" );
71          assertNotNull( header );
72          assertEquals( "no-cache", header.getValue() );
73  
74          header = method.getFirstHeader( "Cache-store" );
75          assertNotNull( header );
76          assertEquals( "no-store", header.getValue() );
77  
78          header = method.getFirstHeader( "Pragma" );
79          assertNotNull( header );
80          assertEquals( "no-cache", header.getValue() );
81  
82          header = method.getFirstHeader( "Expires" );
83          assertNotNull( header );
84          assertEquals( "0", header.getValue() );
85  
86          header = method.getFirstHeader( "Accept-Encoding" );
87          assertNotNull( header );
88          assertEquals( "gzip", header.getValue() );
89      }
90  
91      public void testTurnOffDefaultHeaders()
92      {
93          HttpConfiguration config = new HttpConfiguration();
94          config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
95  
96          TestWagon wagon = new TestWagon();
97          wagon.setHttpConfiguration( config );
98  
99          HttpHead method = new HttpHead();
100         wagon.setHeaders( method );
101 
102         // these are the default headers.
103         // method.addRequestHeader( "Cache-control", "no-cache" );
104         // method.addRequestHeader( "Cache-store", "no-store" );
105         // method.addRequestHeader( "Pragma", "no-cache" );
106         // method.addRequestHeader( "Expires", "0" );
107         // method.addRequestHeader( "Accept-Encoding", "gzip" );
108 
109         Header header = method.getFirstHeader( "Cache-control" );
110         assertNull( header );
111 
112         header = method.getFirstHeader( "Cache-store" );
113         assertNull( header );
114 
115         header = method.getFirstHeader( "Pragma" );
116         assertNull( header );
117 
118         header = method.getFirstHeader( "Expires" );
119         assertNull( header );
120 
121         header = method.getFirstHeader( "Accept-Encoding" );
122         assertNull( header );
123     }
124 
125     private static final class TestWagon
126         extends AbstractHttpClientWagon
127     {
128         @Override
129         public void fillOutputData( OutputData outputData )
130             throws TransferFailedException
131         {
132 
133         }
134     }
135 
136 }