View Javadoc

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