/*
 * Copyright (C) 2011 Thomas Akehurst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.tomakehurst.wiremock;

import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient;
import org.junit.*;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class BrowserProxyAcceptanceTest {

    @ClassRule
    public static WireMockClassRule target = new WireMockClassRule(wireMockConfig().dynamicPort());

    @Rule
    public WireMockClassRule instanceRule = target;

    private WireMockServer proxy;
    private WireMockTestClient testClient;

    @Before
    public void addAResourceToProxy() {
        testClient = new WireMockTestClient(target.port());

        proxy = new WireMockServer(wireMockConfig()
                .dynamicPort()
                .enableBrowserProxying(true));
        proxy.start();
    }

    @After
    public void stopServer() {
        if (proxy.isRunning()) {
            proxy.stop();
        }
    }

    @Test
    public void canProxyHttp() {
        target.stubFor(get(urlEqualTo("/whatever")).willReturn(aResponse().withBody("Got it")));

        assertThat(testClient.getViaProxy(url("/whatever"), proxy.port()).content(), is("Got it"));
    }

    @Test
    public void passesQueryParameters() {
        target.stubFor(get(urlEqualTo("/search?q=things&limit=10")).willReturn(aResponse().withStatus(200)));

        assertThat(testClient.getViaProxy(url("/search?q=things&limit=10"), proxy.port()).statusCode(), is(200));
    }

    private String url(String pathAndQuery) {
        return "http://localhost:" + target.port() + pathAndQuery;
    }

}

@@ -1179,16 +1179,29 @@
 Config()
+%0A            
 .dynamic
@@ -1202,24 +1202,61 @@
 ynamicPort()
+%0A            .dynamicHttpsPort()%0A    
 );%0A%0A    @Rul
@@ -2019,32 +2019,311 @@
 t it%22));%0A    %7D%0A%0A
+    @Test @Ignore%0A    public void canProxyHttps() throws Exception %7B%0A        target.stubFor(get(urlEqualTo(%22/whatever%22)).willReturn(aResponse().withBody(%22Got it%22)));%0A%0A        assertThat(testClient.getViaProxy(httpsUrl(%22/whatever%22), proxy.port()).content(), is(%22Got it%22));%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2632,24 +2632,24 @@
 AndQuery) %7B%0A
-
         retu
@@ -2710,10 +2710,141 @@
 %0A    %7D%0A%0A
+    private String httpsUrl(String pathAndQuery) %7B%0A        return %22https://localhost:%22 + target.httpsPort() + pathAndQuery;%0A    %7D%0A%0A
 %7D%0A

Add ignored failing test for https forward proxying