// Path: hub-extensions/extension-proxy/src/main/java/io/sterodium/extensions/hub/proxy/client/HttpClientProvider.java
// public class HttpClientProvider {
// 
//     public CloseableHttpClient provide() {
//         return HttpClients.createDefault();
//     }
// }
// 
// Path: hub-extensions/extension-proxy/src/main/java/io/sterodium/extensions/hub/proxy/client/RequestForwardingClient.java
// public class RequestForwardingClient {
// 
//     private static final Logger LOGGER = Logger.getLogger(RequestForwardingClient.class.getName());
// 
//     private static final String NODE_HOST = "http://%s:%d/extra";
// 
//     private final HttpClientProvider httpClientProvider;
//     private final String endpoint;
// 
//     public RequestForwardingClient(String host, int port) {
//         this(String.format(NODE_HOST, host, port), new HttpClientProvider());
//     }
// 
//     public RequestForwardingClient(String endpoint, HttpClientProvider httpClientProvider) {
//         this.httpClientProvider = httpClientProvider;
//         this.endpoint = endpoint;
//     }
// 
//     public void forwardRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
//         try (
//                 CloseableHttpClient httpClient = httpClientProvider.provide()
//         ) {
//             HttpRequestBase httpRequest = createHttpRequest(servletRequest);
// 
//             CloseableHttpResponse extensionResponse = httpClient.execute(httpRequest);
//             HttpResponseConverter.copy(extensionResponse, servletResponse);
//         }
//     }
// 
//     private HttpRequestBase createHttpRequest(HttpServletRequest request) throws IOException {
//         String method = request.getMethod();
//         LOGGER.info("Creating " + method + " request to forward");
//         HttpRequestBase httpRequestBase =   HttpPost.METHOD_NAME.equals(method)     ?    createPostRequest(request) :
//                                             HttpGet.METHOD_NAME.equals(method)      ?    new HttpGet() :
//                                             HttpPut.METHOD_NAME.equals(method)      ?    new HttpPut() :
//                                             HttpDelete.METHOD_NAME.equals(method)   ?    new HttpDelete() : null;
// 
//         if (httpRequestBase == null) {
//             throw new UnsupportedHttpMethodException(method);
//         }
//         URI uri = URI.create(endpoint + SeleniumSessions.trimSessionPath(request.getPathInfo()));
//         LOGGER.info("Trimming session id from path, new path: " + uri.toString());
//         httpRequestBase.setURI(uri);
// 
//         return httpRequestBase;
//     }
// 
//     private HttpRequestBase createPostRequest(HttpServletRequest request) throws IOException {
//         HttpPost httpPost = new HttpPost();
//         InputStreamEntity entity = new InputStreamEntity(request.getInputStream(),
//                 request.getContentLength(),
//                 // some requests contain ContentType;Encoding and fails in validation.
//                 // So striping Encoding and retaining only ContentType
//                 ContentType.create((request.getContentType().split(";")[0])));
//         httpPost.setEntity(entity);
// 
//         return httpPost;
//     }
// }
// 
// Path: hub-extensions/extension-proxy/src/main/java/io/sterodium/extensions/hub/proxy/client/RequestForwardingClientProvider.java
// public class RequestForwardingClientProvider {
//     public RequestForwardingClient provide(String host, int port) {
//         return new RequestForwardingClient(host, port);
//     }
// }


import io.sterodium.extensions.hub.proxy.client.HttpClientProvider;
import io.sterodium.extensions.hub.proxy.client.RequestForwardingClient;
import io.sterodium.extensions.hub.proxy.client.RequestForwardingClientProvider;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.TestSession;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
package io.sterodium.extensions.hub.proxy;





/**
 * @author Alexey Nikolaenko alexey@tcherezov.com
 *         Date: 21/09/2015
 */
@RunWith(MockitoJUnitRunner.class)
public class HubRequestsProxyingServletPathsTest {

    @Mock
    HttpServletRequest req;
    @Mock
    HttpServletResponse resp;
    @Mock
    GridRegistry registry;

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    TestSession activeSession1;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    TestSession activeSession2;

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    HttpClientProvider httpClientProvider;
    @Mock
    RequestForwardingClientProvider requestForwardingClientProvider;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
// Path: hub-extensions/extension-proxy/src/main/java/io/sterodium/extensions/hub/proxy/client/HttpClientProvider.java
// public class HttpClientProvider {
// 
//     public CloseableHttpClient provide() {
//         return HttpClients.createDefault();
//     }
// }
// 
// Path: hub-extensions/extension-proxy/src/main/java/io/sterodium/extensions/hub/proxy/client/RequestForwardingClient.java
// public class RequestForwardingClient {
// 
//     private static final Logger LOGGER = Logger.getLogger(RequestForwardingClient.class.getName());
// 
//     private static final String NODE_HOST = "http://%s:%d/extra";
// 
//     private final HttpClientProvider httpClientProvider;
//     private final String endpoint;
// 
//     public RequestForwardingClient(String host, int port) {
//         this(String.format(NODE_HOST, host, port), new HttpClientProvider());
//     }
// 
//     public RequestForwardingClient(String endpoint, HttpClientProvider httpClientProvider) {
//         this.httpClientProvider = httpClientProvider;
//         this.endpoint = endpoint;
//     }
// 
//     public void forwardRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException {
//         try (
//                 CloseableHttpClient httpClient = httpClientProvider.provide()
//         ) {
//             HttpRequestBase httpRequest = createHttpRequest(servletRequest);
// 
//             CloseableHttpResponse extensionResponse = httpClient.execute(httpRequest);
//             HttpResponseConverter.copy(extensionResponse, servletResponse);
//         }
//     }
// 
//     private HttpRequestBase createHttpRequest(HttpServletRequest request) throws IOException {
//         String method = request.getMethod();
//         LOGGER.info("Creating " + method + " request to forward");
//         HttpRequestBase httpRequestBase =   HttpPost.METHOD_NAME.equals(method)     ?    createPostRequest(request) :
//                                             HttpGet.METHOD_NAME.equals(method)      ?    new HttpGet() :
//                                             HttpPut.METHOD_NAME.equals(method)      ?    new HttpPut() :
//                                             HttpDelete.METHOD_NAME.equals(method)   ?    new HttpDelete() : null;
// 
//         if (httpRequestBase == null) {
//             throw new UnsupportedHttpMethodException(method);
//         }
//         URI uri = URI.create(endpoint + SeleniumSessions.trimSessionPath(request.getPathInfo()));
//         LOGGER.info("Trimming session id from path, new path: " + uri.toString());
//         httpRequestBase.setURI(uri);
// 
//         return httpRequestBase;
//     }
// 
//     private HttpRequestBase createPostRequest(HttpServletRequest request) throws IOException {
//         HttpPost httpPost = new HttpPost();
//         InputStreamEntity entity = new InputStreamEntity(request.getInputStream(),
//                 request.getContentLength(),
//                 // some requests contain ContentType;Encoding and fails in validation.
//                 // So striping Encoding and retaining only ContentType
//                 ContentType.create((request.getContentType().split(";")[0])));
//         httpPost.setEntity(entity);
// 
//         return httpPost;
//     }
// }
// 
// Path: hub-extensions/extension-proxy/src/main/java/io/sterodium/extensions/hub/proxy/client/RequestForwardingClientProvider.java
// public class RequestForwardingClientProvider {
//     public RequestForwardingClient provide(String host, int port) {
//         return new RequestForwardingClient(host, port);
//     }
// }


// Path: hub-extensions/extension-proxy/src/test/java/io/sterodium/extensions/hub/proxy/HubRequestsProxyingServletPathsTest.java
import io.sterodium.extensions.hub.proxy.client.HttpClientProvider;
import io.sterodium.extensions.hub.proxy.client.RequestForwardingClient;
import io.sterodium.extensions.hub.proxy.client.RequestForwardingClientProvider;
import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.openqa.grid.internal.GridRegistry;
import org.openqa.grid.internal.TestSession;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

package io.sterodium.extensions.hub.proxy;





/**
 * @author Alexey Nikolaenko alexey@tcherezov.com
 *         Date: 21/09/2015
 */
@RunWith(MockitoJUnitRunner.class)
public class HubRequestsProxyingServletPathsTest {

    @Mock
    HttpServletRequest req;
    @Mock
    HttpServletResponse resp;
    @Mock
    GridRegistry registry;

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    TestSession activeSession1;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    TestSession activeSession2;

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    HttpClientProvider httpClientProvider;
    @Mock
    RequestForwardingClientProvider requestForwardingClientProvider;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    CloseableHttpClient closeableHttpClient;