Example usage for org.apache.commons.httpclient.methods OptionsMethod getResponseBodyAsString

List of usage examples for org.apache.commons.httpclient.methods OptionsMethod getResponseBodyAsString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods OptionsMethod getResponseBodyAsString.

Prototype

@Override
public String getResponseBodyAsString() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as a String .

Usage

From source file:org.apache.wink.itest.headers.HeadersTest.java

public void testAllowHeaders() throws Exception {
    try {/*  w  ww .  j  a  v  a  2s. com*/
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/headersallow1/allow1", false));
        httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(httpMethod);
            assertEquals(204, result);
            assertNotNull(httpMethod.getResponseHeader("Allow"));
            List<String> allowedMethods = Arrays
                    .asList(httpMethod.getResponseHeader("Allow").getValue().split(", "));
            System.out.println(allowedMethods);
            assertEquals(3, allowedMethods.size());
            assertTrue(allowedMethods.contains("HEAD"));
            assertTrue(allowedMethods.contains("OPTIONS"));
            assertTrue(allowedMethods.contains("GET"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/headersallow2", false));
        httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(httpMethod);
            assertEquals(204, result);
            assertNotNull(httpMethod.getResponseHeader("Allow"));
            List<String> allowedMethods = Arrays
                    .asList(httpMethod.getResponseHeader("Allow").getValue().split(", "));
            System.out.println(allowedMethods);
            assertEquals(6, allowedMethods.size());
            assertTrue(allowedMethods.contains("HEAD"));
            assertTrue(allowedMethods.contains("OPTIONS"));
            assertTrue(allowedMethods.contains("GET"));
            assertTrue(allowedMethods.contains("PUT"));
            assertTrue(allowedMethods.contains("POST"));
            assertTrue(allowedMethods.contains("DELETE"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/headersallow3/sublocator", false));
        httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(httpMethod);
            assertEquals(204, result);
            assertNotNull(httpMethod.getResponseHeader("Allow"));
            List<String> allowedMethods = Arrays
                    .asList(httpMethod.getResponseHeader("Allow").getValue().split(", "));
            System.out.println(allowedMethods);
            assertEquals(3, allowedMethods.size());
            assertTrue(allowedMethods.contains("HEAD"));
            assertTrue(allowedMethods.contains("OPTIONS"));
            assertTrue(allowedMethods.contains("GET"));
            assertEquals(null, httpMethod.getResponseBodyAsString());
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that an OPTIONS request can be sent to resource containing only a
 * GET method.// ww  w  .  j a  v  a  2s .c  om
 */
public void testOPTIONSRequest() {
    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(BASE_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(204, result);
            Enumeration<?> allowedMethods = httpMethod.getAllowedMethods();
            assertNotNull(allowedMethods);
            assertTrue(allowedMethods.hasMoreElements());
            List<String> methods = new ArrayList<String>();
            while (allowedMethods.hasMoreElements()) {
                methods.add((String) allowedMethods.nextElement());
            }
            assertTrue(methods.contains("HEAD"));
            assertTrue(methods.contains("GET"));
            assertTrue(methods.contains("OPTIONS"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that a OPTIONS request can be sent to resource annotated with a
 * custom OPTIONS annotation.// ww  w.  j  a  v a 2  s.  c  om
 */
public void testCustomOPTIONSRequest() {
    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(ALT_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals("", responseBody);
            Header header = httpMethod.getResponseHeader("Allow");
            assertNotNull(header);
            String value = header.getValue();
            assertTrue(value.contains("HEAD"));
            assertTrue(value.contains("OPTIONS"));
            assertTrue(value.contains("GET"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}