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.maven.wagon.FileTestUtils;
023import org.apache.maven.wagon.ResourceDoesNotExistException;
024import org.apache.maven.wagon.TransferFailedException;
025import org.apache.maven.wagon.Wagon;
026import org.apache.maven.wagon.authorization.AuthorizationException;
027import org.apache.maven.wagon.repository.Repository;
028import org.eclipse.jetty.servlet.ServletHolder;
029
030import java.io.File;
031
032/**
033 * User: jdumay Date: 24/01/2008 Time: 17:17:34
034 */
035public class HttpWagonErrorTest
036    extends HttpWagonHttpServerTestCase
037{
038    private int serverPort;
039
040    protected void setUp()
041        throws Exception
042    {
043        super.setUp();
044        ServletHolder servlets = new ServletHolder( new ErrorWithMessageServlet() );
045        context.addServlet( servlets, "/*" );
046        startServer();
047        serverPort = getPort();
048    }
049
050    public void testGet401()
051        throws Exception
052    {
053        Exception thrown = null;
054
055        try
056        {
057            Wagon wagon = getWagon();
058
059            Repository testRepository = new Repository();
060            testRepository.setUrl( "http://localhost:" + serverPort );
061
062            wagon.connect( testRepository );
063
064            File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
065            destFile.deleteOnExit();
066
067            wagon.get( "401", destFile );
068
069            wagon.disconnect();
070        }
071        catch ( Exception e )
072        {
073            thrown = e;
074        }
075        finally
076        {
077            stopServer();
078        }
079
080        assertNotNull( thrown );
081        assertEquals( AuthorizationException.class, thrown.getClass() );
082        assertEquals( "authentication failed for http://localhost:" + serverPort + "/401, status: 401 "
083            + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
084    }
085
086    public void testGet403()
087        throws Exception
088    {
089        Exception thrown = null;
090
091        try
092        {
093            Wagon wagon = getWagon();
094
095            Repository testRepository = new Repository();
096            testRepository.setUrl( "http://localhost:" + serverPort );
097
098            wagon.connect( testRepository );
099
100            File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
101            destFile.deleteOnExit();
102
103            wagon.get( "403", destFile );
104
105            wagon.disconnect();
106        }
107        catch ( Exception e )
108        {
109            thrown = e;
110        }
111        finally
112        {
113            stopServer();
114        }
115
116        assertNotNull( thrown );
117        assertEquals( AuthorizationException.class, thrown.getClass() );
118        assertEquals( "authorization failed for http://localhost:" + serverPort + "/403, status: 403 "
119            + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
120    }
121
122    public void testGet404()
123            throws Exception
124    {
125        Exception thrown = null;
126
127        try
128        {
129            Wagon wagon = getWagon();
130
131            Repository testRepository = new Repository();
132            testRepository.setUrl( "http://localhost:" + serverPort );
133
134            wagon.connect( testRepository );
135
136            File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
137            destFile.deleteOnExit();
138
139            wagon.get( "404", destFile );
140
141            wagon.disconnect();
142        }
143        catch ( Exception e )
144        {
145            thrown = e;
146        }
147        finally
148        {
149            stopServer();
150        }
151
152        assertNotNull( thrown );
153        assertEquals( ResourceDoesNotExistException.class, thrown.getClass() );
154        assertEquals( "resource missing at http://localhost:" + serverPort + "/404, status: 404 "
155            + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
156    }
157
158    public void testGet407()
159        throws Exception
160    {
161        Exception thrown = null;
162
163        try
164        {
165            Wagon wagon = getWagon();
166
167            Repository testRepository = new Repository();
168            testRepository.setUrl( "http://localhost:" + getPort() );
169
170            wagon.connect( testRepository );
171
172            File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
173            destFile.deleteOnExit();
174
175            wagon.get( "407", destFile );
176
177            wagon.disconnect();
178        }
179        catch ( Exception e )
180        {
181            thrown = e;
182        }
183        finally
184        {
185            stopServer();
186        }
187
188        assertNotNull( thrown );
189        assertEquals( AuthorizationException.class, thrown.getClass() );
190        assertEquals( "proxy authentication failed for http://localhost:" + serverPort + "/407, status: 407 "
191            + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
192    }
193
194    public void testGet500()
195        throws Exception
196    {
197        Exception thrown = null;
198
199        try
200        {
201            Wagon wagon = getWagon();
202
203            Repository testRepository = new Repository();
204            testRepository.setUrl( "http://localhost:" + serverPort );
205
206            wagon.connect( testRepository );
207
208            File destFile = FileTestUtils.createUniqueFile( getName(), getName() );
209            destFile.deleteOnExit();
210
211            wagon.get( "500", destFile );
212
213            wagon.disconnect();
214        }
215        catch ( Exception e )
216        {
217            thrown = e;
218        }
219        finally
220        {
221            stopServer();
222        }
223
224        assertNotNull( thrown );
225        assertEquals( TransferFailedException.class, thrown.getClass() );
226        assertEquals( "transfer failed for http://localhost:" + serverPort + "/500, status: 500 "
227            + ErrorWithMessageServlet.MESSAGE, thrown.getMessage() );
228    }
229}