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