001package org.apache.maven.wagon.providers.webdav;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import junit.framework.TestCase;
023import org.apache.http.Header;
024import org.apache.http.client.methods.HttpHead;
025import org.apache.http.client.params.HttpClientParams;
026import org.apache.http.params.HttpParams;
027import org.apache.maven.wagon.ConnectionException;
028import org.apache.maven.wagon.OutputData;
029import org.apache.maven.wagon.TransferFailedException;
030import org.apache.maven.wagon.authentication.AuthenticationException;
031import org.apache.maven.wagon.authentication.AuthenticationInfo;
032import org.apache.maven.wagon.proxy.ProxyInfo;
033import org.apache.maven.wagon.repository.Repository;
034import org.apache.maven.wagon.shared.http.HttpConfiguration;
035import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
036import org.junit.Ignore;
037
038public class HttpClientWagonTest
039    extends TestCase
040{
041
042    @Ignore("not sure how to test this")
043    public void testSetPreemptiveAuthParamViaConfig()
044    {
045        HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
046        methodConfig.setUsePreemptive( true );
047
048        HttpConfiguration config = new HttpConfiguration();
049        config.setAll( methodConfig );
050
051        TestWagon wagon = new TestWagon();
052        wagon.setHttpConfiguration( config );
053
054        HttpHead method = new HttpHead();
055        wagon.setHeaders( method );
056
057        HttpParams params = method.getParams();
058        assertNotNull( params );
059        //assertTrue( params.isParameterTrue( HttpClientParams.PREEMPTIVE_AUTHENTICATION ) );
060    }
061
062//    @Ignore("not sure how to test this")
063//    public void testSetMaxRedirectsParamViaConfig()
064//    {
065//        HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
066//        int maxRedirects = 2;
067//        methodConfig.addParam( HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects );
068//
069//        HttpConfiguration config = new HttpConfiguration();
070//        config.setAll( methodConfig );
071//
072//        TestWagon wagon = new TestWagon();
073//        wagon.setHttpConfiguration( config );
074//
075//        HttpHead method = new HttpHead();
076//        wagon.setParameters( method );
077//
078//        HttpParams params = method.getParams();
079//        assertNotNull( params );
080//        assertEquals( maxRedirects, params.getIntParameter( HttpClientParams.MAX_REDIRECTS, -1 ) );
081//    }
082
083    public void testDefaultHeadersUsedByDefault()
084    {
085        HttpConfiguration config = new HttpConfiguration();
086        config.setAll( new HttpMethodConfiguration() );
087
088        TestWagon wagon = new TestWagon();
089        wagon.setHttpConfiguration( config );
090
091        HttpHead method = new HttpHead();
092        wagon.setHeaders( method );
093
094        // these are the default headers.
095        // method.addRequestHeader( "Cache-control", "no-cache" );
096        // method.addRequestHeader( "Pragma", "no-cache" );
097        // "Accept-Encoding" is automatically set by HttpClient at runtime
098
099        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}