1 package org.apache.archiva.web.rss;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.meterware.httpunit.GetMethodWebRequest;
23 import com.meterware.httpunit.HttpException;
24 import com.meterware.httpunit.WebRequest;
25 import com.meterware.httpunit.WebResponse;
26 import com.meterware.servletunit.ServletRunner;
27 import com.meterware.servletunit.ServletUnitClient;
28 import junit.framework.TestCase;
29 import org.apache.commons.codec.Encoder;
30 import org.apache.commons.codec.binary.Base64;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
39 import javax.servlet.http.HttpServletResponse;
41 import org.apache.archiva.test.ArchivaBlockJUnit4ClassRunner;
43 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
44 public class RssFeedServletTest
47 private ServletRunner sr;
49 private ServletUnitClient client;
51 static String PREVIOUS_ARCHIVA_PATH;
54 public static void initConfigurationPath()
57 PREVIOUS_ARCHIVA_PATH = System.getProperty( "archiva.user.configFileName" );
58 System.setProperty( "archiva.user.configFileName",
59 System.getProperty( "test.resources.path/" ) + "empty-archiva.xml" );
63 public static void restoreConfigurationPath()
66 System.setProperty( "archiva.user.configFileName", PREVIOUS_ARCHIVA_PATH );
73 sr = new ServletRunner( new File( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
74 client = sr.newClient();
78 public void tearDown()
83 client.clearContents();
95 public void testRetrieveServlet()
99 RssFeedServlet servlet =
100 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
101 assertNotNull( servlet );
105 public void testRequestNewArtifactsInRepo()
108 RssFeedServlet servlet =
109 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
110 assertNotNull( servlet );
112 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
114 Base64 encoder = new Base64( 0, new byte[0] );
115 String userPass = "user1:password1";
116 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
117 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
119 WebResponse response = client.getResponse( request );
120 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
121 assertNotNull( "Should have recieved a response", response );
122 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
127 public void testRequestNewVersionsOfArtifact()
130 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
131 "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
132 assertNotNull( servlet );
134 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
136 Base64 encoder = new Base64( 0, new byte[0] );
137 String userPass = "user1:password1";
138 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
139 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
141 WebResponse response = client.getResponse( request );
142 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
143 assertNotNull( "Should have recieved a response", response );
144 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
148 public void XXX_testInvalidRequest()
151 RssFeedServlet servlet =
152 (RssFeedServlet) client.newInvocation( "http://localhost/feeds?invalid_param=xxx" ).getServlet();
153 assertNotNull( servlet );
157 WebResponse resp = client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
158 assertEquals( HttpServletResponse.SC_BAD_REQUEST, resp.getResponseCode() );
160 catch ( HttpException he )
162 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST,
163 he.getResponseCode() );
168 public void XXX_testInvalidAuthenticationRequest()
171 RssFeedServlet servlet =
172 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet();
173 assertNotNull( servlet );
175 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
177 Encoder encoder = new Base64();
178 String userPass = "unauthUser:unauthPass";
179 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
180 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
184 WebResponse resp = client.getResponse( request );
185 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
187 catch ( HttpException he )
189 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
190 he.getResponseCode() );
195 public void XXX_testUnauthorizedRequest()
198 RssFeedServlet servlet =
199 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet();
200 assertNotNull( servlet );
202 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
204 Base64 encoder = new Base64( 0, new byte[0] );
205 String userPass = "user1:password1";
206 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
207 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
211 WebResponse resp = client.getResponse( request );
212 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
214 catch ( HttpException he )
216 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
217 he.getResponseCode() );