View Javadoc

1   package org.apache.archiva.web.rss;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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.junit.After;
32  import org.junit.AfterClass;
33  import org.junit.Before;
34  import org.junit.BeforeClass;
35  import org.junit.Ignore;
36  import org.junit.Test;
37  import org.junit.runner.RunWith;
38  
39  import javax.servlet.http.HttpServletResponse;
40  import java.io.File;
41  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
42  
43  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
44  public class RssFeedServletTest
45      extends TestCase
46  {
47      private ServletRunner sr;
48  
49      private ServletUnitClient client;
50  
51      static String PREVIOUS_ARCHIVA_PATH;
52  
53      @BeforeClass
54      public static void initConfigurationPath()
55          throws Exception
56      {
57          PREVIOUS_ARCHIVA_PATH = System.getProperty( "archiva.user.configFileName" );
58          System.setProperty( "archiva.user.configFileName",
59                              System.getProperty( "test.resources.path/" ) + "empty-archiva.xml" );
60      }
61  
62      @AfterClass
63      public static void restoreConfigurationPath()
64          throws Exception
65      {
66          System.setProperty( "archiva.user.configFileName", PREVIOUS_ARCHIVA_PATH );
67      }
68  
69      @Before
70      public void setUp()
71          throws Exception
72      {
73          sr = new ServletRunner( new File( "src/test/webapp/WEB-INF/feedServletTest-web.xml" ) );
74          client = sr.newClient();
75      }
76  
77      @After
78      public void tearDown()
79          throws Exception
80      {
81          if ( client != null )
82          {
83              client.clearContents();
84          }
85  
86          if ( sr != null )
87          {
88              sr.shutDown();
89          }
90  
91          super.tearDown();
92      }
93  
94      @Test
95      public void testRetrieveServlet()
96          throws Exception
97      {
98  
99          RssFeedServlet servlet =
100             (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
101         assertNotNull( servlet );
102     }
103 
104     @Test
105     public void testRequestNewArtifactsInRepo()
106         throws Exception
107     {
108         RssFeedServlet servlet =
109             (RssFeedServlet) client.newInvocation( "http://localhost/feeds/test-repo" ).getServlet();
110         assertNotNull( servlet );
111 
112         WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/test-repo" );
113 
114         Base64 encoder = new Base64( 0, new byte[0] );
115         String userPass = "user1:password1";
116         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
117         request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
118 
119         WebResponse response = client.getResponse( request );
120         assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
121         assertNotNull( "Should have recieved a response", response );
122         assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
123 
124     }
125 
126     @Test
127     public void testRequestNewVersionsOfArtifact()
128         throws Exception
129     {
130         RssFeedServlet servlet = (RssFeedServlet) client.newInvocation(
131             "http://localhost/feeds/org/apache/archiva/artifact-two" ).getServlet();
132         assertNotNull( servlet );
133 
134         WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
135 
136         Base64 encoder = new Base64( 0, new byte[0] );
137         String userPass = "user1:password1";
138         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
139         request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
140 
141         WebResponse response = client.getResponse( request );
142         assertEquals( RssFeedServlet.MIME_TYPE, response.getHeaderField( "CONTENT-TYPE" ) );
143         assertNotNull( "Should have recieved a response", response );
144         assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK, response.getResponseCode() );
145     }
146 
147     @Ignore
148     public void XXX_testInvalidRequest()
149         throws Exception
150     {
151         RssFeedServlet servlet =
152             (RssFeedServlet) client.newInvocation( "http://localhost/feeds?invalid_param=xxx" ).getServlet();
153         assertNotNull( servlet );
154 
155         try
156         {
157             WebResponse resp = client.getResponse( "http://localhost/feeds?invalid_param=xxx" );
158             assertEquals( HttpServletResponse.SC_BAD_REQUEST, resp.getResponseCode() );
159         }
160         catch ( HttpException he )
161         {
162             assertEquals( "Should have been a bad request response code.", HttpServletResponse.SC_BAD_REQUEST,
163                           he.getResponseCode() );
164         }
165     }
166 
167     @Ignore
168     public void XXX_testInvalidAuthenticationRequest()
169         throws Exception
170     {
171         RssFeedServlet servlet =
172             (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet();
173         assertNotNull( servlet );
174 
175         WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
176 
177         Encoder encoder = new Base64();
178         String userPass = "unauthUser:unauthPass";
179         String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
180         request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
181 
182         try
183         {
184             WebResponse resp = client.getResponse( request );
185             assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
186         }
187         catch ( HttpException he )
188         {
189             assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
190                           he.getResponseCode() );
191         }
192     }
193 
194     @Ignore
195     public void XXX_testUnauthorizedRequest()
196         throws Exception
197     {
198         RssFeedServlet servlet =
199             (RssFeedServlet) client.newInvocation( "http://localhost/feeds/unauthorized-repo" ).getServlet();
200         assertNotNull( servlet );
201 
202         WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/unauthorized-repo" );
203 
204         Base64 encoder = new Base64( 0, new byte[0] );
205         String userPass = "user1:password1";
206         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
207         request.setHeaderField( "Authorization", "BASIC " + encodedUserPass );
208 
209         try
210         {
211             WebResponse resp = client.getResponse( request );
212             assertEquals( HttpServletResponse.SC_UNAUTHORIZED, resp.getResponseCode() );
213         }
214         catch ( HttpException he )
215         {
216             assertEquals( "Should have been a unauthorized response.", HttpServletResponse.SC_UNAUTHORIZED,
217                           he.getResponseCode() );
218         }
219     }
220 
221 
222 }