Example usage for org.apache.commons.httpclient.methods PutMethod PutMethod

List of usage examples for org.apache.commons.httpclient.methods PutMethod PutMethod

Introduction

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

Prototype

public PutMethod(String paramString) 

Source Link

Usage

From source file:org.apache.wink.itest.exceptions.ExceptionsWhileTargettingTest.java

/**
 * Tests that a 415 error is thrown when request entity data sent is not
 * acceptable by the resource.// www  . j  ava 2 s. c  o  m
 * 
 * @throws Exception
 */
public void test415WhenResourceMethodDoesNotAcceptRequestEntity() throws Exception {
    PutMethod putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");
    try {
        putMethod.setRequestEntity(new StringRequestEntity("some content", "text/plain", "UTF-8"));
        client.executeMethod(putMethod);
        assertEquals(200, putMethod.getStatusCode());
        assertEquals("some content", putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }

    putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");
    try {
        putMethod.setRequestEntity(new StringRequestEntity("some content", "customplain/something", "UTF-8"));
        client.executeMethod(putMethod);
        assertEquals(415, putMethod.getStatusCode());
        ServerContainerAssertions.assertExceptionBodyFromServer(415, putMethod.getResponseBodyAsString());

    } finally {
        putMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.exceptions.ExceptionsWhileTargettingTest.java

/**
 * Tests that a 406 error is produced if server side cannot produce any
 * acceptable content type.//w ww. j a  va2s  .  c om
 * 
 * @throws Exception
 */
public void test406WhenResourceMethodDoesNotProduceResponseEntityType() throws Exception {
    PutMethod putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");

    try {
        putMethod.addRequestHeader("Accept", "text/plain");
        putMethod.setRequestEntity(new StringRequestEntity("some content", "text/plain", "UTF-8"));
        client.executeMethod(putMethod);

        assertEquals(200, putMethod.getStatusCode());
        assertEquals("some content", putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }

    putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");
    try {
        putMethod.addRequestHeader("Accept", "text/customplain");
        putMethod.setRequestEntity(new StringRequestEntity("some content", "text/plain", "UTF-8"));
        client.executeMethod(putMethod);

        assertEquals(406, putMethod.getStatusCode());
        ServerContainerAssertions.assertExceptionBodyFromServer(406, putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.nofindmethods.DoNotUseMethodNamesForHTTPVerbsTest.java

/**
 * Negative tests that method names that begin with HTTP verbs are not
 * invoked on a root resource./*from  w ww.  jav a  2  s. co m*/
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testMethodsNotValid() throws HttpException, IOException {
    HttpMethod method = new PostMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/someresource");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new GetMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/someresource");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new PutMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/someresource");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new DeleteMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/someresource");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new GetMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/counter/root");
    try {
        client.executeMethod(method);
        assertEquals(200, method.getStatusCode());
        assertEquals("0", method.getResponseBodyAsString());
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.wink.itest.nofindmethods.DoNotUseMethodNamesForHTTPVerbsTest.java

/**
 * Negative tests that method names that begin with HTTP verbs are not
 * invoked on a sublocator method./*from w ww . j ava  2 s  .  c  o m*/
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testSublocatorMethodsNotValid() throws HttpException, IOException {
    HttpMethod method = new PostMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/sublocatorresource/sub");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new GetMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/sublocatorresource/sub");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new PutMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/sublocatorresource/sub");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new DeleteMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/sublocatorresource/sub");
    try {
        client.executeMethod(method);
        assertEquals(405, method.getStatusCode());
    } finally {
        method.releaseConnection();
    }

    method = new GetMethod(getBaseURI() + "/nousemethodnamesforhttpverbs/counter/sublocator");
    try {
        client.executeMethod(method);
        assertEquals(200, method.getStatusCode());
        assertEquals("0", method.getResponseBodyAsString());
    } finally {
        method.releaseConnection();
    }
}

From source file:org.apache.wink.itest.request.RequestMethodsTest.java

private void checkIfModifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
        throws IOException, HttpException {
    HttpClient client = new HttpClient();

    /*/* w  w  w.j a v a  2  s . com*/
     * get the time zone for the server
     */
    GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/timezone");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/date");
    Date d2 = new Date(System.currentTimeMillis() - 120000);
    Date d = new Date(System.currentTimeMillis() - 60000);
    DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); // DateFormat.getDateTimeInstance();
    String date = dateFormat.format(d);
    putMethod.setRequestEntity(new StringRequestEntity(date, "text/string", "UTF-8"));
    try {
        /*
         * sets a last modified date
         */
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Modified-Since", formatter.format(d));
    try {
        /*
         * verifies that if the exact date is sent in and used in
         * If-Modified-Since header, then the server will be ok and that it
         * will return 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    try {
        /*
         * verifies that if no If-Modified-Since header is sent, then the
         * server will be ok and the Request instance won't build a
         * response.
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), getMethod.getResponseBodyAsString());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified").getValue());
        rfc1123Format.setTimeZone(TimeZone.getDefault());
    } finally {
        getMethod.releaseConnection();
    }

    String lastModified = getMethod.getResponseHeader("Last-Modified").getValue();
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Modified-Since", lastModified);
    try {
        /*
         * verifies that using Last-Modified response header sent by server
         * as If-Modified-Since request header, then the server will return
         * a 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Modified-Since", formatter.format(d2));
    try {
        /*
         * verifies that using a If-Modified-Since earlier than the
         * Last-Modified response header sent by server then the server will
         * return a 200 with entity
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), getMethod.getResponseBodyAsString());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified").getValue());
        rfc1123Format.setTimeZone(TimeZone.getDefault());
    } finally {
        getMethod.releaseConnection();
    }

    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Modified-Since", formatter.format(new Date()));
    try {
        /*
         * verifies that using a If-Modified-Since later than the
         * Last-Modified response header sent by server, then the server
         * will return a 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.request.RequestMethodsTest.java

private void checkIfUnmodifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
        throws IOException, HttpException {
    HttpClient client = new HttpClient();

    /*/*w  w  w  .  j a v a  2 s. c  o  m*/
     * get the time zone for the server
     */
    GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/timezone");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/date");
    Date d2 = new Date(System.currentTimeMillis() - 120000);
    Date d = new Date(System.currentTimeMillis() - 60000);
    DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); // DateFormat.getDateTimeInstance();
    String date = dateFormat.format(d);
    putMethod.setRequestEntity(new StringRequestEntity(date, "text/string", "UTF-8"));
    try {
        /*
         * sets a last modified date
         */
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Unmodified-Since", formatter.format(d));
    try {
        /*
         * verifies that if the exact date is sent in and used in
         * If-Unmodified-Since header, then the server will be ok and that
         * it will return 200
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), getMethod.getResponseBodyAsString());
        rfc1123Format.setTimeZone(TimeZone.getDefault());

        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified").getValue());
        rfc1123Format.setTimeZone(TimeZone.getDefault());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    try {
        /*
         * verifies that if no If-Unmodified-Since header is sent, then the
         * server will be ok and the Request instance won't build a
         * response.
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), getMethod.getResponseBodyAsString());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified").getValue());
        rfc1123Format.setTimeZone(TimeZone.getDefault());
    } finally {
        getMethod.releaseConnection();
    }

    String lastModified = getMethod.getResponseHeader("Last-Modified").getValue();

    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Unmodified-Since", lastModified);
    try {
        /*
         * verifies that using Last-Modified response header sent by server
         * as If-Unmodified-Since request header, then the server will
         * return the entity
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), getMethod.getResponseBodyAsString());
        rfc1123Format.setTimeZone(TimeZone.getDefault());

        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified").getValue());
        rfc1123Format.setTimeZone(TimeZone.getDefault());
    } finally {
        getMethod.releaseConnection();
    }

    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Unmodified-Since", formatter.format(d2));
    try {
        /*
         * verifies that using a If-Unmodified-Since earlier than the
         * Last-Modified response header sent by server then the server will
         * return a 412
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    getMethod = new GetMethod(getBaseURI() + "/context/request/date");
    getMethod.setRequestHeader("If-Unmodified-Since", formatter.format(new Date()));
    try {
        /*
         * verifies that using a If-Unmodified-Since later than the
         * Last-Modified response header sent by server, then the server
         * will return 200 and the entity
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals("the date: " + rfc1123Format.format(d), getMethod.getResponseBodyAsString());
        rfc1123Format.setTimeZone(TimeZone.getDefault());

        rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
        assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified").getValue());
        rfc1123Format.setTimeZone(TimeZone.getDefault());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.request.RequestMethodsTest.java

/**
 * Tests the/*from ww w.  j a v a  2  s . c om*/
 * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
 * uses the <code>If-Match</code> header.
 * 
 * @throws HttpException
 * @throws IOException
 */
private void checkETagIfMatch(String etag, boolean isEntityTagWeak) throws HttpException, IOException {
    HttpClient client = new HttpClient();

    final String justTheTag = etag;
    final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
    String isWeak = isEntityTagWeak ? "true" : "false";

    PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/etag");
    putMethod.setRequestEntity(new StringRequestEntity(setETag, "text/string", "UTF-8"));
    try {
        /*
         * sets an entity tag
         */
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag);
    try {
        /*
         * verifies that if the exact etag is sent in, then the request is
         * allowed to proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    try {
        /*
         * verifies that a request without an If-Match header will still
         * proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag.substring(1, setETag.length() - 1));
    try {
        /*
         * verifies that an unquoted entity tag is not a valid entity tag
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag.substring(0, setETag.length() - 1));
    try {
        /*
         * verifies that a misquoted entity tag is not a valid entity tag
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", setETag.substring(1, setETag.length()));
    try {
        /*
         * verifies that a misquoted entity tag is not a valid entity tag
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", "\"someothervalue\"");
    try {
        /*
         * verifies that if an etag is sent that does not match the server
         * etag, that a 412 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, that a 412 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, that a 412 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(412, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-Match", "\"austin\", " + setETag + " , \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do match the server
         * etag, that a 200 and entity body is returned
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.request.RequestMethodsTest.java

/**
 * Tests the/*from   w  w w .  j a v a  2 s.co  m*/
 * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
 * uses the <code>If-None-Match</code> header.
 * 
 * @throws HttpException
 * @throws IOException
 */
private void checkETagIfNoneMatch(String etag, boolean isEntityTagWeak) throws HttpException, IOException {
    HttpClient client = new HttpClient();
    final String justTheTag = etag;
    final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
    String isWeak = isEntityTagWeak ? "true" : "false";

    PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/etag");
    putMethod.setRequestEntity(new StringRequestEntity(setETag, "text/string", "UTF-8"));
    try {
        /*
         * sets an entity tag
         */
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag);
    try {
        /*
         * verifies that if the exact etag is sent in, then the response is
         * a 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", "\"*\"");
    try {
        /*
         * verifies that if a "*" etag is sent in, then the response returns
         * a 304
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        getMethod.releaseConnection();
    }

    PostMethod postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.setRequestHeader("If-None-Match", setETag);
    try {
        /*
         * verifies that if a matching etag is sent in, then the response
         * returns a 412
         */
        client.executeMethod(postMethod);
        assertEquals(412, postMethod.getStatusCode());
        assertEquals(setETag, postMethod.getResponseHeader("ETag").getValue());
    } finally {
        postMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.setRequestHeader("If-None-Match", "\"*\"");
    try {
        /*
         * verifies that if a "*" etag is sent in, then the response returns
         * a 412
         */
        client.executeMethod(postMethod);
        assertEquals(412, postMethod.getStatusCode());
        assertEquals(setETag, postMethod.getResponseHeader("ETag").getValue());
    } finally {
        postMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    try {
        /*
         * verifies that a request without an If-None-Match header will
         * still proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    try {
        /*
         * verifies that a request without an If-None-Match header will
         * still proceed
         */
        client.executeMethod(postMethod);
        assertEquals(200, postMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, postMethod.getResponseBodyAsString());
    } finally {
        postMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag.substring(1, setETag.length() - 1));
    try {
        /*
         * verifies that an unquoted entity tag is invalid
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag.substring(0, setETag.length() - 1));
    try {
        /*
         * verifies that a misquoted entity tag is invalid
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", setETag.substring(1, setETag.length()));
    try {
        /*
         * verifies that a misquoted entity tag is invalid
         */
        client.executeMethod(getMethod);
        assertEquals(400, getMethod.getStatusCode());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", "\"someothervalue\"");
    try {
        /*
         * verifies that if an etag is sent that does not match the server
         * etag, that request is allowed to proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.setRequestHeader("If-None-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, that the request is allowed to proceed
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-None-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do not match the
         * server etag, then a 200 and the request entity is returned
         */
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
    } finally {
        getMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.addRequestHeader("If-None-Match", "\"austin\", \"powers\"");
    try {
        /*
         * verifies that a request without an If-None-Match header will
         * still proceed
         */
        client.executeMethod(postMethod);
        assertEquals(200, postMethod.getStatusCode());
        assertEquals("the etag: " + justTheTag + isWeak, postMethod.getResponseBodyAsString());
    } finally {
        postMethod.releaseConnection();
    }

    getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
    getMethod.addRequestHeader("If-None-Match", "\"austin\", " + setETag + " , \"powers\"");
    try {
        /*
         * verifies that if multiple etags are sent that do match the server
         * etag, that a 304 is returned
         */
        client.executeMethod(getMethod);
        assertEquals(304, getMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        getMethod.releaseConnection();
    }

    postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
    postMethod.addRequestHeader("If-None-Match", "\"austin\", " + setETag + " , \"powers\"");
    try {
        /*
         * verifies that a request with an If-None-Match header will fail
         */
        client.executeMethod(postMethod);
        assertEquals(412, postMethod.getStatusCode());
        assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java

/**
 * Tests putting and then getting a byte array.
 * /*from   ww  w.j a v a2  s  . com*/
 * @throws HttpException
 * @throws IOException
 */
public void testPutByteArray() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/bytesarray");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/bytesarray");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null
                : getMethod.getResponseHeader("Content-Type").getValue();
        assertNotNull(contentType, contentType);
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.standard.JAXRSBytesArrayTest.java

/**
 * Tests receiving an empty byte array./*from   ww  w.  j a v a2 s. c  o m*/
 * 
 * @throws HttpException
 * @throws IOException
 */
public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/bytesarray");
    byte[] barr = new byte[1000];
    Random r = new Random();
    r.nextBytes(barr);
    putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type"));
    try {
        client.executeMethod(putMethod);
        assertEquals(204, putMethod.getStatusCode());
    } finally {
        putMethod.releaseConnection();
    }

    GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/bytesarray");
    getMethod.addRequestHeader("Accept", "mytype/subtype");
    try {
        client.executeMethod(getMethod);
        assertEquals(200, getMethod.getStatusCode());
        InputStream is = getMethod.getResponseBodyAsStream();

        byte[] receivedBArr = new byte[1000];
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(receivedBArr);

        int checkEOF = dis.read();
        assertEquals(-1, checkEOF);
        for (int c = 0; c < barr.length; ++c) {
            assertEquals(barr[c], receivedBArr[c]);
        }
        assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue());
        assertEquals(barr.length,
                Integer.valueOf(getMethod.getResponseHeader("Content-Length").getValue()).intValue());
    } finally {
        getMethod.releaseConnection();
    }
}