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.commons.httpclient.Credentials; 024import org.apache.commons.httpclient.Header; 025import org.apache.commons.httpclient.NTCredentials; 026import org.apache.commons.httpclient.auth.AuthScope; 027import org.apache.commons.httpclient.methods.HeadMethod; 028import org.apache.commons.httpclient.params.HttpClientParams; 029import org.apache.commons.httpclient.params.HttpMethodParams; 030import org.apache.maven.wagon.ConnectionException; 031import org.apache.maven.wagon.OutputData; 032import org.apache.maven.wagon.TransferFailedException; 033import org.apache.maven.wagon.authentication.AuthenticationException; 034import org.apache.maven.wagon.authentication.AuthenticationInfo; 035import org.apache.maven.wagon.proxy.ProxyInfo; 036import org.apache.maven.wagon.repository.Repository; 037 038public class HttpClientWagonTest 039 extends TestCase 040{ 041 042 public void testSetPreemptiveAuthParamViaConfig() 043 { 044 HttpMethodConfiguration methodConfig = new HttpMethodConfiguration(); 045 methodConfig.addParam( HttpClientParams.PREEMPTIVE_AUTHENTICATION, "%b,true" ); 046 047 HttpConfiguration config = new HttpConfiguration(); 048 config.setAll( methodConfig ); 049 050 TestWagon wagon = new TestWagon(); 051 wagon.setHttpConfiguration( config ); 052 053 HeadMethod method = new HeadMethod(); 054 wagon.setParameters( method ); 055 056 HttpMethodParams params = method.getParams(); 057 assertNotNull( params ); 058 assertTrue( params.isParameterTrue( HttpClientParams.PREEMPTIVE_AUTHENTICATION ) ); 059 } 060 061 public void testSetMaxRedirectsParamViaConfig() 062 { 063 HttpMethodConfiguration methodConfig = new HttpMethodConfiguration(); 064 int maxRedirects = 2; 065 methodConfig.addParam( HttpClientParams.MAX_REDIRECTS, "%i," + maxRedirects ); 066 067 HttpConfiguration config = new HttpConfiguration(); 068 config.setAll( methodConfig ); 069 070 TestWagon wagon = new TestWagon(); 071 wagon.setHttpConfiguration( config ); 072 073 HeadMethod method = new HeadMethod(); 074 wagon.setParameters( method ); 075 076 HttpMethodParams params = method.getParams(); 077 assertNotNull( params ); 078 assertEquals( maxRedirects, params.getIntParameter( HttpClientParams.MAX_REDIRECTS, -1 ) ); 079 } 080 081 public void testDefaultHeadersUsedByDefault() 082 { 083 HttpConfiguration config = new HttpConfiguration(); 084 config.setAll( new HttpMethodConfiguration() ); 085 086 TestWagon wagon = new TestWagon(); 087 wagon.setHttpConfiguration( config ); 088 089 HeadMethod method = new HeadMethod(); 090 wagon.setHeaders( method ); 091 092 // these are the default headers. 093 // method.addRequestHeader( "Cache-control", "no-cache" ); 094 // method.addRequestHeader( "Cache-store", "no-store" ); 095 // method.addRequestHeader( "Pragma", "no-cache" ); 096 // method.addRequestHeader( "Expires", "0" ); 097 // method.addRequestHeader( "Accept-Encoding", "gzip" ); 098 099 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}