Search in sources :

Example 1 with ConnectionFactory

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

the class HttpRequestTest method customConnectionFactory.

/**
   * Verify custom connection factory
   */
@Test
public void customConnectionFactory() throws Exception {
    handler = new RequestHandler() {

        @Override
        public void handle(Request request, HttpServletResponse response) {
            response.setStatus(HTTP_OK);
        }
    };
    ConnectionFactory factory = new ConnectionFactory() {

        public HttpURLConnection create(URL otherUrl) throws IOException {
            return (HttpURLConnection) new URL(url).openConnection();
        }

        public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
            throw new IOException();
        }
    };
    HttpRequest.setConnectionFactory(factory);
    int code = get("http://not/a/real/url").code();
    assertEquals(200, code);
}
Also used : ConnectionFactory(com.github.kevinsawicki.http.HttpRequest.ConnectionFactory) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ConnectionFactory (com.github.kevinsawicki.http.HttpRequest.ConnectionFactory)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 Proxy (java.net.Proxy)1 URL (java.net.URL)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Request (org.eclipse.jetty.server.Request)1 Test (org.junit.Test)1