1 package org.apache.maven.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 javax.servlet.http.HttpServletResponse;
24 import org.apache.commons.codec.Encoder;
25 import org.apache.commons.codec.binary.Base64;
26 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
28 import sun.misc.BASE64Encoder;
30 import com.meterware.httpunit.GetMethodWebRequest;
31 import com.meterware.httpunit.HttpException;
32 import com.meterware.httpunit.WebRequest;
33 import com.meterware.httpunit.WebResponse;
34 import com.meterware.servletunit.ServletRunner;
35 import com.meterware.servletunit.ServletUnitClient;
41 public class RssFeedServletTest
42 extends PlexusInSpringTestCase
44 private ServletRunner sr;
46 private ServletUnitClient client;
51 sr = new ServletRunner( getTestFile( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
52 client = sr.newClient();
55 public void testRetrieveServlet()
58 RssFeedServlet servlet =
59 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
60 assertNotNull( servlet );
63 public void testRequestNewArtifactsInRepo()
66 RssFeedServlet servlet =
67 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
68 assertNotNull( servlet );
70 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
72 BASE64Encoder encoder = new BASE64Encoder();
73 String userPass = "user1:password1";
74 String encodedUserPass = encoder.encode( userPass.getBytes() );
75 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
77 WebResponse response = client.getResponse( request );
78 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
79 assertNotNull( "Should have recieved a response", response );
80 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
83 public void testRequestNewVersionsOfArtifact()
86 RssFeedServlet servlet =
87 (RssFeedServlet) client.newInvocation(
88 "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
89 assertNotNull( servlet );
91 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
93 BASE64Encoder encoder = new BASE64Encoder();
94 String userPass = "user1:password1";
95 String encodedUserPass = encoder.encode( userPass.getBytes() );
96 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
98 WebResponse response = client.getResponse( request );
99 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
100 assertNotNull( "Should have recieved a response", response );
101 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
104 public void testInvalidRequest()
107 RssFeedServlet servlet =
108 (RssFeedServlet) client.newInvocation(
109 "http://localhost/feeds?invalid_param=xxx" ).getServlet();
110 assertNotNull( servlet );
114 client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
115 fail( "Expected exception" );
117 catch ( HttpException he )
119 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST, he.getResponseCode() );
123 public void testInvalidAuthenticationRequest()
126 RssFeedServlet servlet =
127 (RssFeedServlet) client.newInvocation(
128 "http://localhost/feeds/unauthorized-repo" ).getServlet();
129 assertNotNull( servlet );
132 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
134 Encoder encoder = new Base64();
135 String userPass = "unauthUser:unauthPass";
136 String encodedUserPass = new String( ( byte[] ) encoder.encode( userPass.getBytes() ) );
137 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
141 client.getResponse( request );
142 fail( "Expected exception" );
144 catch ( HttpException he )
146 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED, he.getResponseCode() );
150 public void testUnauthorizedRequest()
153 RssFeedServlet servlet =
154 (RssFeedServlet) client.newInvocation(
155 "http://localhost/feeds/unauthorized-repo" ).getServlet();
156 assertNotNull( servlet );
159 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
161 BASE64Encoder encoder = new BASE64Encoder();
162 String userPass = "user1:password1";
163 String encodedUserPass = encoder.encode( userPass.getBytes() );
164 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
168 client.getResponse( request );
169 fail( "Expected exception" );
171 catch ( HttpException he )
173 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED, he.getResponseCode() );
178 protected String getPlexusConfigLocation()
180 return "org/apache/maven/archiva/web/rss/RssFeedServletTest.xml";
184 protected void tearDown()
187 if ( client != null )
189 client.clearContents();