Search in sources :

Example 1 with WebConversation

use of com.meterware.httpunit.WebConversation in project javaee7-samples by javaee-samples.

the class MyResourceTest method testPut.

@Test
public void testPut() throws IOException, SAXException {
    WebConversation conv = new WebConversation();
    conv.setAuthentication("file", "u1", "p1");
    byte[] bytes = new byte[8];
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    PutMethodWebRequest putRequest = new PutMethodWebRequest(base + "/webresources/myresource", bais, "text/plain");
    try {
        WebResponse response = conv.getResponse(putRequest);
    } catch (HttpException e) {
        assertNotNull(e);
        assertEquals(403, e.getResponseCode());
        return;
    }
    fail("PUT is not authorized and can still be called");
}
Also used : PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpException(com.meterware.httpunit.HttpException) Test(org.junit.Test)

Example 2 with WebConversation

use of com.meterware.httpunit.WebConversation in project javaee7-samples by javaee-samples.

the class MyResourceTest method testGetWithCorrectCredentials.

@Test
public void testGetWithCorrectCredentials() throws IOException, SAXException {
    WebConversation conv = new WebConversation();
    conv.setAuthentication("file", "u1", "p1");
    GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/webresources/myresource");
    WebResponse response = null;
    try {
        response = conv.getResponse(getRequest);
    } catch (AuthorizationRequiredException e) {
        fail(e.getMessage());
    }
    assertNotNull(response);
    assertTrue(response.getText().contains("get"));
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) AuthorizationRequiredException(com.meterware.httpunit.AuthorizationRequiredException) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 3 with WebConversation

use of com.meterware.httpunit.WebConversation in project geode by apache.

the class TestSessionsTomcat8Base method testSessionPersists1.

/**
   * Check that our session persists. The values we pass in as query params are used to set
   * attributes on the session.
   */
@Test
public void testSessionPersists1() throws Exception {
    String key = "value_testSessionPersists1";
    String value = "Foo";
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    req.setParameter("cmd", QueryCommand.SET.name());
    req.setParameter("param", key);
    req.setParameter("value", value);
    WebResponse response = wc.getResponse(req);
    String sessionId = response.getNewCookieValue("JSESSIONID");
    assertNotNull("No apparent session cookie", sessionId);
    // The request retains the cookie from the prior response...
    req.setParameter("cmd", QueryCommand.GET.name());
    req.setParameter("param", key);
    req.removeParameter("value");
    response = wc.getResponse(req);
    assertEquals(value, response.getText());
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 4 with WebConversation

use of com.meterware.httpunit.WebConversation in project geode by apache.

the class TestSessionsTomcat8Base method testBasicRegion.

/**
   * Test that a session attribute gets set into the region too.
   */
@Test
public void testBasicRegion() throws Exception {
    String key = "value_testBasicRegion";
    String value = "Foo";
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    // Set an attribute
    req.setParameter("cmd", QueryCommand.SET.name());
    req.setParameter("param", key);
    req.setParameter("value", value);
    WebResponse response = wc.getResponse(req);
    String sessionId = response.getNewCookieValue("JSESSIONID");
    assertEquals(value, region.get(sessionId).getAttribute(key));
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 5 with WebConversation

use of com.meterware.httpunit.WebConversation in project geode by apache.

the class TestSessionsTomcat8Base method testCommitSessionValveInvalidSession.

/*
   * Test for issue #38 CommitSessionValve throws exception on invalidated sessions
   */
@Test
public void testCommitSessionValveInvalidSession() throws Exception {
    Callback c = new Callback() {

        @Override
        public void call(HttpServletRequest request, HttpServletResponse response) throws IOException {
            HttpSession session = request.getSession();
            session.invalidate();
            response.getWriter().write("done");
        }
    };
    servlet.getServletContext().setAttribute("callback", c);
    WebConversation wc = new WebConversation();
    WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
    // Execute the callback
    req.setParameter("cmd", QueryCommand.CALLBACK.name());
    req.setParameter("param", "callback");
    WebResponse response = wc.getResponse(req);
    assertEquals("done", response.getText());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Aggregations

WebConversation (com.meterware.httpunit.WebConversation)44 Test (org.junit.Test)44 WebResponse (com.meterware.httpunit.WebResponse)43 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)42 WebRequest (com.meterware.httpunit.WebRequest)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 HttpSession (javax.servlet.http.HttpSession)9 WebForm (com.meterware.httpunit.WebForm)7 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)5 AuthorizationRequiredException (com.meterware.httpunit.AuthorizationRequiredException)3 PrintWriter (java.io.PrintWriter)3 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)3 HttpException (com.meterware.httpunit.HttpException)2 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)2 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)1 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)1 WebLink (com.meterware.httpunit.WebLink)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Cookie (javax.servlet.http.Cookie)1