View Javadoc
1   /**
2    *
3    */
4   package org.melati.login.test;
5   
6   
7   import org.melati.Melati;
8   import org.melati.login.HttpAuthorizationMelatiException;
9   import org.melati.login.HttpBasicAuthenticationAccessHandler;
10  import org.melati.servlet.test.MockHttpServletRequest;
11  import org.melati.servlet.test.MockHttpServletResponse;
12  
13  import org.apache.commons.codec.binary.Base64;
14  
15  
16  /**
17   * @author timp
18   *
19   */
20  public class HttpBasicAuthenticationAccessHandlerTest extends AccessHandlerTestAbstract {
21  
22    /**
23     * @param name
24     */
25    public HttpBasicAuthenticationAccessHandlerTest(String name) {
26      super(name);
27    }
28  
29    public void setUp() throws Exception {
30      super.setUp();
31      MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
32      MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
33      mockHttpServletRequest.setHeader("Authorization", 
34          "Basic " + new String(Base64.encodeBase64("_administrator_:FIXME".getBytes())));
35      m.setRequest(mockHttpServletRequest);
36      m.setResponse(mockHttpServletResponse);
37    }
38  
39    /**
40     * Create the AccessHandler and set its input stream.
41     *
42     * @see org.melati.login.test.AccessHandlerTestAbstract#setAccessHandler()
43     */
44    public void setAccessHandler() {
45      HttpBasicAuthenticationAccessHandler ah = new HttpBasicAuthenticationAccessHandler();
46      it = ah;
47    }
48  
49    /**
50     * Test method for {@link org.melati.login.AccessHandler#establishUser(Melati)}.
51     */
52    public void ignoreTestEstablishUserFromRequestWrongMethod() {
53      ((MockHttpServletRequest)m.getRequest()).setHeader("Authorization", 
54          "basic " + Base64.encodeBase64("_administrator_:FIXME".getBytes()));
55      try { 
56        it.establishUser(m);
57        fail("Should have bombed");
58      } catch (HttpAuthorizationMelatiException e) { 
59        e = null;
60      }
61      ((MockHttpServletRequest)m.getRequest()).setHeader("Authorization", 
62          "Basic " + Base64.encodeBase64("_administrator_/FIXME".getBytes()));
63      try { 
64        it.establishUser(m);
65        fail("Should have bombed");
66      } catch (HttpAuthorizationMelatiException e) { 
67        e = null;
68      }
69      ((MockHttpServletRequest)m.getRequest()).setHeader("Authorization", 
70          "Basic" + Base64.encodeBase64("_administrator_:FIXME".getBytes()));
71      try { 
72        it.establishUser(m);
73        fail("Should have bombed");
74      } catch (HttpAuthorizationMelatiException e) { 
75        e = null;
76      }
77    }
78  }