package com.mdsol.mauth.proxy;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.mdsol.mauth.MAuthRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

public class ProxyServerTest {

  private static final String BASE_URL = "http://localhost";
  private static final String MY_RESOURCE = "/my/resource";

  @Rule
  public WireMockRule service1 = new WireMockRule(wireMockConfig().dynamicPort());

  @Test
  public void shouldCorrectlyModifyRequestWhenForwardUrlHeaderIsNull() throws IOException {
    service1.stubFor(get(urlEqualTo(MY_RESOURCE))
        .withHeader(MAuthRequest.MAUTH_AUTHENTICATION_HEADER_NAME, containing("MWS "))
        .withHeader(MAuthRequest.MAUTH_TIME_HEADER_NAME, matching(".*"))
        .willReturn(aResponse()
            .withStatus(HttpStatus.SC_OK)
            .withBody("success")));

    ProxyServer proxyServer = getProxyServer();

    final int proxyPort = proxyServer.getPort();

    HttpResponse response = HttpClients.createDefault().execute(new HttpGet(BASE_URL + ":" + proxyPort + MY_RESOURCE));
    assert (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
    proxyServer.stop();
  }

  @Test
  public void shouldOverrideExistingMAuthHeaders() throws IOException {
    final String WRONG_MAUTH_HEADER = "This is a wrong Mauth Header";

    service1.stubFor(get(urlEqualTo(MY_RESOURCE))
        .withHeader(MAuthRequest.MAUTH_AUTHENTICATION_HEADER_NAME, notMatching(WRONG_MAUTH_HEADER))
        .withHeader(MAuthRequest.MAUTH_TIME_HEADER_NAME, notMatching(WRONG_MAUTH_HEADER))
        .willReturn(aResponse()
            .withStatus(HttpStatus.SC_OK)
            .withBody("success")));

    ProxyServer proxyServer = getProxyServer();

    final int proxyPort = proxyServer.getPort();

    final HttpGet request = new HttpGet(BASE_URL + ":" + proxyPort + MY_RESOURCE);
    request.addHeader(MAuthRequest.MAUTH_AUTHENTICATION_HEADER_NAME, WRONG_MAUTH_HEADER);
    request.addHeader(MAuthRequest.MAUTH_TIME_HEADER_NAME, WRONG_MAUTH_HEADER);

    HttpResponse response = HttpClients.createDefault().execute(request);
    assert (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
    proxyServer.stop();
  }

  private ProxyServer getProxyServer() throws IOException {
    ProxyServer proxyServer = null;
    try {
      proxyServer = new ProxyServer(new ProxyConfig(
          0,
          (512 * 1024),
          UUID.randomUUID(),
          "classpath:/fake_private_key"
      ));
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
    proxyServer.serve();
    return proxyServer;
  }
}
@@ -29,119 +29,8 @@
 y;%0A%0A
-import com.github.tomakehurst.wiremock.WireMockServer;%0Aimport com.github.tomakehurst.wiremock.client.WireMock;%0A
 impo
@@ -121,16 +121,49 @@
 equest;%0A
+import org.apache.http.HttpHost;%0A
 import o
@@ -308,27 +308,142 @@
 .client.
-HttpClients
+CloseableHttpClient;%0Aimport org.apache.http.impl.client.HttpClients;%0Aimport org.apache.http.impl.conn.DefaultProxyRoutePlanner
 ;%0Aimport
@@ -1026,34 +1026,25 @@
 uest
-WhenForwardUrlHeaderIsNull
+ToAddMAuthHeaders
 () t
@@ -1441,118 +1441,63 @@
     
-final int proxyPort = proxyServer.getPort();%0A%0A    HttpResponse response = HttpClients.createDefault().execute(
+HttpResponse response = execute(proxyServer.getPort(), 
 new 
@@ -1517,33 +1517,39 @@
 URL + %22:%22 + 
-proxyP
+service1.p
 ort
+()
  + MY_RESOUR
@@ -2213,58 +2213,8 @@
 );%0A%0A
-    final int proxyPort = proxyServer.getPort();%0A%0A
     
@@ -2270,17 +2270,23 @@
 %22 + 
-proxyP
+service1.p
 ort
+()
  + M
@@ -2501,44 +2501,39 @@
 e = 
-HttpClients.createDefault().execute(
+execute(proxyServer.getPort(), 
 requ
@@ -2538,16 +2538,17 @@
 quest);%0A
+%0A
     asse
@@ -2914,24 +2914,51 @@
 %22%0A      ));%0A
+      proxyServer.serve();%0A
     %7D catch 
@@ -3015,24 +3015,31 @@
 ;%0A    %7D%0A    
+return 
 proxyServer.
@@ -3041,40 +3041,411 @@
 rver
-.serve();%0A    return proxyServer
+;%0A  %7D%0A%0A  private HttpResponse execute(int proxyServerPort, HttpGet request) throws IOException %7B%0A    HttpHost proxy = new HttpHost(%22localhost%22, proxyServerPort);%0A    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);%0A    final CloseableHttpClient httpClient = HttpClients.custom()%0A        .setRoutePlanner(routePlanner)%0A        .build();%0A%0A    return httpClient.execute(request)
 ;%0A  

Make tests use Proxy settings for http client