Search in sources :

Example 46 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project camel by apache.

the class HttpClientRouteTest method testHttpRestricMethod.

@Test
public void testHttpRestricMethod() throws Exception {
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/testHttpMethodRestrict", new ByteArrayInputStream(POST_DATA.getBytes()), "text/xml; charset=UTF-8");
    ServletUnitClient client = newClient();
    WebResponse response = client.getResponse(req);
    assertEquals("The response message is wrong ", "OK", response.getResponseMessage());
    assertEquals("The response body is wrong", POST_DATA, response.getText());
    // Send other web method request
    req = new GetMethodWebRequest(CONTEXT_URL + "/services/testHttpMethodRestrict");
    try {
        response = client.getResponse(req);
        fail("Expect the exception here");
    } catch (Exception ex) {
        HttpException httpException = (HttpException) ex;
        assertEquals("Get a wrong response code", 405, httpException.getResponseCode());
    }
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) HttpException(com.meterware.httpunit.HttpException) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) HttpException(com.meterware.httpunit.HttpException) Test(org.junit.Test)

Example 47 with GetMethodWebRequest

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

the class ScopedBeanTest method checkRenderedPage.

@Test
public void checkRenderedPage() throws Exception {
    WebConversation conv = new WebConversation();
    GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/faces/index.xhtml");
    String responseText = conv.getResponse(getRequest).getText();
    assert (responseText.contains("Hello there!"));
}
Also used : WebConversation(com.meterware.httpunit.WebConversation) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Test(org.junit.Test)

Example 48 with GetMethodWebRequest

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

the class MyResourceTest method testGetSubResourceWithCorrectCredentials.

@Test
public void testGetSubResourceWithCorrectCredentials() throws IOException, SAXException {
    WebConversation conv = new WebConversation();
    conv.setAuthentication("file", "u1", "p1");
    GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/webresources/myresource/1");
    WebResponse response = null;
    try {
        response = conv.getResponse(getRequest);
    } catch (AuthorizationRequiredException e) {
        fail(e.getMessage());
    }
    assertNotNull(response);
    assertTrue(response.getText().contains("get1"));
}
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 49 with GetMethodWebRequest

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

the class MyResourceTest method testGetWithIncorrectCredentials.

@Test
public void testGetWithIncorrectCredentials() throws IOException, SAXException {
    WebConversation conv = new WebConversation();
    conv.setAuthentication("file", "random", "random");
    GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/webresources/myresource");
    try {
        WebResponse response = conv.getResponse(getRequest);
    } catch (AuthorizationRequiredException e) {
        assertNotNull(e);
        return;
    }
    fail("GET can be called with incorrect credentials");
}
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 50 with GetMethodWebRequest

use of com.meterware.httpunit.GetMethodWebRequest in project v7files by thiloplanz.

the class BucketsServletTest method testFormPostGET.

public void testFormPostGET() throws IOException, SAXException {
    BasicBSONObject bucket = prepareBucket("1", "FormPost", null, null);
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
    ServletUnitClient sc = sr.newClient();
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        try {
            sc.getResponse(request);
            fail("bucket not found => 404");
        } catch (HttpNotFoundException e) {
            assertEquals(String.format("Bucket '1' does not have a file matching digest '%s'", sha.getDigest()), e.getResponseMessage());
        }
    }
    bucket.append("FormPost", new BasicBSONObject("data", new BasicBSONObject("files", new BasicBSONObject("file", new BasicBSONObject("filename", "a.txt").append("sha", sha.getSHA())))));
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        WebResponse response = sc.getResponse(request);
        assertEquals("test", response.getText());
        assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
        assertEquals(4, response.getContentLength());
        assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
    }
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        request.setParameter("filename", "x.txt");
        WebResponse response = sc.getResponse(request);
        assertEquals("test", response.getText());
        assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
        assertEquals(4, response.getContentLength());
        assertEquals("attachment; filename=\"x.txt\"", response.getHeaderField("Content-Disposition"));
    }
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        request.setHeaderField("If-None-Match", sha.getDigest());
        WebResponse response = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest)

Aggregations

GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)76 WebResponse (com.meterware.httpunit.WebResponse)73 WebRequest (com.meterware.httpunit.WebRequest)72 Test (org.junit.Test)55 WebConversation (com.meterware.httpunit.WebConversation)43 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)16 TextBlock (com.meterware.httpunit.TextBlock)14 URL (java.net.URL)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 HttpSession (javax.servlet.http.HttpSession)9 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)8 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)8 WebForm (com.meterware.httpunit.WebForm)7 OperateOnDeployment (org.jboss.arquillian.container.test.api.OperateOnDeployment)5 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)4 WebLink (com.meterware.httpunit.WebLink)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 AuthorizationRequiredException (com.meterware.httpunit.AuthorizationRequiredException)3 WebTable (com.meterware.httpunit.WebTable)3