/*
 * Copyright 2013 Google Inc. All Rights Reserved.
 * 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.google.appengine.tck.urlfetch;

import java.net.URL;

import com.google.appengine.api.urlfetch.FetchOptions;
import com.google.appengine.api.urlfetch.HTTPResponse;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
 */
@RunWith(Arquillian.class)
public class FetchOptionsBuilderTest extends FetchOptionsTestBase {

    @Test
    public void testWithDefaults() throws Exception {
        FetchOptions options = FetchOptions.Builder.withDefaults();
        testOptions(options);
    }

    @Test
    public void testFollowRedirects() throws Exception {
        final URL redirect = getUrl("redirect");
        FetchOptions options = FetchOptions.Builder.followRedirects();
        testOptions(redirect, options, new ResponseHandler() {
            public void handle(HTTPResponse response) throws Exception {
                URL finalURL = response.getFinalUrl();
                Assert.assertEquals(getUrl(""), finalURL);
            }
        });
    }

    @Test
    public void testDoNotFollowRedirects() throws Exception {
        final URL redirect = getUrl("redirect");
        FetchOptions options = FetchOptions.Builder.doNotFollowRedirects();
        testOptions(redirect, options, new ResponseHandler() {
            public void handle(HTTPResponse response) throws Exception {
                Assert.assertEquals(302, response.getResponseCode());
            }
        });
    }

    @Test
    public void testAllowTruncate() throws Exception {
        FetchOptions options = FetchOptions.Builder.allowTruncate();
        testOptions(options);
    }

    @Test
    public void testDisallowTruncate() throws Exception {
        FetchOptions options = FetchOptions.Builder.disallowTruncate();
        testOptions(options);
    }

    @Test
    public void testValidateCertificate() throws Exception {
        FetchOptions options = FetchOptions.Builder.validateCertificate();
        testOptions(options);
    }

    @Test
    public void testDoNotValidateCertificate() throws Exception {
        FetchOptions options = FetchOptions.Builder.doNotValidateCertificate();
        testOptions(options);
    }

    @Test
    public void testWithDeadline() throws Exception {
        FetchOptions options = FetchOptions.Builder.withDeadline(10 * 1000.0);
        testOptions(options);
    }

}

@@ -649,16 +649,68 @@
 fetch;%0A%0A
+import static junit.framework.Assert.assertEquals;%0A%0A
 import j
@@ -827,17 +827,248 @@
 HTTP
-Response;
+Method;%0Aimport com.google.appengine.api.urlfetch.HTTPRequest;%0Aimport com.google.appengine.api.urlfetch.HTTPResponse;%0Aimport com.google.appengine.api.urlfetch.URLFetchService;%0Aimport com.google.appengine.api.urlfetch.URLFetchServiceFactory;%0A
 %0Aimp
@@ -1993,32 +1993,698 @@
      %7D);%0A    %7D%0A%0A
+    @Test%0A    public void testFollowRedirectsExternal() throws Exception %7B%0A        final URL redirectUrl = new URL(%22http://google.com/%22);%0A        final String expectedDestinationURL = %22http://www.google.com/%22;%0A%0A        FetchOptions options = FetchOptions.Builder.followRedirects();%0A%0A        HTTPRequest request = new HTTPRequest(redirectUrl, HTTPMethod.GET, options);%0A        URLFetchService service = URLFetchServiceFactory.getURLFetchService();%0A        HTTPResponse response = service.fetch(request);%0A        String destinationUrl = response.getFinalUrl().toString();%0A        assertEquals(%22Did not get redirected.%22, expectedDestinationURL, destinationUrl);%0A    %7D%0A%0A
     @Test%0A    pu

Add redirect test that uses an external server.