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;
34 import javax.servlet.http.HttpServletResponse;
36 public class RssFeedServletTest
39 private ServletRunner sr;
41 private ServletUnitClient client;
46 sr = new ServletRunner( new File( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
47 client = sr.newClient();
51 protected void tearDown()
56 client.clearContents();
67 public void testRetrieveServlet()
70 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
71 "http://localhost/feeds/test-repo" ).getServlet();
72 assertNotNull( servlet );
75 public void testRequestNewArtifactsInRepo()
78 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
79 "http://localhost/feeds/test-repo" ).getServlet();
80 assertNotNull( servlet );
82 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
84 Base64 encoder = new Base64(0, new byte[0]);
85 String userPass = "user1:password1";
86 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
87 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
89 WebResponse response = client.getResponse( request );
90 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
91 assertNotNull( "Should have recieved a response", response );
92 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
95 public void testRequestNewVersionsOfArtifact()
98 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
99 "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
100 assertNotNull( servlet );
102 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
104 Base64 encoder = new Base64(0, new byte[0]);
105 String userPass = "user1:password1";
106 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
107 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
109 WebResponse response = client.getResponse( request );
110 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
111 assertNotNull( "Should have recieved a response", response );
112 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
115 public void XXX_testInvalidRequest()
118 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
119 "http://localhost/feeds?invalid_param=xxx" ).getServlet();
120 assertNotNull( servlet );
124 WebResponse resp = client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
125 assertEquals( HttpServletResponse.SC_BAD_REQUEST, resp.getResponseCode() );
127 catch ( HttpException he )
129 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST,
130 he.getResponseCode() );
134 public void XXX_testInvalidAuthenticationRequest()
137 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
138 "http://localhost/feeds/unauthorized-repo" ).getServlet();
139 assertNotNull( servlet );
141 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
143 Encoder encoder = new Base64();
144 String userPass = "unauthUser:unauthPass";
145 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
146 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
150 WebResponse resp = client.getResponse( request );
151 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
153 catch ( HttpException he )
155 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
156 he.getResponseCode() );
160 public void XXX_testUnauthorizedRequest()
163 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
164 "http://localhost/feeds/unauthorized-repo" ).getServlet();
165 assertNotNull( servlet );
167 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
169 Base64 encoder = new Base64(0, new byte[0]);
170 String userPass = "user1:password1";
171 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
172 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
176 WebResponse resp = client.getResponse( request );
177 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
179 catch ( HttpException he )
181 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
182 he.getResponseCode() );