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;
43 public class RssFeedServletTest
44 extends PlexusInSpringTestCase
46 private ServletRunner sr;
48 private ServletUnitClient client;
53 sr = new ServletRunner( getTestFile( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
54 client = sr.newClient();
57 public void testRetrieveServlet()
60 RssFeedServlet servlet =
61 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
62 assertNotNull( servlet );
65 public void testRequestNewArtifactsInRepo()
68 RssFeedServlet servlet =
69 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
70 assertNotNull( servlet );
72 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
74 BASE64Encoder encoder = new BASE64Encoder();
75 String userPass = "user1:password1";
76 String encodedUserPass = encoder.encode( userPass.getBytes() );
77 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
79 WebResponse response = client.getResponse( request );
80 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
81 assertNotNull( "Should have recieved a response", response );
82 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
85 public void testRequestNewVersionsOfArtifact()
88 RssFeedServlet servlet =
89 (RssFeedServlet) client.newInvocation(
90 "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
91 assertNotNull( servlet );
93 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
95 BASE64Encoder encoder = new BASE64Encoder();
96 String userPass = "user1:password1";
97 String encodedUserPass = encoder.encode( userPass.getBytes() );
98 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
100 WebResponse response = client.getResponse( request );
101 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
102 assertNotNull( "Should have recieved a response", response );
103 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
106 public void testInvalidRequest()
109 RssFeedServlet servlet =
110 (RssFeedServlet) client.newInvocation(
111 "http://localhost/feeds?invalid_param=xxx" ).getServlet();
112 assertNotNull( servlet );
116 WebResponse response = client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
118 catch ( HttpException he )
120 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST, he.getResponseCode() );
124 public void testInvalidAuthenticationRequest()
127 RssFeedServlet servlet =
128 (RssFeedServlet) client.newInvocation(
129 "http://localhost/feeds/unauthorized-repo" ).getServlet();
130 assertNotNull( servlet );
133 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
135 Encoder encoder = new Base64();
136 String userPass = "unauthUser:unauthPass";
137 String encodedUserPass = new String( ( byte[] ) encoder.encode( userPass.getBytes() ) );
138 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
142 WebResponse response = client.getResponse( request );
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 WebResponse response = client.getResponse( request );
170 catch ( HttpException he )
172 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED, he.getResponseCode() );
177 protected String getPlexusConfigLocation()
179 return "org/apache/maven/archiva/web/rss/RssFeedServletTest.xml";
183 protected void tearDown()
186 if ( client != null )
188 client.clearContents();