/*
 *
 * Copyright 2013 Netflix, Inc.
 *
 * 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.netflix.niws.client.http;

import static org.junit.Assert.assertEquals;

import java.net.URI;

import org.junit.Test;

import com.netflix.client.ClientFactory;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.client.config.IClientConfigKey;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;

public class FollowRedirectTest {
    @Test
    public void testRedirectNotFollowed() throws Exception {
        IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("myclient");
        config.setProperty(CommonClientConfigKey.FollowRedirects, Boolean.FALSE);
        ClientFactory.registerClientFromProperties("myclient", config);
        RestClient client = (RestClient) ClientFactory.getNamedClient("myclient");
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://jigsaw.w3.org/HTTP/300/302.html")).build();
        HttpResponse response = client.executeWithLoadBalancer(request);
        assertEquals(302, response.getStatus());          
    }

    @Test
    public void testRedirectFollowed() throws Exception {
        IClientConfig config = DefaultClientConfigImpl
                .getClientConfigWithDefaultValues("myclient2")
                .set(IClientConfigKey.Keys.FollowRedirects, Boolean.TRUE);
        ClientFactory.registerClientFromProperties("myclient2", config);
        com.netflix.niws.client.http.RestClient client = (com.netflix.niws.client.http.RestClient) ClientFactory.getNamedClient("myclient2");
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://jigsaw.w3.org/HTTP/300/302.html")).build();
        HttpResponse response = client.execute(request);
        assertEquals(200, response.getStatus());      
    }

}

@@ -693,41 +693,211 @@
 ava.
-net.URI;%0A%0Aimport org.junit.Test;%0A
+io.IOException;%0Aimport java.net.URI;%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0Aimport com.google.mockwebserver.MockResponse;%0Aimport com.google.mockwebserver.MockWebServer;
 %0Aimp
@@ -1271,16 +1271,1013 @@
 tTest %7B%0A
+    %0A    private MockWebServer redirectingServer;%0A    private MockWebServer redirectedServer;%0A    %0A    @Before%0A    public void setup() throws IOException %7B%0A        redirectedServer = new MockWebServer();%0A        redirectedServer.enqueue(new MockResponse()%0A            .setResponseCode(200)%0A            .setHeader(%22Content-type%22, %22text/plain%22)%0A            .setBody(%22OK%22));       %0A        redirectingServer = new MockWebServer(); %0A        redirectedServer.play();%0A        redirectingServer.enqueue(new MockResponse()%0A            .setResponseCode(302)%0A            .setHeader(%22Location%22, %22http://localhost:%22 + redirectedServer.getPort()));%0A        redirectingServer.play();%0A    %7D%0A    %0A    @After%0A    public void shutdown() %7B%0A        try %7B%0A            redirectedServer.shutdown();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        try %7B%0A            redirectingServer.shutdown();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A    %0A
     @Tes
@@ -2756,40 +2756,49 @@
 p://
-jigsaw.w3.org/HTTP/300/302.html%22
+localhost:%22 + redirectingServer.getPort()
 )).b
@@ -3499,40 +3499,49 @@
 p://
-jigsaw.w3.org/HTTP/300/302.html%22
+localhost:%22 + redirectingServer.getPort()
 )).b
@@ -3669,8 +3669,9 @@
    %7D%0A%0A%7D%0A
+%0A

Use local mocked HTTP server to test redirecting.