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.apache.jackrabbit.core.RepositoryImpl;
32 import org.apache.jackrabbit.core.util.RepositoryLock;
36 import javax.servlet.http.HttpServletResponse;
38 public class RssFeedServletTest
41 private ServletRunner sr;
43 private ServletUnitClient client;
48 sr = new ServletRunner( new File( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
49 client = sr.newClient();
53 protected void tearDown()
58 client.clearContents();
69 public void testRetrieveServlet()
73 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
74 "http://localhost/feeds/test-repo" ).getServlet();
75 assertNotNull( servlet );
78 public void testRequestNewArtifactsInRepo()
81 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
82 "http://localhost/feeds/test-repo" ).getServlet();
83 assertNotNull( servlet );
85 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
87 Base64 encoder = new Base64(0, new byte[0]);
88 String userPass = "user1:password1";
89 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
90 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
92 WebResponse response = client.getResponse( request );
93 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
94 assertNotNull( "Should have recieved a response", response );
95 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
99 public void testRequestNewVersionsOfArtifact()
102 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
103 "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
104 assertNotNull( servlet );
106 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
108 Base64 encoder = new Base64(0, new byte[0]);
109 String userPass = "user1:password1";
110 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
111 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
113 WebResponse response = client.getResponse( request );
114 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
115 assertNotNull( "Should have recieved a response", response );
116 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
119 public void XXX_testInvalidRequest()
122 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
123 "http://localhost/feeds?invalid_param=xxx" ).getServlet();
124 assertNotNull( servlet );
128 WebResponse resp = client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
129 assertEquals( HttpServletResponse.SC_BAD_REQUEST, resp.getResponseCode() );
131 catch ( HttpException he )
133 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST,
134 he.getResponseCode() );
138 public void XXX_testInvalidAuthenticationRequest()
141 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
142 "http://localhost/feeds/unauthorized-repo" ).getServlet();
143 assertNotNull( servlet );
145 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
147 Encoder encoder = new Base64();
148 String userPass = "unauthUser:unauthPass";
149 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
150 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
154 WebResponse resp = client.getResponse( request );
155 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
157 catch ( HttpException he )
159 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
160 he.getResponseCode() );
164 public void XXX_testUnauthorizedRequest()
167 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
168 "http://localhost/feeds/unauthorized-repo" ).getServlet();
169 assertNotNull( servlet );
171 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
173 Base64 encoder = new Base64(0, new byte[0]);
174 String userPass = "user1:password1";
175 String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
176 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
180 WebResponse resp = client.getResponse( request );
181 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
183 catch ( HttpException he )
185 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
186 he.getResponseCode() );