|
| 1 | +package org.javaee7.servlet.security.deny.uncovered; |
| 2 | + |
| 3 | +import com.meterware.httpunit.AuthorizationRequiredException; |
| 4 | +import com.meterware.httpunit.GetMethodWebRequest; |
| 5 | +import com.meterware.httpunit.HttpException; |
| 6 | +import com.meterware.httpunit.PostMethodWebRequest; |
| 7 | +import com.meterware.httpunit.PutMethodWebRequest; |
| 8 | +import com.meterware.httpunit.WebConversation; |
| 9 | +import com.meterware.httpunit.WebResponse; |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.File; |
| 12 | +import java.net.URL; |
| 13 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 14 | +import org.jboss.arquillian.junit.Arquillian; |
| 15 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 16 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 17 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 18 | +import org.junit.Test; |
| 19 | +import static org.junit.Assert.*; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Arun Gupta |
| 24 | + */ |
| 25 | +@RunWith(Arquillian.class) |
| 26 | +public class SecureServletTest { |
| 27 | + |
| 28 | + private static final String WEBAPP_SRC = "src/main/webapp"; |
| 29 | + |
| 30 | + @ArquillianResource |
| 31 | + private URL base; |
| 32 | + |
| 33 | + @Deployment(testable = false) |
| 34 | + public static WebArchive createDeployment() { |
| 35 | + WebArchive war = ShrinkWrap.create(WebArchive.class). |
| 36 | + addClass(SecureServlet.class). |
| 37 | + addAsWebInfResource((new File(WEBAPP_SRC + "/WEB-INF", "web.xml"))); |
| 38 | + return war; |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testGetMethod() throws Exception { |
| 43 | + WebConversation conv = new WebConversation(); |
| 44 | + conv.setAuthentication("file", "u1", "p1"); |
| 45 | + GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/SecureServlet"); |
| 46 | + WebResponse response = null; |
| 47 | + try { |
| 48 | + response = conv.getResponse(getRequest); |
| 49 | + } catch (AuthorizationRequiredException e) { |
| 50 | + fail(e.getMessage()); |
| 51 | + } |
| 52 | + assertNotNull(response); |
| 53 | + assertTrue(response.getText().contains("<title>Servlet Security - Basic Auth with File-base Realm</title>")); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testPostMethod() throws Exception { |
| 58 | + WebConversation conv = new WebConversation(); |
| 59 | + conv.setAuthentication("file", "u1", "p1"); |
| 60 | + |
| 61 | + PostMethodWebRequest postRequest = new PostMethodWebRequest(base + "/SecureServlet"); |
| 62 | + try { |
| 63 | + conv.getResponse(postRequest); |
| 64 | + } catch (HttpException e) { |
| 65 | + assertEquals(403, e.getResponseCode()); |
| 66 | + return; |
| 67 | + } |
| 68 | + fail("POST method could be called"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testPutMethod() throws Exception { |
| 73 | + WebConversation conv = new WebConversation(); |
| 74 | + conv.setAuthentication("file", "u1", "p1"); |
| 75 | + |
| 76 | + byte[] bytes = new byte[10]; |
| 77 | + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
| 78 | + PutMethodWebRequest putRequest = new PutMethodWebRequest(base + "/SecureServlet", bais, "text/plain"); |
| 79 | + try { |
| 80 | + conv.getResponse(putRequest); |
| 81 | + } catch (HttpException e) { |
| 82 | + assertEquals(403, e.getResponseCode()); |
| 83 | + return; |
| 84 | + } |
| 85 | + fail("PUT method could be called"); |
| 86 | + } |
| 87 | +} |
0 commit comments