1   package org.apache.maven.wagon.shared.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  import org.apache.commons.httpclient.Header;
23  import org.apache.commons.httpclient.methods.HeadMethod;
24  import org.apache.commons.httpclient.params.HttpClientParams;
25  import org.apache.commons.httpclient.params.HttpMethodParams;
26  
27  import junit.framework.TestCase;
28  
29  public class AbstractHttpClientWagonTest
30      extends TestCase
31  {
32  
33      public void testSetPreemptiveAuthParamViaConfig()
34      {
35          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
36          methodConfig.addParam( HttpClientParams.PREEMPTIVE_AUTHENTICATION, "%b,true" );
37  
38          HttpConfiguration config = new HttpConfiguration();
39          config.setAll( methodConfig );
40  
41          TestWagon wagon = new TestWagon();
42          wagon.setHttpConfiguration( config );
43  
44          HeadMethod method = new HeadMethod();
45          wagon.setParameters( method );
46  
47          HttpMethodParams params = method.getParams();
48          assertNotNull( params );
49          assertTrue( params.isParameterTrue( HttpClientParams.PREEMPTIVE_AUTHENTICATION ) );
50      }
51  
52      public void testSetMaxRedirectsParamViaConfig()
53      {
54          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
55          int maxRedirects = 2;
56          methodConfig.addParam( HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects );
57  
58          HttpConfiguration config = new HttpConfiguration();
59          config.setAll( methodConfig );
60  
61          TestWagon wagon = new TestWagon();
62          wagon.setHttpConfiguration( config );
63  
64          HeadMethod method = new HeadMethod();
65          wagon.setParameters( method );
66  
67          HttpMethodParams params = method.getParams();
68          assertNotNull( params );
69          assertEquals( maxRedirects, params.getIntParameter( HttpClientParams.MAX_REDIRECTS, -1 ) );
70      }
71  
72      public void testDefaultHeadersUsedByDefault()
73      {
74          HttpConfiguration config = new HttpConfiguration();
75          config.setAll( new HttpMethodConfiguration() );
76  
77          TestWagon wagon = new TestWagon();
78          wagon.setHttpConfiguration( config );
79  
80          HeadMethod method = new HeadMethod();
81          wagon.setHeaders( method );
82  
83          // these are the default headers.
84          // method.addRequestHeader( "Cache-control", "no-cache" );
85          // method.addRequestHeader( "Cache-store", "no-store" );
86          // method.addRequestHeader( "Pragma", "no-cache" );
87          // method.addRequestHeader( "Expires", "0" );
88          // method.addRequestHeader( "Accept-Encoding", "gzip" );
89  
90          Header header = method.getRequestHeader( "Cache-control" );
91          assertNotNull( header );
92          assertEquals( "no-cache", header.getValue() );
93  
94          header = method.getRequestHeader( "Cache-store" );
95          assertNotNull( header );
96          assertEquals( "no-store", header.getValue() );
97  
98          header = method.getRequestHeader( "Pragma" );
99          assertNotNull( header );
100         assertEquals( "no-cache", header.getValue() );
101 
102         header = method.getRequestHeader( "Expires" );
103         assertNotNull( header );
104         assertEquals( "0", header.getValue() );
105 
106         header = method.getRequestHeader( "Accept-Encoding" );
107         assertNotNull( header );
108         assertEquals( "gzip", header.getValue() );
109     }
110 
111     public void testTurnOffDefaultHeaders()
112     {
113         HttpConfiguration config = new HttpConfiguration();
114         config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
115 
116         TestWagon wagon = new TestWagon();
117         wagon.setHttpConfiguration( config );
118 
119         HeadMethod method = new HeadMethod();
120         wagon.setHeaders( method );
121 
122         // these are the default headers.
123         // method.addRequestHeader( "Cache-control", "no-cache" );
124         // method.addRequestHeader( "Cache-store", "no-store" );
125         // method.addRequestHeader( "Pragma", "no-cache" );
126         // method.addRequestHeader( "Expires", "0" );
127         // method.addRequestHeader( "Accept-Encoding", "gzip" );
128 
129         Header header = method.getRequestHeader( "Cache-control" );
130         assertNull( header );
131 
132         header = method.getRequestHeader( "Cache-store" );
133         assertNull( header );
134 
135         header = method.getRequestHeader( "Pragma" );
136         assertNull( header );
137 
138         header = method.getRequestHeader( "Expires" );
139         assertNull( header );
140 
141         header = method.getRequestHeader( "Accept-Encoding" );
142         assertNull( header );
143     }
144 
145     private static final class TestWagon
146         extends AbstractHttpClientWagon
147     {
148     }
149 
150 }