Search in sources :

Example 1 with HttpRequestException

use of com.github.kevinsawicki.http.HttpRequest.HttpRequestException in project http-request by kevinsawicki.

the class HttpRequestTest method sendErrorReadStream.

/**
   * Send a stream that throws an exception when read from
   *
   * @throws Exception
   */
@Test
public void sendErrorReadStream() throws Exception {
    handler = new RequestHandler() {

        @Override
        public void handle(Request request, HttpServletResponse response) {
            response.setStatus(HTTP_OK);
            try {
                response.getWriter().print("content");
            } catch (IOException e) {
                fail();
            }
        }
    };
    final IOException readCause = new IOException();
    final IOException closeCause = new IOException();
    InputStream stream = new InputStream() {

        public int read() throws IOException {
            throw readCause;
        }

        public void close() throws IOException {
            throw closeCause;
        }
    };
    try {
        post(url).send(stream);
        fail("Exception not thrown");
    } catch (HttpRequestException e) {
        assertEquals(readCause, e.getCause());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with HttpRequestException

use of com.github.kevinsawicki.http.HttpRequest.HttpRequestException in project http-request by kevinsawicki.

the class HttpRequestTest method sendErrorCloseStream.

/**
   * Send a stream that throws an exception when read from
   *
   * @throws Exception
   */
@Test
public void sendErrorCloseStream() throws Exception {
    handler = new RequestHandler() {

        @Override
        public void handle(Request request, HttpServletResponse response) {
            response.setStatus(HTTP_OK);
            try {
                response.getWriter().print("content");
            } catch (IOException e) {
                fail();
            }
        }
    };
    final IOException closeCause = new IOException();
    InputStream stream = new InputStream() {

        public int read() throws IOException {
            return -1;
        }

        public void close() throws IOException {
            throw closeCause;
        }
    };
    try {
        post(url).ignoreCloseExceptions(false).send(stream);
        fail("Exception not thrown");
    } catch (HttpRequestException e) {
        assertEquals(closeCause, e.getCause());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpRequestException(com.github.kevinsawicki.http.HttpRequest.HttpRequestException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpRequestException (com.github.kevinsawicki.http.HttpRequest.HttpRequestException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Request (org.eclipse.jetty.server.Request)2 Test (org.junit.Test)2