1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.ui.basicauth;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.DisabledException;
21  
22  
23  
24  import org.springframework.mock.web.MockHttpServletRequest;
25  import org.springframework.mock.web.MockHttpServletResponse;
26  
27  
28  /***
29   * Tests {@link BasicProcessingFilterEntryPoint}.
30   *
31   * @author Ben Alex
32   * @version $Id: BasicProcessingFilterEntryPointTests.java,v 1.4 2005/11/17 00:56:30 benalex Exp $
33   */
34  public class BasicProcessingFilterEntryPointTests extends TestCase {
35      //~ Constructors ===========================================================
36  
37      public BasicProcessingFilterEntryPointTests() {
38          super();
39      }
40  
41      public BasicProcessingFilterEntryPointTests(String arg0) {
42          super(arg0);
43      }
44  
45      //~ Methods ================================================================
46  
47      public final void setUp() throws Exception {
48          super.setUp();
49      }
50  
51      public static void main(String[] args) {
52          junit.textui.TestRunner.run(BasicProcessingFilterEntryPointTests.class);
53      }
54  
55      public void testDetectsMissingRealmName() throws Exception {
56          BasicProcessingFilterEntryPoint ep = new BasicProcessingFilterEntryPoint();
57  
58          try {
59              ep.afterPropertiesSet();
60              fail("Should have thrown IllegalArgumentException");
61          } catch (IllegalArgumentException expected) {
62              assertEquals("realmName must be specified", expected.getMessage());
63          }
64      }
65  
66      public void testGettersSetters() {
67          BasicProcessingFilterEntryPoint ep = new BasicProcessingFilterEntryPoint();
68          ep.setRealmName("realm");
69          assertEquals("realm", ep.getRealmName());
70      }
71  
72      public void testNormalOperation() throws Exception {
73          BasicProcessingFilterEntryPoint ep = new BasicProcessingFilterEntryPoint();
74          ep.setRealmName("hello");
75  
76          MockHttpServletRequest request = new MockHttpServletRequest();
77          request.setRequestURI("/some_path");
78          MockHttpServletResponse response = new MockHttpServletResponse();
79  
80          ep.afterPropertiesSet();
81  
82          String msg = "These are the jokes kid";
83          ep.commence(request, response, new DisabledException(msg));
84  
85          assertEquals(401, response.getStatus());
86          assertEquals(msg, response.getErrorMessage());
87  
88          assertEquals("Basic realm=\"hello\"",
89              response.getHeader("WWW-Authenticate"));
90      }
91  }