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 java.io.IOException;
24 import javax.servlet.http.HttpServletResponse;
26 import org.apache.commons.codec.Encoder;
27 import org.apache.commons.codec.binary.Base64;
28 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30 import sun.misc.BASE64Encoder;
32 import com.meterware.httpunit.GetMethodWebRequest;
33 import com.meterware.httpunit.HttpException;
34 import com.meterware.httpunit.WebRequest;
35 import com.meterware.httpunit.WebResponse;
36 import com.meterware.servletunit.ServletRunner;
37 import com.meterware.servletunit.ServletUnitClient;
41 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
44 public class RssFeedServletTest
45 extends PlexusInSpringTestCase
47 private ServletRunner sr;
49 private ServletUnitClient client;
54 sr = new ServletRunner( getTestFile( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
55 client = sr.newClient();
58 public void testRetrieveServlet()
61 RssFeedServlet servlet =
62 (RssFeedServlet) client.newInvocation( "http://localhost/rss/rss_feeds?repoId=test-repo" ).getServlet();
63 assertNotNull( servlet );
66 public void testRequestNewArtifactsInRepo()
69 RssFeedServlet servlet =
70 (RssFeedServlet) client.newInvocation( "http://localhost/rss/rss_feeds?repoId=test-repo" ).getServlet();
71 assertNotNull( servlet );
73 WebRequest request = new GetMethodWebRequest( "http://localhost/rss/rss_feeds?repoId=test-repo" );
75 BASE64Encoder encoder = new BASE64Encoder();
76 String userPass = "user1:password1";
77 String encodedUserPass = encoder.encode( userPass.getBytes() );
78 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
80 WebResponse response = client.getResponse( request );
81 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
82 assertNotNull( "Should have recieved a response", response );
83 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
86 public void testRequestNewVersionsOfArtifact()
89 RssFeedServlet servlet =
90 (RssFeedServlet) client.newInvocation(
91 "http://localhost/rss/rss_feeds?groupId=org.apache.archiva&artifactId=artifact-two" ).getServlet();
92 assertNotNull( servlet );
94 WebRequest request = new GetMethodWebRequest( "http://localhost/rss/rss_feeds?groupId=org.apache.archiva&artifactId=artifact-two" );
96 BASE64Encoder encoder = new BASE64Encoder();
97 String userPass = "user1:password1";
98 String encodedUserPass = encoder.encode( userPass.getBytes() );
99 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
101 WebResponse response = client.getResponse( request );
102 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
103 assertNotNull( "Should have recieved a response", response );
104 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
107 public void testInvalidRequest()
110 RssFeedServlet servlet =
111 (RssFeedServlet) client.newInvocation(
112 "http://localhost/rss/rss_feeds?invalid_param=xxx" ).getServlet();
113 assertNotNull( servlet );
117 WebResponse response = client.getResponse( "http://localhost/rss/rss_feeds?invalid_param=xxx" );
119 catch ( HttpException he )
121 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST, he.getResponseCode() );
125 public void testInvalidAuthenticationRequest()
128 RssFeedServlet servlet =
129 (RssFeedServlet) client.newInvocation(
130 "http://localhost/rss/rss_feeds?repoId=unauthorized-repo" ).getServlet();
131 assertNotNull( servlet );
134 WebRequest request = new GetMethodWebRequest( "http://localhost/rss/rss_feeds?repoId=unauthorized-repo" );
136 Encoder encoder = new Base64();
137 String userPass = "unauthUser:unauthPass";
138 String encodedUserPass = new String( ( byte[] ) encoder.encode( userPass.getBytes() ) );
139 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
143 WebResponse response = client.getResponse( request );
145 catch ( HttpException he )
147 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED, he.getResponseCode() );
151 public void testUnauthorizedRequest()
154 RssFeedServlet servlet =
155 (RssFeedServlet) client.newInvocation(
156 "http://localhost/rss/rss_feeds?repoId=unauthorized-repo" ).getServlet();
157 assertNotNull( servlet );
160 WebRequest request = new GetMethodWebRequest( "http://localhost/rss/rss_feeds?repoId=unauthorized-repo" );
162 BASE64Encoder encoder = new BASE64Encoder();
163 String userPass = "user1:password1";
164 String encodedUserPass = encoder.encode( userPass.getBytes() );
165 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
169 WebResponse response = client.getResponse( request );
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();