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 com.meterware.httpunit.GetMethodWebRequest;
25 import com.meterware.httpunit.HttpException;
26 import com.meterware.httpunit.WebRequest;
27 import com.meterware.httpunit.WebResponse;
28 import com.meterware.servletunit.ServletRunner;
29 import com.meterware.servletunit.ServletUnitClient;
30 import org.apache.commons.codec.Encoder;
31 import org.apache.commons.codec.binary.Base64;
32 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
33 import sun.misc.BASE64Encoder;
35 public class RssFeedServletTest
36 extends PlexusInSpringTestCase
38 private ServletRunner sr;
40 private ServletUnitClient client;
45 sr = new ServletRunner( getTestFile( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
46 client = sr.newClient();
49 public void testRetrieveServlet()
52 RssFeedServlet servlet =
53 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
54 assertNotNull( servlet );
57 public void testRequestNewArtifactsInRepo()
60 RssFeedServlet servlet =
61 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
62 assertNotNull( servlet );
64 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
66 BASE64Encoder encoder = new BASE64Encoder();
67 String userPass = "user1:password1";
68 String encodedUserPass = encoder.encode( userPass.getBytes() );
69 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
71 WebResponse response = client.getResponse( request );
72 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
73 assertNotNull( "Should have recieved a response", response );
74 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
77 public void testRequestNewVersionsOfArtifact()
80 RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
81 "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
82 assertNotNull( servlet );
84 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
86 BASE64Encoder encoder = new BASE64Encoder();
87 String userPass = "user1:password1";
88 String encodedUserPass = encoder.encode( userPass.getBytes() );
89 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
91 WebResponse response = client.getResponse( request );
92 assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
93 assertNotNull( "Should have recieved a response", response );
94 assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
97 public void XXX_testInvalidRequest()
100 RssFeedServlet servlet =
101 (RssFeedServlet) client.newInvocation( "http://localhost/feeds?invalid_param=xxx" ).getServlet();
102 assertNotNull( servlet );
106 WebResponse resp = client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
107 assertEquals( HttpServletResponse.SC_BAD_REQUEST, resp.getResponseCode() );
109 catch ( HttpException he )
111 assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST,
112 he.getResponseCode() );
116 public void XXX_testInvalidAuthenticationRequest()
119 RssFeedServlet servlet =
120 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet();
121 assertNotNull( servlet );
123 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
125 Encoder encoder = new Base64();
126 String userPass = "unauthUser:unauthPass";
127 String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
128 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
132 WebResponse resp = client.getResponse( request );
133 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
135 catch ( HttpException he )
137 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
138 he.getResponseCode() );
142 public void XXX_testUnauthorizedRequest()
145 RssFeedServlet servlet =
146 (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet();
147 assertNotNull( servlet );
149 WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
151 BASE64Encoder encoder = new BASE64Encoder();
152 String userPass = "user1:password1";
153 String encodedUserPass = encoder.encode( userPass.getBytes() );
154 request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
158 WebResponse resp = client.getResponse( request );
159 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
161 catch ( HttpException he )
163 assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
164 he.getResponseCode() );
169 protected String getPlexusConfigLocation()
171 return "org/apache/maven/archiva/web/rss/RssFeedServletTest.xml";
175 protected void tearDown()
178 if ( client != null )
180 client.clearContents();