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 
120     public void testTurnOffDefaultHeaders()
121     {
122         HttpConfiguration config = new HttpConfiguration();
123         config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
124 
125         TestWagon wagon = new TestWagon();
126         wagon.setHttpConfiguration( config );
127 
128         HeadMethod method = new HeadMethod();
129         wagon.setHeaders( method );
130 
131         // these are the default headers.
132         // method.addRequestHeader( "Cache-control", "no-cache" );
133         // method.addRequestHeader( "Cache-store", "no-store" );
134         // method.addRequestHeader( "Pragma", "no-cache" );
135         // method.addRequestHeader( "Expires", "0" );
136         // method.addRequestHeader( "Accept-Encoding", "gzip" );
137 
138         Header header = method.getRequestHeader( "Cache-control" );
139         assertNull( header );
140 
141         header = method.getRequestHeader( "Cache-store" );
142         assertNull( header );
143 
144         header = method.getRequestHeader( "Pragma" );
145         assertNull( header );
146 
147         header = method.getRequestHeader( "Expires" );
148         assertNull( header );
149 
150         header = method.getRequestHeader( "Accept-Encoding" );
151         assertNull( header );
152     }
153 
154     public void testNTCredentialsWithUsernameNull()
155         throws AuthenticationException, ConnectionException
156     {
157         TestWagon wagon = new TestWagon();
158 
159         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
160         wagon.connect( repository );
161 
162         wagon.openConnection();
163 
164         assertNull( wagon.getAuthenticationInfo().getUserName() );
165         assertNull( wagon.getAuthenticationInfo().getPassword() );
166 
167         assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
168     }
169 
170     public void testNTCredentialsNoNTDomain()
171         throws AuthenticationException, ConnectionException
172     {
173         TestWagon wagon = new TestWagon();
174 
175         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
176         String myUsernameNoNTDomain = "myUserNameNoNTDomain";
177         authenticationInfo.setUserName( myUsernameNoNTDomain );
178 
179         String myPassword = "myPassword";
180         authenticationInfo.setPassword( myPassword );
181 
182         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
183 
184         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
185 
186         wagon.openConnection();
187 
188         assertEquals( myUsernameNoNTDomain, wagon.getAuthenticationInfo().getUserName() );
189         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
190 
191         assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
192     }
193 
194     public void testNTCredentialsWithNTDomain()
195         throws AuthenticationException, ConnectionException
196     {
197         TestWagon wagon = new TestWagon();
198 
199         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
200         String myNTDomain = "myNTDomain";
201         String myUsername = "myUsername";
202         String myNTDomainAndUser = myNTDomain + "\\" + myUsername;
203         authenticationInfo.setUserName( myNTDomainAndUser );
204 
205         String myPassword = "myPassword";
206         authenticationInfo.setPassword( myPassword );
207 
208         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
209 
210         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
211 
212         wagon.openConnection();
213 
214         assertEquals( myNTDomainAndUser, wagon.getAuthenticationInfo().getUserName() );
215         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
216 
217         Credentials credentials = wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) );
218         assertTrue( credentials instanceof NTCredentials );
219 
220         NTCredentials ntCredentials = (NTCredentials) credentials;
221         assertEquals( myNTDomain, ntCredentials.getDomain() );
222         assertEquals( myUsername, ntCredentials.getUserName() );
223         assertEquals( myPassword, ntCredentials.getPassword() );
224     }
225 
226     private static final class TestWagon
227         extends AbstractHttpClientWagon
228     {
229         @Override
230         public void fillOutputData( OutputData outputData )
231             throws TransferFailedException
232         {
233 
234         }
235     }
236 
237 }