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.HttpConfiguration;
35  import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
36  import org.junit.Ignore;
37  
38  public class HttpClientWagonTest
39      extends TestCase
40  {
41  
42      @Ignore("not sure how to test this")
43      public void testSetPreemptiveAuthParamViaConfig()
44      {
45          HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
46          methodConfig.setUsePreemptive( true );
47  
48          HttpConfiguration config = new HttpConfiguration();
49          config.setAll( methodConfig );
50  
51          TestWagon wagon = new TestWagon();
52          wagon.setHttpConfiguration( config );
53  
54          HttpHead method = new HttpHead();
55          wagon.setHeaders( method );
56  
57          HttpParams params = method.getParams();
58          assertNotNull( params );
59          //assertTrue( params.isParameterTrue( HttpClientParams.PREEMPTIVE_AUTHENTICATION ) );
60      }
61  
62  //    @Ignore("not sure how to test this")
63  //    public void testSetMaxRedirectsParamViaConfig()
64  //    {
65  //        HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
66  //        int maxRedirects = 2;
67  //        methodConfig.addParam( HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects );
68  //
69  //        HttpConfiguration config = new HttpConfiguration();
70  //        config.setAll( methodConfig );
71  //
72  //        TestWagon wagon = new TestWagon();
73  //        wagon.setHttpConfiguration( config );
74  //
75  //        HttpHead method = new HttpHead();
76  //        wagon.setParameters( method );
77  //
78  //        HttpParams params = method.getParams();
79  //        assertNotNull( params );
80  //        assertEquals( maxRedirects, params.getIntParameter( HttpClientParams.MAX_REDIRECTS, -1 ) );
81  //    }
82  
83      public void testDefaultHeadersUsedByDefault()
84      {
85          HttpConfiguration config = new HttpConfiguration();
86          config.setAll( new HttpMethodConfiguration() );
87  
88          TestWagon wagon = new TestWagon();
89          wagon.setHttpConfiguration( config );
90  
91          HttpHead method = new HttpHead();
92          wagon.setHeaders( method );
93  
94          // these are the default headers.
95          // method.addRequestHeader( "Cache-control", "no-cache" );
96          // method.addRequestHeader( "Pragma", "no-cache" );
97          // "Accept-Encoding" is automatically set by HttpClient at runtime
98  
99          Header header = method.getFirstHeader( "Cache-control" );
100         assertNotNull( header );
101         assertEquals( "no-cache", header.getValue() );
102 
103         header = method.getFirstHeader( "Pragma" );
104         assertNotNull( header );
105         assertEquals( "no-cache", header.getValue() );
106     }
107 
108     public void testTurnOffDefaultHeaders()
109     {
110         HttpConfiguration config = new HttpConfiguration();
111         config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
112 
113         TestWagon wagon = new TestWagon();
114         wagon.setHttpConfiguration( config );
115 
116         HttpHead method = new HttpHead();
117         wagon.setHeaders( method );
118 
119         // these are the default headers.
120         // method.addRequestHeader( "Cache-control", "no-cache" );
121         // method.addRequestHeader( "Pragma", "no-cache" );
122 
123         Header header = method.getFirstHeader( "Cache-control" );
124         assertNull( header );
125 
126         header = method.getFirstHeader( "Pragma" );
127         assertNull( header );
128     }
129 
130     @Ignore("not sure how to test this")
131     public void testNTCredentialsWithUsernameNull()
132         throws AuthenticationException, ConnectionException
133     {
134         TestWagon wagon = new TestWagon();
135 
136         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
137         wagon.connect( repository );
138 
139         wagon.openConnection();
140 
141         assertNull( wagon.getAuthenticationInfo().getUserName() );
142         assertNull( wagon.getAuthenticationInfo().getPassword() );
143 
144         //assertFalse( wagon.getHttpClient()..getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
145     }
146 
147     @Ignore("not sure how to test this")
148     public void testNTCredentialsNoNTDomain()
149         throws AuthenticationException, ConnectionException
150     {
151         TestWagon wagon = new TestWagon();
152 
153         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
154         String myUsernameNoNTDomain = "myUserNameNoNTDomain";
155         authenticationInfo.setUserName( myUsernameNoNTDomain );
156 
157         String myPassword = "myPassword";
158         authenticationInfo.setPassword( myPassword );
159 
160         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
161 
162         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
163 
164         wagon.openConnection();
165 
166         assertEquals( myUsernameNoNTDomain, wagon.getAuthenticationInfo().getUserName() );
167         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
168 
169         //assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
170     }
171 
172     @Ignore("not sure how to test this")
173     public void testNTCredentialsWithNTDomain()
174         throws AuthenticationException, ConnectionException
175     {
176         TestWagon wagon = new TestWagon();
177 
178         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
179         String myNTDomain = "myNTDomain";
180         String myUsername = "myUsername";
181         String myNTDomainAndUser = myNTDomain + "\\" + myUsername;
182         authenticationInfo.setUserName( myNTDomainAndUser );
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( myNTDomainAndUser, wagon.getAuthenticationInfo().getUserName() );
194         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
195 
196 //        Credentials credentials = wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) );
197 //        assertTrue( credentials instanceof NTCredentials );
198 //
199 //        NTCredentials ntCredentials = (NTCredentials) credentials;
200 //        assertEquals( myNTDomain, ntCredentials.getDomain() );
201 //        assertEquals( myUsername, ntCredentials.getUserName() );
202 //        assertEquals( myPassword, ntCredentials.getPassword() );
203     }
204 
205     private static final class TestWagon
206         extends WebDavWagon
207     {
208         @Override
209         public void fillOutputData( OutputData outputData )
210             throws TransferFailedException
211         {
212 
213         }
214     }
215 
216 }