001package org.apache.maven.wagon.providers.http;
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
022
023import junit.framework.TestCase;
024
025import org.apache.http.Header;
026
027
028import org.apache.http.client.config.RequestConfig;
029import org.apache.http.client.methods.HttpHead;
030import org.apache.maven.wagon.OutputData;
031import org.apache.maven.wagon.TransferFailedException;
032import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
033import org.apache.maven.wagon.shared.http.ConfigurationUtils;
034import org.apache.maven.wagon.shared.http.HttpConfiguration;
035import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
036
037public class HttpClientWagonTest
038    extends TestCase
039{
040
041    public void testSetMaxRedirectsParamViaConfig()
042    {
043        HttpMethodConfiguration methodConfig = new HttpMethodConfiguration();
044        int maxRedirects = 2;
045        methodConfig.addParam("http.protocol.max-redirects", "%i," + maxRedirects);
046
047        HttpConfiguration config = new HttpConfiguration();
048        config.setAll( methodConfig );
049
050        HttpHead method = new HttpHead();
051        RequestConfig.Builder builder = RequestConfig.custom();
052        ConfigurationUtils.copyConfig( config.getMethodConfiguration( method ), builder );
053        RequestConfig requestConfig = builder.build();
054
055        assertEquals(2, requestConfig.getMaxRedirects());
056    }
057
058    public void testDefaultHeadersUsedByDefault()
059    {
060        HttpConfiguration config = new HttpConfiguration();
061        config.setAll( new HttpMethodConfiguration() );
062
063        TestWagon wagon = new TestWagon();
064        wagon.setHttpConfiguration( config );
065
066        HttpHead method = new HttpHead();
067        wagon.setHeaders( method );
068
069        // these are the default headers.
070        // method.addRequestHeader( "Cache-control", "no-cache" );
071        // method.addRequestHeader( "Pragma", "no-cache" );
072        // "Accept-Encoding" is automatically set by HttpClient at runtime
073
074        Header header = method.getFirstHeader( "Cache-control" );
075        assertNotNull( header );
076        assertEquals( "no-cache", header.getValue() );
077
078        header = method.getFirstHeader( "Pragma" );
079        assertNotNull( header );
080        assertEquals( "no-cache", header.getValue() );
081    }
082
083    public void testTurnOffDefaultHeaders()
084    {
085        HttpConfiguration config = new HttpConfiguration();
086        config.setAll( new HttpMethodConfiguration().setUseDefaultHeaders( false ) );
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
098        Header header = method.getFirstHeader( "Cache-control" );
099        assertNull( header );
100
101        header = method.getFirstHeader( "Pragma" );
102        assertNull( header );
103    }
104
105    private static final class TestWagon
106        extends AbstractHttpClientWagon
107    {
108        @Override
109        public void fillOutputData( OutputData outputData )
110            throws TransferFailedException
111        {
112
113        }
114    }
115
116}