View Javadoc

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