View Javadoc
1   package org.apache.maven.wagon.providers.webdav;
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 junit.framework.TestCase;
23  import org.apache.commons.httpclient.Credentials;
24  import org.apache.commons.httpclient.Header;
25  import org.apache.commons.httpclient.NTCredentials;
26  import org.apache.commons.httpclient.auth.AuthScope;
27  import org.apache.commons.httpclient.methods.HeadMethod;
28  import org.apache.commons.httpclient.params.HttpClientParams;
29  import org.apache.commons.httpclient.params.HttpMethodParams;
30  import org.apache.maven.wagon.ConnectionException;
31  import org.apache.maven.wagon.OutputData;
32  import org.apache.maven.wagon.TransferFailedException;
33  import org.apache.maven.wagon.authentication.AuthenticationException;
34  import org.apache.maven.wagon.authentication.AuthenticationInfo;
35  import org.apache.maven.wagon.proxy.ProxyInfo;
36  import org.apache.maven.wagon.repository.Repository;
37  
38  public class HttpClientWagonTest
39      extends TestCase
40  {
41  
42      public void testSetPreemptiveAuthParamViaConfig()
43      {
44          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
45          methodConfig.addParam( HttpClientParams.PREEMPTIVE_AUTHENTICATION, "%b,true" );
46  
47          HttpConfiguration config = new HttpConfiguration();
48          config.setAll( methodConfig );
49  
50          TestWagon wagon = new TestWagon();
51          wagon.setHttpConfiguration( config );
52  
53          HeadMethod method = new HeadMethod();
54          wagon.setParameters( method );
55  
56          HttpMethodParams params = method.getParams();
57          assertNotNull( params );
58          assertTrue( params.isParameterTrue( HttpClientParams.PREEMPTIVE_AUTHENTICATION ) );
59      }
60  
61      public void testSetMaxRedirectsParamViaConfig()
62      {
63          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
64          int maxRedirects = 2;
65          methodConfig.addParam( HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects );
66  
67          HttpConfiguration config = new HttpConfiguration();
68          config.setAll( methodConfig );
69  
70          TestWagon wagon = new TestWagon();
71          wagon.setHttpConfiguration( config );
72  
73          HeadMethod method = new HeadMethod();
74          wagon.setParameters( method );
75  
76          HttpMethodParams params = method.getParams();
77          assertNotNull( params );
78          assertEquals( maxRedirects, params.getIntParameter( HttpClientParams.MAX_REDIRECTS, -1 ) );
79      }
80  
81      public void testDefaultHeadersUsedByDefault()
82      {
83          HttpConfiguration config = new HttpConfiguration();
84          config.setAll( new HttpMethodConfiguration() );
85  
86          TestWagon wagon = new TestWagon();
87          wagon.setHttpConfiguration( config );
88  
89          HeadMethod method = new HeadMethod();
90          wagon.setHeaders( method );
91  
92          // these are the default headers.
93          // method.addRequestHeader( "Cache-control", "no-cache" );
94          // method.addRequestHeader( "Cache-store", "no-store" );
95          // method.addRequestHeader( "Pragma", "no-cache" );
96          // method.addRequestHeader( "Expires", "0" );
97          // method.addRequestHeader( "Accept-Encoding", "gzip" );
98  
99          Header header = method.getRequestHeader( "Cache-control" );
100         assertNotNull( header );
101         assertEquals( "no-cache", header.getValue() );
102 
103         header = method.getRequestHeader( "Cache-store" );
104         assertNotNull( header );
105         assertEquals( "no-store", header.getValue() );
106 
107         header = method.getRequestHeader( "Pragma" );
108         assertNotNull( header );
109         assertEquals( "no-cache", header.getValue() );
110 
111         header = method.getRequestHeader( "Expires" );
112         assertNotNull( header );
113         assertEquals( "0", header.getValue() );
114 
115         header = method.getRequestHeader( "Accept-Encoding" );
116         assertNotNull( header );
117         assertEquals( "gzip", header.getValue() );
118 
119         header = method.getRequestHeader( "User-Agent" );
120         assertNotNull( header );
121         // during test-phase /META-INF/maven/org.apache.maven.wagon/*/pom.properties hasn't been created yet
122         assertTrue( header.getValue().startsWith( "Apache-Maven-Wagon/unknown-version (Java " ) );
123     }
124 
125     public void testTurnOffDefaultHeaders()
126     {
127         HttpConfiguration config = new HttpConfiguration();
128         config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
129 
130         TestWagon wagon = new TestWagon();
131         wagon.setHttpConfiguration( config );
132 
133         HeadMethod method = new HeadMethod();
134         wagon.setHeaders( method );
135 
136         // these are the default headers.
137         // method.addRequestHeader( "Cache-control", "no-cache" );
138         // method.addRequestHeader( "Cache-store", "no-store" );
139         // method.addRequestHeader( "Pragma", "no-cache" );
140         // method.addRequestHeader( "Expires", "0" );
141         // method.addRequestHeader( "Accept-Encoding", "gzip" );
142 
143         Header header = method.getRequestHeader( "Cache-control" );
144         assertNull( header );
145 
146         header = method.getRequestHeader( "Cache-store" );
147         assertNull( header );
148 
149         header = method.getRequestHeader( "Pragma" );
150         assertNull( header );
151 
152         header = method.getRequestHeader( "Expires" );
153         assertNull( header );
154 
155         header = method.getRequestHeader( "Accept-Encoding" );
156         assertNull( header );
157     }
158 
159     public void testNTCredentialsWithUsernameNull()
160         throws AuthenticationException, ConnectionException
161     {
162         TestWagon wagon = new TestWagon();
163 
164         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
165         wagon.connect( repository );
166 
167         wagon.openConnection();
168 
169         assertNull( wagon.getAuthenticationInfo().getUserName() );
170         assertNull( wagon.getAuthenticationInfo().getPassword() );
171 
172         assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
173     }
174 
175     public void testNTCredentialsNoNTDomain()
176         throws AuthenticationException, ConnectionException
177     {
178         TestWagon wagon = new TestWagon();
179 
180         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
181         String myUsernameNoNTDomain = "myUserNameNoNTDomain";
182         authenticationInfo.setUserName( myUsernameNoNTDomain );
183 
184         String myPassword = "myPassword";
185         authenticationInfo.setPassword( myPassword );
186 
187         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
188 
189         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
190 
191         wagon.openConnection();
192 
193         assertEquals( myUsernameNoNTDomain, wagon.getAuthenticationInfo().getUserName() );
194         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
195 
196         assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
197     }
198 
199     public void testNTCredentialsWithNTDomain()
200         throws AuthenticationException, ConnectionException
201     {
202         TestWagon wagon = new TestWagon();
203 
204         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
205         String myNTDomain = "myNTDomain";
206         String myUsername = "myUsername";
207         String myNTDomainAndUser = myNTDomain + "\\" + myUsername;
208         authenticationInfo.setUserName( myNTDomainAndUser );
209 
210         String myPassword = "myPassword";
211         authenticationInfo.setPassword( myPassword );
212 
213         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
214 
215         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
216 
217         wagon.openConnection();
218 
219         assertEquals( myNTDomainAndUser, wagon.getAuthenticationInfo().getUserName() );
220         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
221 
222         Credentials credentials = wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) );
223         assertTrue( credentials instanceof NTCredentials );
224 
225         NTCredentials ntCredentials = (NTCredentials) credentials;
226         assertEquals( myNTDomain, ntCredentials.getDomain() );
227         assertEquals( myUsername, ntCredentials.getUserName() );
228         assertEquals( myPassword, ntCredentials.getPassword() );
229     }
230 
231     private static final class TestWagon
232         extends AbstractHttpClientWagon
233     {
234         @Override
235         public void fillOutputData( OutputData outputData )
236             throws TransferFailedException
237         {
238 
239         }
240     }
241 
242 }