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( "Cache-store", "no-store" );
97          // method.addRequestHeader( "Pragma", "no-cache" );
98          // "Accept-Encoding" is automatically set by HttpClient at runtime
99  
100         Header header = method.getFirstHeader( "Cache-control" );
101         assertNotNull( header );
102         assertEquals( "no-cache", header.getValue() );
103 
104         header = method.getFirstHeader( "Cache-store" );
105         assertNotNull( header );
106         assertEquals( "no-store", header.getValue() );
107 
108         header = method.getFirstHeader( "Pragma" );
109         assertNotNull( header );
110         assertEquals( "no-cache", header.getValue() );
111     }
112 
113     public void testTurnOffDefaultHeaders()
114     {
115         HttpConfiguration config = new HttpConfiguration();
116         config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
117 
118         TestWagon wagon = new TestWagon();
119         wagon.setHttpConfiguration( config );
120 
121         HttpHead method = new HttpHead();
122         wagon.setHeaders( method );
123 
124         // these are the default headers.
125         // method.addRequestHeader( "Cache-control", "no-cache" );
126         // method.addRequestHeader( "Cache-store", "no-store" );
127         // method.addRequestHeader( "Pragma", "no-cache" );
128 
129         Header header = method.getFirstHeader( "Cache-control" );
130         assertNull( header );
131 
132         header = method.getFirstHeader( "Cache-store" );
133         assertNull( header );
134 
135         header = method.getFirstHeader( "Pragma" );
136         assertNull( header );
137     }
138 
139     @Ignore("not sure how to test this")
140     public void testNTCredentialsWithUsernameNull()
141         throws AuthenticationException, ConnectionException
142     {
143         TestWagon wagon = new TestWagon();
144 
145         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
146         wagon.connect( repository );
147 
148         wagon.openConnection();
149 
150         assertNull( wagon.getAuthenticationInfo().getUserName() );
151         assertNull( wagon.getAuthenticationInfo().getPassword() );
152 
153         //assertFalse( wagon.getHttpClient()..getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
154     }
155 
156     @Ignore("not sure how to test this")
157     public void testNTCredentialsNoNTDomain()
158         throws AuthenticationException, ConnectionException
159     {
160         TestWagon wagon = new TestWagon();
161 
162         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
163         String myUsernameNoNTDomain = "myUserNameNoNTDomain";
164         authenticationInfo.setUserName( myUsernameNoNTDomain );
165 
166         String myPassword = "myPassword";
167         authenticationInfo.setPassword( myPassword );
168 
169         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
170 
171         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
172 
173         wagon.openConnection();
174 
175         assertEquals( myUsernameNoNTDomain, wagon.getAuthenticationInfo().getUserName() );
176         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
177 
178         //assertFalse( wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) ) instanceof NTCredentials );
179     }
180 
181     @Ignore("not sure how to test this")
182     public void testNTCredentialsWithNTDomain()
183         throws AuthenticationException, ConnectionException
184     {
185         TestWagon wagon = new TestWagon();
186 
187         AuthenticationInfo authenticationInfo = new AuthenticationInfo();
188         String myNTDomain = "myNTDomain";
189         String myUsername = "myUsername";
190         String myNTDomainAndUser = myNTDomain + "\\" + myUsername;
191         authenticationInfo.setUserName( myNTDomainAndUser );
192 
193         String myPassword = "myPassword";
194         authenticationInfo.setPassword( myPassword );
195 
196         Repository repository = new Repository( "mockRepoId", "mockRepoURL" );
197 
198         wagon.connect( repository, authenticationInfo, (ProxyInfo) null );
199 
200         wagon.openConnection();
201 
202         assertEquals( myNTDomainAndUser, wagon.getAuthenticationInfo().getUserName() );
203         assertEquals( myPassword, wagon.getAuthenticationInfo().getPassword() );
204 
205 //        Credentials credentials = wagon.getClient().getState().getCredentials( new AuthScope( null, 0 ) );
206 //        assertTrue( credentials instanceof NTCredentials );
207 //
208 //        NTCredentials ntCredentials = (NTCredentials) credentials;
209 //        assertEquals( myNTDomain, ntCredentials.getDomain() );
210 //        assertEquals( myUsername, ntCredentials.getUserName() );
211 //        assertEquals( myPassword, ntCredentials.getPassword() );
212     }
213 
214     private static final class TestWagon
215         extends WebDavWagon
216     {
217         @Override
218         public void fillOutputData( OutputData outputData )
219             throws TransferFailedException
220         {
221 
222         }
223     }
224 
225 }