1   /* LICENSE */
2   package net.smartlab.web.auth.handlers;
3   
4   import javax.servlet.http.HttpSession;
5   
6   import junit.framework.TestCase;
7   import net.smartlab.web.auth.handlers.CaptchaRegistrationHandler.Captcha;
8   
9   import com.meterware.httpunit.PostMethodWebRequest;
10  import com.meterware.httpunit.WebRequest;
11  import com.meterware.httpunit.WebResponse;
12  import com.meterware.servletunit.ServletRunner;
13  import com.meterware.servletunit.ServletUnitClient;
14  
15  /**
16   * TODO documentation
17   * 
18   * @author rlogiacco
19   */
20  public class CaptchaRegistrationHandlerServletTest extends TestCase {
21  
22  	private ServletRunner container;
23  
24  
25  	/**
26  	 * @see junit.framework.TestCase#setUp()
27  	 */
28  	protected void setUp() throws Exception {
29  		super.setUp();
30  		container = new ServletRunner();
31  		container.registerServlet("captcha", CaptchaRegistrationHandler.Servlet.class.getName());
32  	}
33  
34  	/**
35  	 * @see junit.framework.TestCase#tearDown()
36  	 */
37  	protected void tearDown() throws Exception {
38  		super.tearDown();
39  	}
40  
41  	/**
42  	 * Test method for
43  	 * {@link net.smartlab.web.auth.handlers.CaptchaRegistrationHandler.Servlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}
44  	 * .
45  	 * @throws Exception 
46  	 */
47  	public void testServiceServletRequestServletResponse() throws Exception {
48  		CaptchaRegistrationHandler handler = CaptchaRegistrationHandlerTest.getHandler();
49  		Captcha captcha = handler.generate(12);
50  		ServletUnitClient client = container.newClient();
51  		HttpSession session = client.getSession(true);
52  		session.setAttribute("net.smartlab.web.auth.captcha", captcha);
53  		WebRequest request = new PostMethodWebRequest("http://something/captcha");
54  		WebResponse response = client.getResponse(request);
55  		assertNotNull("No response received", response);
56  		assertEquals(200, response.getResponseCode());
57  		assertEquals("image/png", response.getContentType());
58  		assertEquals(captcha.getImage().length, response.getContentLength());
59  	}
60  }