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
022import org.apache.http.auth.AuthScope;
023import org.apache.maven.wagon.shared.http.BasicAuthScope;
024import org.junit.Assert;
025import org.junit.Test;
026
027public class BasicAuthScopeTest
028{
029
030    /**
031     * Test AuthScope override with no overriding values set. Nothing should
032     * change in original host/port.
033     */
034    @Test
035    public void testGetScopeNothingOverridden()
036    {
037        BasicAuthScope scope = new BasicAuthScope();
038
039        AuthScope authScope = scope.getScope( "original.host.com", 3456 );
040        Assert.assertEquals( "original.host.com", authScope.getHost() );
041        Assert.assertEquals( 3456, authScope.getPort() );
042        Assert.assertEquals( AuthScope.ANY_REALM, authScope.getRealm() );
043    }
044
045    /**
046     * Test AuthScope override for all values overridden
047     */
048    @Test
049    public void testGetScopeAllOverridden()
050    {
051        BasicAuthScope scope = new BasicAuthScope();
052        scope.setHost( "override.host.com" );
053        scope.setPort( "1234" );
054        scope.setRealm( "override-realm" );
055        AuthScope authScope = scope.getScope( "original.host.com", 3456 );
056        Assert.assertEquals( "override.host.com", authScope.getHost() );
057        Assert.assertEquals( 1234, authScope.getPort() );
058        Assert.assertEquals( "override-realm", authScope.getRealm() );
059    }
060
061    /**
062     * Test AuthScope override for all values overridden with "ANY"
063     */
064    @Test
065    public void testGetScopeAllAny()
066    {
067        BasicAuthScope scope = new BasicAuthScope();
068        scope.setHost( "ANY" );
069        scope.setPort( "ANY" );
070        scope.setRealm( "ANY" );
071        AuthScope authScope = scope.getScope( "original.host.com", 3456 );
072        Assert.assertEquals( AuthScope.ANY_HOST, authScope.getHost() );
073        Assert.assertEquals( AuthScope.ANY_PORT, authScope.getPort() );
074        Assert.assertEquals( AuthScope.ANY_REALM, authScope.getRealm() );
075    }
076
077    /**
078     * Test AuthScope override for realm value overridden
079     */
080    @Test
081    public void testGetScopeRealmOverridden()
082    {
083        BasicAuthScope scope = new BasicAuthScope();
084        scope.setRealm( "override-realm" );
085        AuthScope authScope = scope.getScope( "original.host.com", 3456 );
086        Assert.assertEquals( "original.host.com", authScope.getHost() );
087        Assert.assertEquals( 3456, authScope.getPort() );
088        Assert.assertEquals( "override-realm", authScope.getRealm() );
089    }
090
091    /**
092     * Test AuthScope where original port is -1, which should result in ANY
093     */
094    @Test
095    public void testGetScopeOriginalPortIsNegativeOne()
096    {
097        BasicAuthScope scope = new BasicAuthScope();
098        AuthScope authScope = scope.getScope( "original.host.com", -1 );
099        Assert.assertEquals( AuthScope.ANY_PORT, authScope.getPort() );
100    }
101}