mirror of https://github.com/apache/jclouds.git
Issue 55 enhanced integration tests to support redirect testing
git-svn-id: http://jclouds.googlecode.com/svn/trunk@1448 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
00b3ccdc7c
commit
3f7833c2c3
|
@ -26,48 +26,60 @@ package org.jclouds.http.commands;
|
||||||
import org.jclouds.http.commands.callables.xml.ParseSax;
|
import org.jclouds.http.commands.callables.xml.ParseSax;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.assistedinject.Assisted;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* temporary factory until guice can do multi-type assisted inject
|
* temporary factory until guice can do multi-type assisted inject
|
||||||
|
*
|
||||||
* @see <a href="http://code.google.com/p/google-guice/issues/detail?id=346" />
|
* @see <a href="http://code.google.com/p/google-guice/issues/detail?id=346" />
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
public class CommandFactory {
|
public class CommandFactory {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ParseSaxFactory parseSaxFactory;
|
private ParseSaxFactory parseSaxFactory;
|
||||||
|
|
||||||
public static interface ParseSaxFactory {
|
public static interface ParseSaxFactory {
|
||||||
ParseSax<?> create(ParseSax.HandlerWithResult<?> handler);
|
ParseSax<?> create(ParseSax.HandlerWithResult<?> handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public GetAndParseSax<?> createGetAndParseSax(String uri,
|
public GetAndParseSax<?> createGetAndParseSax(String uri, ParseSax.HandlerWithResult<?> handler) {
|
||||||
ParseSax.HandlerWithResult<?> handler) {
|
return new GetAndParseSax(uri, parseSaxFactory.create(handler));
|
||||||
return new GetAndParseSax(uri, parseSaxFactory.create(handler));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private GetStringFactory getStringFactory;
|
private GetStringFactory getStringFactory;
|
||||||
|
|
||||||
public static interface GetStringFactory {
|
public static interface GetStringFactory {
|
||||||
GetString create(String uri);
|
GetString create(String uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GetString createGetString(String uri) {
|
public GetString createGetString(String uri) {
|
||||||
return getStringFactory.create(uri);
|
return getStringFactory.create(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private HeadFactory headFactory;
|
private PutFactory putFactory;
|
||||||
|
|
||||||
public static interface HeadFactory {
|
public static interface PutFactory {
|
||||||
Head create(String uri);
|
Put create(@Assisted("uri") String uri, @Assisted("payload") String payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Head createHead(String uri) {
|
public Put createPut(String uri, String payload) {
|
||||||
return headFactory.create(uri);
|
return putFactory.create(uri, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private HeadFactory headFactory;
|
||||||
|
|
||||||
|
public static interface HeadFactory {
|
||||||
|
Head create(String uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Head createHead(String uri) {
|
||||||
|
return headFactory.create(uri);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you 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 org.jclouds.http.commands;
|
||||||
|
|
||||||
|
import com.google.inject.Inject;
|
||||||
|
import com.google.inject.assistedinject.Assisted;
|
||||||
|
import org.jclouds.http.HttpFutureCommand;
|
||||||
|
import org.jclouds.http.commands.callables.ReturnTrueIf2xx;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PUT a string and respond with true if successful.
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public class Put extends HttpFutureCommand<Boolean> {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public Put(ReturnTrueIf2xx callable, @Assisted("uri") String uri,
|
||||||
|
@Assisted("payload") String payload) {
|
||||||
|
super("PUT", uri, callable);
|
||||||
|
this.getRequest().setPayload(payload);
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,6 +26,7 @@ package org.jclouds.http.commands.config;
|
||||||
import org.jclouds.http.commands.CommandFactory;
|
import org.jclouds.http.commands.CommandFactory;
|
||||||
import org.jclouds.http.commands.GetString;
|
import org.jclouds.http.commands.GetString;
|
||||||
import org.jclouds.http.commands.Head;
|
import org.jclouds.http.commands.Head;
|
||||||
|
import org.jclouds.http.commands.Put;
|
||||||
import org.jclouds.http.commands.callables.xml.ParseSax;
|
import org.jclouds.http.commands.callables.xml.ParseSax;
|
||||||
import org.jclouds.http.commands.callables.xml.config.SaxModule;
|
import org.jclouds.http.commands.callables.xml.config.SaxModule;
|
||||||
|
|
||||||
|
@ -34,30 +35,26 @@ import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.assistedinject.FactoryProvider;
|
import com.google.inject.assistedinject.FactoryProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* note that all this private factory clutter will go away when the following is
|
* note that all this private factory clutter will go away when the following is implemented @link
|
||||||
* implemented @link http://code.google.com/p/google-guice/issues/detail?id=346
|
* http://code.google.com/p/google-guice/issues/detail?id=346 it will be replaced with a
|
||||||
* it will be replaced with a configuration:
|
* configuration:
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
public class HttpCommandsModule extends AbstractModule {
|
public class HttpCommandsModule extends AbstractModule {
|
||||||
protected void configure() {
|
protected void configure() {
|
||||||
bind(CommandFactory.GetStringFactory.class)
|
bind(CommandFactory.GetStringFactory.class).toProvider(
|
||||||
.toProvider(
|
FactoryProvider.newFactory(CommandFactory.GetStringFactory.class, GetString.class));
|
||||||
FactoryProvider.newFactory(
|
bind(CommandFactory.HeadFactory.class).toProvider(
|
||||||
CommandFactory.GetStringFactory.class,
|
FactoryProvider.newFactory(CommandFactory.HeadFactory.class, Head.class));
|
||||||
GetString.class));
|
bind(CommandFactory.PutFactory.class).toProvider(
|
||||||
bind(CommandFactory.HeadFactory.class).toProvider(
|
FactoryProvider.newFactory(CommandFactory.PutFactory.class, Put.class));
|
||||||
FactoryProvider.newFactory(CommandFactory.HeadFactory.class,
|
install(new SaxModule());
|
||||||
Head.class));
|
bind(CommandFactory.ParseSaxFactory.class).toProvider(
|
||||||
|
FactoryProvider.newFactory(new TypeLiteral<CommandFactory.ParseSaxFactory>() {
|
||||||
|
}, new TypeLiteral<ParseSax<?>>() {
|
||||||
|
}));
|
||||||
|
|
||||||
install(new SaxModule());
|
}
|
||||||
bind(CommandFactory.ParseSaxFactory.class).toProvider(
|
|
||||||
FactoryProvider.newFactory(
|
|
||||||
new TypeLiteral<CommandFactory.ParseSaxFactory>() {
|
|
||||||
}, new TypeLiteral<ParseSax<?>>() {
|
|
||||||
}));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,44 +40,44 @@ import java.util.List;
|
||||||
|
|
||||||
public abstract class BaseHttpFutureCommandClient implements HttpFutureCommandClient {
|
public abstract class BaseHttpFutureCommandClient implements HttpFutureCommandClient {
|
||||||
|
|
||||||
protected final URL target;
|
protected final URL target;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
protected Logger logger = Logger.NULL;
|
protected Logger logger = Logger.NULL;
|
||||||
|
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
protected List<HttpRequestFilter> requestFilters = Collections.emptyList();
|
protected List<HttpRequestFilter> requestFilters = Collections.emptyList();
|
||||||
@RedirectHandler
|
@RedirectHandler
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
protected HttpResponseHandler redirectHandler = new CloseContentAndSetExceptionHandler();
|
protected HttpResponseHandler redirectHandler = new CloseContentAndSetExceptionHandler();
|
||||||
@ClientErrorHandler
|
@ClientErrorHandler
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
protected HttpResponseHandler clientErrorHandler = new CloseContentAndSetExceptionHandler();
|
protected HttpResponseHandler clientErrorHandler = new CloseContentAndSetExceptionHandler();
|
||||||
@ServerErrorHandler
|
@ServerErrorHandler
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
protected HttpResponseHandler serverErrorHandler = new CloseContentAndSetExceptionHandler();
|
protected HttpResponseHandler serverErrorHandler = new CloseContentAndSetExceptionHandler();
|
||||||
|
|
||||||
@RetryHandler
|
@RetryHandler
|
||||||
@Inject(optional = true)
|
@Inject(optional = true)
|
||||||
protected HttpRetryHandler httpRetryHandler = new BackoffLimitedRetryHandler(5);
|
protected HttpRetryHandler httpRetryHandler = new BackoffLimitedRetryHandler(5);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public BaseHttpFutureCommandClient(URL target) {
|
public BaseHttpFutureCommandClient(URL target) {
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void handleResponse(HttpFutureCommand<?> command, HttpResponse response) {
|
protected void handleResponse(HttpFutureCommand<?> command, HttpResponse response) {
|
||||||
int code = response.getStatusCode();
|
int code = response.getStatusCode();
|
||||||
if (code >= 500) {
|
if (code >= 500) {
|
||||||
this.serverErrorHandler.handle(command, response);
|
serverErrorHandler.handle(command, response);
|
||||||
} else if (code >= 400 && code < 500) {
|
} else if (code >= 400 && code < 500) {
|
||||||
this.clientErrorHandler.handle(command, response);
|
clientErrorHandler.handle(command, response);
|
||||||
} else if (code >= 300 && code < 400) {
|
} else if (code >= 300 && code < 400) {
|
||||||
this.redirectHandler.handle(command, response);
|
redirectHandler.handle(command, response);
|
||||||
} else {
|
} else {
|
||||||
command.getResponseFuture().setResponse(response);
|
command.getResponseFuture().setResponse(response);
|
||||||
command.getResponseFuture().run();
|
command.getResponseFuture().run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -31,8 +31,10 @@ import java.util.concurrent.TimeoutException;
|
||||||
import org.jclouds.http.commands.GetAndParseSax;
|
import org.jclouds.http.commands.GetAndParseSax;
|
||||||
import org.jclouds.http.commands.GetString;
|
import org.jclouds.http.commands.GetString;
|
||||||
import org.jclouds.http.commands.Head;
|
import org.jclouds.http.commands.Head;
|
||||||
|
import org.jclouds.http.commands.Put;
|
||||||
import org.jclouds.http.commands.callables.xml.ParseSax;
|
import org.jclouds.http.commands.callables.xml.ParseSax;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* // TODO: Adrian: Document this!
|
* // TODO: Adrian: Document this!
|
||||||
|
@ -42,77 +44,90 @@ import org.testng.annotations.Test;
|
||||||
@Test(threadPoolSize = 10)
|
@Test(threadPoolSize = 10)
|
||||||
public abstract class BaseHttpFutureCommandClientTest extends BaseJettyTest {
|
public abstract class BaseHttpFutureCommandClientTest extends BaseJettyTest {
|
||||||
|
|
||||||
@Test(invocationCount = 50, timeOut = 3000)
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
public void testRequestFilter() throws MalformedURLException,
|
public void testRequestFilter() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
GetString get = factory.createGetString("/");
|
GetString get = factory.createGetString("/");
|
||||||
get.getRequest().getHeaders().put("filterme", "filterme");
|
get.getRequest().getHeaders().put("filterme", "filterme");
|
||||||
client.submit(get);
|
client.submit(get);
|
||||||
assert get.get(10, TimeUnit.SECONDS).trim().equals("test") : String
|
assertEquals(get.get(10, TimeUnit.SECONDS).trim(), "test");
|
||||||
.format("expected: [%1$s], but got [%2$s]", "test", get.get(10,
|
}
|
||||||
TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(invocationCount = 50, timeOut = 3000)
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
public void testGetStringWithHeader() throws MalformedURLException,
|
public void testGetStringWithHeader() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
GetString get = factory.createGetString("/");
|
GetString get = factory.createGetString("/");
|
||||||
get.getRequest().getHeaders().put("test", "test");
|
get.getRequest().getHeaders().put("test", "test");
|
||||||
client.submit(get);
|
client.submit(get);
|
||||||
assert get.get(10, TimeUnit.SECONDS).trim().equals("test") : String
|
assertEquals(get.get(10, TimeUnit.SECONDS).trim(), "test");
|
||||||
.format("expected: [%1$s], but got [%2$s]", "test", get.get(10,
|
}
|
||||||
TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(invocationCount = 50, timeOut = 3000)
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
public void testGetString() throws MalformedURLException,
|
public void testGetString() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
GetString get = factory.createGetString("/");
|
GetString get = factory.createGetString("/");
|
||||||
assert get != null;
|
assert get != null;
|
||||||
client.submit(get);
|
client.submit(get);
|
||||||
assert get.get(10, TimeUnit.SECONDS).trim().equals(XML) : String
|
assertEquals(get.get(10, TimeUnit.SECONDS).trim(), XML);
|
||||||
.format("expected: [%1$s], but got [%2$s]", XML, get.get(10,
|
|
||||||
TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(invocationCount = 50, timeOut = 3000)
|
}
|
||||||
public void testHead() throws MalformedURLException, ExecutionException,
|
|
||||||
InterruptedException, TimeoutException {
|
|
||||||
Head head = factory.createHead("/");
|
|
||||||
assert head != null;
|
|
||||||
client.submit(head);
|
|
||||||
assert head.get(10, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(invocationCount = 50, timeOut = 3000)
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
public void testGetAndParseSax() throws MalformedURLException,
|
public void testGetStringRedirect() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
GetAndParseSax<?> getAndParseSax = factory.createGetAndParseSax("/",
|
GetString get = factory.createGetString("/redirect");
|
||||||
new ParseSax.HandlerWithResult<String>() {
|
assert get != null;
|
||||||
@Override
|
client.submit(get);
|
||||||
public String getResult() {
|
assertEquals(get.get(10, TimeUnit.SECONDS).trim(), XML2);
|
||||||
return bar;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private String bar = null;
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
private StringBuilder currentText = new StringBuilder();
|
public void testPutRedirect() throws MalformedURLException, ExecutionException,
|
||||||
|
InterruptedException, TimeoutException {
|
||||||
|
Put put = factory.createPut("/redirect", "foo");
|
||||||
|
assert put != null;
|
||||||
|
client.submit(put);
|
||||||
|
assertEquals(put.get(10, TimeUnit.SECONDS), new Boolean(true));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
public void endElement(String uri, String name, String qName) {
|
public void testHead() throws MalformedURLException, ExecutionException, InterruptedException,
|
||||||
|
TimeoutException {
|
||||||
|
Head head = factory.createHead("/");
|
||||||
|
assert head != null;
|
||||||
|
client.submit(head);
|
||||||
|
assert head.get(10, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
if (qName.equals("bar")) {
|
@Test(invocationCount = 50, timeOut = 3000)
|
||||||
bar = currentText.toString();
|
public void testGetAndParseSax() throws MalformedURLException, ExecutionException,
|
||||||
}
|
InterruptedException, TimeoutException {
|
||||||
currentText = new StringBuilder();
|
GetAndParseSax<?> getAndParseSax = factory.createGetAndParseSax("/",
|
||||||
}
|
new ParseSax.HandlerWithResult<String>() {
|
||||||
|
@Override
|
||||||
|
public String getResult() {
|
||||||
|
return bar;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
private String bar = null;
|
||||||
public void characters(char ch[], int start, int length) {
|
private StringBuilder currentText = new StringBuilder();
|
||||||
currentText.append(ch, start, length);
|
|
||||||
}
|
@Override
|
||||||
});
|
public void endElement(String uri, String name, String qName) {
|
||||||
assert getAndParseSax != null;
|
|
||||||
client.submit(getAndParseSax);
|
if (qName.equals("bar")) {
|
||||||
assert getAndParseSax.get(10, TimeUnit.SECONDS).equals("whoppers");
|
bar = currentText.toString();
|
||||||
}
|
}
|
||||||
|
currentText = new StringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void characters(char ch[], int start, int length) {
|
||||||
|
currentText.append(ch, start, length);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert getAndParseSax != null;
|
||||||
|
client.submit(getAndParseSax);
|
||||||
|
assertEquals(getAndParseSax.get(10, TimeUnit.SECONDS), "whoppers");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,25 +56,30 @@ import com.google.inject.name.Names;
|
||||||
|
|
||||||
public abstract class BaseJettyTest {
|
public abstract class BaseJettyTest {
|
||||||
protected static final String XML = "<foo><bar>whoppers</bar></foo>";
|
protected static final String XML = "<foo><bar>whoppers</bar></foo>";
|
||||||
|
protected static final String XML2 = "<foo><bar>chubbs</bar></foo>";
|
||||||
|
|
||||||
protected Server server = null;
|
protected Server server = null;
|
||||||
protected CommandFactory factory;
|
protected CommandFactory factory;
|
||||||
protected HttpFutureCommandClient client;
|
protected HttpFutureCommandClient client;
|
||||||
protected Injector injector;
|
protected Injector injector;
|
||||||
private Closer closer;
|
private Closer closer;
|
||||||
private AtomicInteger cycle = new AtomicInteger(0);
|
private AtomicInteger cycle = new AtomicInteger(0);
|
||||||
|
private Server server2;
|
||||||
|
|
||||||
@BeforeTest
|
@BeforeTest
|
||||||
@Parameters( { "test-jetty-port" })
|
@Parameters( { "test-jetty-port" })
|
||||||
public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
|
public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
|
||||||
Handler handler = new AbstractHandler() {
|
Handler server1Handler = new AbstractHandler() {
|
||||||
|
|
||||||
public void handle(String target, HttpServletRequest request,
|
public void handle(String target, HttpServletRequest request,
|
||||||
HttpServletResponse response, int dispatch) throws IOException, ServletException {
|
HttpServletResponse response, int dispatch) throws IOException, ServletException {
|
||||||
failIfNoContentLength(request, response);
|
if (failIfNoContentLength(request, response))
|
||||||
if (request.getHeader("test") != null) {
|
return;
|
||||||
|
else if (request.getHeader("test") != null) {
|
||||||
response.setContentType("text/plain");
|
response.setContentType("text/plain");
|
||||||
response.setStatus(HttpServletResponse.SC_OK);
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
response.getWriter().println("test");
|
response.getWriter().println("test");
|
||||||
|
} else if (target.equals("/redirect")) {
|
||||||
|
response.sendRedirect("http://localhost:" + (testPort + 1));
|
||||||
} else {
|
} else {
|
||||||
if (failOnRequest(request, response))
|
if (failOnRequest(request, response))
|
||||||
return;
|
return;
|
||||||
|
@ -87,8 +92,23 @@ public abstract class BaseJettyTest {
|
||||||
};
|
};
|
||||||
|
|
||||||
server = new Server(testPort);
|
server = new Server(testPort);
|
||||||
server.setHandler(handler);
|
server.setHandler(server1Handler);
|
||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
|
Handler server2Handler = new AbstractHandler() {
|
||||||
|
public void handle(String target, HttpServletRequest request,
|
||||||
|
HttpServletResponse response, int dispatch) throws IOException, ServletException {
|
||||||
|
response.setContentType("text/xml");
|
||||||
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
response.getWriter().println(XML2);
|
||||||
|
((Request) request).setHandled(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
server2 = new Server(testPort + 1);
|
||||||
|
server2.setHandler(server2Handler);
|
||||||
|
server2.start();
|
||||||
|
|
||||||
final Properties properties = new Properties();
|
final Properties properties = new Properties();
|
||||||
properties.put(HttpConstants.PROPERTY_HTTP_ADDRESS, "localhost");
|
properties.put(HttpConstants.PROPERTY_HTTP_ADDRESS, "localhost");
|
||||||
properties.put(HttpConstants.PROPERTY_HTTP_PORT, testPort + "");
|
properties.put(HttpConstants.PROPERTY_HTTP_PORT, testPort + "");
|
||||||
|
@ -124,6 +144,7 @@ public abstract class BaseJettyTest {
|
||||||
@AfterTest
|
@AfterTest
|
||||||
public void tearDownJetty() throws Exception {
|
public void tearDownJetty() throws Exception {
|
||||||
closer.close();
|
closer.close();
|
||||||
|
server2.stop();
|
||||||
server.stop();
|
server.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,34 +29,33 @@ import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* // TODO: Adrian: Document this!
|
* Tests the basic structure of the {@link GetString} object
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public class GetStringTest {
|
public class GetStringTest {
|
||||||
private static final String GOOD_PATH = "/index.html";
|
private static final String GOOD_PATH = "/index.html";
|
||||||
|
|
||||||
private GetString get = null;
|
private GetString get = null;
|
||||||
private ReturnStringIf200 callable = null;
|
private ReturnStringIf200 callable = null;
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
void setUp() {
|
void setUp() {
|
||||||
callable = new ReturnStringIf200();
|
callable = new ReturnStringIf200();
|
||||||
get = new GetString(callable, GOOD_PATH);
|
get = new GetString(callable, GOOD_PATH);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
@AfterMethod
|
||||||
|
void tearDown() {
|
||||||
|
get = null;
|
||||||
|
callable = null;
|
||||||
|
}
|
||||||
|
|
||||||
@AfterMethod
|
@Test
|
||||||
void tearDown() {
|
public void testConstructor() {
|
||||||
get = null;
|
assert get.getResponseFuture() != null;
|
||||||
callable = null;
|
assert get.getRequest().getUri().equals(GOOD_PATH);
|
||||||
}
|
assert get.getRequest().getMethod().equals("GET");
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void testConstructor() {
|
|
||||||
assert get.getResponseFuture() != null;
|
|
||||||
assert get.getRequest().getUri().equals(GOOD_PATH);
|
|
||||||
assert get.getRequest().getMethod().equals("GET");
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -43,6 +43,7 @@ import org.jclouds.http.HttpRequestFilter;
|
||||||
import org.jclouds.http.HttpResponse;
|
import org.jclouds.http.HttpResponse;
|
||||||
import org.jclouds.http.internal.BaseHttpFutureCommandClient;
|
import org.jclouds.http.internal.BaseHttpFutureCommandClient;
|
||||||
|
|
||||||
|
import com.google.appengine.api.urlfetch.FetchOptions;
|
||||||
import com.google.appengine.api.urlfetch.HTTPHeader;
|
import com.google.appengine.api.urlfetch.HTTPHeader;
|
||||||
import com.google.appengine.api.urlfetch.HTTPMethod;
|
import com.google.appengine.api.urlfetch.HTTPMethod;
|
||||||
import com.google.appengine.api.urlfetch.HTTPRequest;
|
import com.google.appengine.api.urlfetch.HTTPRequest;
|
||||||
|
@ -166,8 +167,13 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
|
||||||
} else {
|
} else {
|
||||||
url = new URL(target, request.getUri());
|
url = new URL(target, request.getUri());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FetchOptions options = disallowTruncate();
|
||||||
|
followRedirectsUnlessRequestContainsPayload(request, options);
|
||||||
|
|
||||||
HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod()),
|
HTTPRequest gaeRequest = new HTTPRequest(url, HTTPMethod.valueOf(request.getMethod()),
|
||||||
disallowTruncate().doNotFollowRedirects());
|
options);
|
||||||
|
|
||||||
for (String header : request.getHeaders().keySet()) {
|
for (String header : request.getHeaders().keySet()) {
|
||||||
// GAE/J v1.2.1 re-writes the host header, so we'll skip it.
|
// GAE/J v1.2.1 re-writes the host header, so we'll skip it.
|
||||||
if (!header.equals(HttpConstants.HOST)) {
|
if (!header.equals(HttpConstants.HOST)) {
|
||||||
|
@ -176,6 +182,7 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.getPayload() != null) {
|
if (request.getPayload() != null) {
|
||||||
changeRequestContentToBytes(request);
|
changeRequestContentToBytes(request);
|
||||||
gaeRequest.setPayload((byte[]) request.getPayload());
|
gaeRequest.setPayload((byte[]) request.getPayload());
|
||||||
|
@ -184,4 +191,12 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient {
|
||||||
}
|
}
|
||||||
return gaeRequest;
|
return gaeRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void followRedirectsUnlessRequestContainsPayload(HttpRequest request,
|
||||||
|
FetchOptions options) {
|
||||||
|
if (request.getPayload() != null)
|
||||||
|
options.doNotFollowRedirects();
|
||||||
|
else
|
||||||
|
options.followRedirects();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,9 +34,9 @@ import org.jclouds.http.BaseHttpFutureCommandClientTest;
|
||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.inject.Module;
|
|
||||||
import com.google.appengine.tools.development.ApiProxyLocalImpl;
|
import com.google.appengine.tools.development.ApiProxyLocalImpl;
|
||||||
import com.google.apphosting.api.ApiProxy;
|
import com.google.apphosting.api.ApiProxy;
|
||||||
|
import com.google.inject.Module;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -45,97 +45,112 @@ import com.google.apphosting.api.ApiProxy;
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public class URLFetchServiceClientIntegrationTest extends
|
public class URLFetchServiceClientIntegrationTest extends BaseHttpFutureCommandClientTest {
|
||||||
BaseHttpFutureCommandClientTest {
|
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
void setupApiProxy() {
|
void setupApiProxy() {
|
||||||
ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
|
ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
|
||||||
ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")) {
|
ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Test(invocationCount = 50, timeOut = 1500)
|
@Test(invocationCount = 50, timeOut = 1500)
|
||||||
public void testGetAndParseSax() throws MalformedURLException,
|
public void testGetAndParseSax() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
setupApiProxy();
|
setupApiProxy();
|
||||||
super.testGetAndParseSax();
|
super.testGetAndParseSax();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Test(invocationCount = 50, timeOut = 1500)
|
@Test(invocationCount = 50, timeOut = 1500)
|
||||||
public void testGetString() throws MalformedURLException,
|
public void testGetString() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
setupApiProxy();
|
setupApiProxy();
|
||||||
super.testGetString();
|
super.testGetString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Test(enabled = false)
|
@Test(invocationCount = 50, timeOut = 1500)
|
||||||
public void testGetStringWithHeader() throws MalformedURLException,
|
public void testGetStringRedirect() throws MalformedURLException, ExecutionException,
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
// GAE does not support sending headers in their test stub as of version
|
setupApiProxy();
|
||||||
// 1.2.0
|
super.testGetStringRedirect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Test(invocationCount = 50, timeOut = 1500)
|
@Test(enabled = false, invocationCount = 50, timeOut = 1500)
|
||||||
public void testHead() throws MalformedURLException, ExecutionException,
|
public void testPutRedirect() throws MalformedURLException, ExecutionException,
|
||||||
InterruptedException, TimeoutException {
|
InterruptedException, TimeoutException {
|
||||||
setupApiProxy();
|
setupApiProxy();
|
||||||
super.testHead();
|
super.testPutRedirect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(enabled = false)
|
@Override
|
||||||
public void testRequestFilter() throws MalformedURLException,
|
@Test(enabled = false)
|
||||||
ExecutionException, InterruptedException, TimeoutException {
|
public void testGetStringWithHeader() throws MalformedURLException, ExecutionException,
|
||||||
// GAE does not support sending headers in their test stub as of version
|
InterruptedException, TimeoutException {
|
||||||
// 1.2.0
|
// GAE does not support sending headers in their test stub as of version
|
||||||
}
|
// 1.2.0
|
||||||
|
}
|
||||||
|
|
||||||
class TestEnvironment implements ApiProxy.Environment {
|
@Override
|
||||||
public String getAppId() {
|
@Test(invocationCount = 50, timeOut = 1500)
|
||||||
return "Unit Tests";
|
public void testHead() throws MalformedURLException, ExecutionException, InterruptedException,
|
||||||
}
|
TimeoutException {
|
||||||
|
setupApiProxy();
|
||||||
|
super.testHead();
|
||||||
|
}
|
||||||
|
|
||||||
public String getVersionId() {
|
@Test(enabled = false)
|
||||||
return "1.0";
|
public void testRequestFilter() throws MalformedURLException, ExecutionException,
|
||||||
}
|
InterruptedException, TimeoutException {
|
||||||
|
// GAE does not support sending headers in their test stub as of version
|
||||||
|
// 1.2.0
|
||||||
|
}
|
||||||
|
|
||||||
public void setDefaultNamespace(String s) {
|
class TestEnvironment implements ApiProxy.Environment {
|
||||||
}
|
public String getAppId() {
|
||||||
|
return "Unit Tests";
|
||||||
|
}
|
||||||
|
|
||||||
public String getRequestNamespace() {
|
public String getVersionId() {
|
||||||
return null;
|
return "1.0";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultNamespace() {
|
public void setDefaultNamespace(String s) {
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public String getAuthDomain() {
|
public String getRequestNamespace() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLoggedIn() {
|
public String getDefaultNamespace() {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getEmail() {
|
public String getAuthDomain() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAdmin() {
|
public boolean isLoggedIn() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected Module createClientModule() {
|
public String getEmail() {
|
||||||
return new URLFetchServiceClientModule();
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public boolean isAdmin() {
|
||||||
protected void addConnectionProperties(Properties props) {
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Module createClientModule() {
|
||||||
|
return new URLFetchServiceClientModule();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void addConnectionProperties(Properties props) {
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -23,38 +23,48 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.http.httpnio.pool;
|
package org.jclouds.http.httpnio.pool;
|
||||||
|
|
||||||
import com.google.inject.Module;
|
import java.net.MalformedURLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import org.jclouds.command.pool.PoolConstants;
|
import org.jclouds.command.pool.PoolConstants;
|
||||||
import org.jclouds.http.BaseHttpFutureCommandClientTest;
|
import org.jclouds.http.BaseHttpFutureCommandClientTest;
|
||||||
import org.jclouds.http.httpnio.config.HttpNioConnectionPoolClientModule;
|
import org.jclouds.http.httpnio.config.HttpNioConnectionPoolClientModule;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.util.Properties;
|
import com.google.inject.Module;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* // TODO: Adrian: Document this!
|
* Tests for {@link HttpNioConnectionPoolFutureCommandClient}.
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public class HttpNioConnectionPoolFutureCommandClientTest extends
|
public class HttpNioConnectionPoolFutureCommandClientTest extends BaseHttpFutureCommandClientTest {
|
||||||
BaseHttpFutureCommandClientTest {
|
|
||||||
|
|
||||||
protected void addConnectionProperties(Properties properties) {
|
protected void addConnectionProperties(Properties properties) {
|
||||||
properties.setProperty(
|
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE, "75");
|
||||||
PoolConstants.PROPERTY_POOL_MAX_CONNECTION_REUSE, "75");
|
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES, "2");
|
||||||
properties.setProperty(
|
properties.setProperty(PoolConstants.PROPERTY_POOL_REQUEST_INVOKER_THREADS, "1");
|
||||||
PoolConstants.PROPERTY_POOL_MAX_SESSION_FAILURES, "2");
|
properties.setProperty(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS, "2");
|
||||||
properties.setProperty(
|
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS, "12");
|
||||||
PoolConstants.PROPERTY_POOL_REQUEST_INVOKER_THREADS, "1");
|
}
|
||||||
properties.setProperty(PoolConstants.PROPERTY_POOL_IO_WORKER_THREADS,
|
|
||||||
"2");
|
protected Module createClientModule() {
|
||||||
properties.setProperty(PoolConstants.PROPERTY_POOL_MAX_CONNECTIONS,
|
return new HttpNioConnectionPoolClientModule();
|
||||||
"12");
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
|
@Test(enabled = false)
|
||||||
|
public void testGetStringRedirect() throws MalformedURLException, ExecutionException,
|
||||||
|
InterruptedException, TimeoutException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Test(enabled = false)
|
||||||
|
public void testPutRedirect() throws MalformedURLException, ExecutionException,
|
||||||
|
InterruptedException, TimeoutException {
|
||||||
|
}
|
||||||
|
|
||||||
protected Module createClientModule() {
|
|
||||||
return new HttpNioConnectionPoolClientModule();
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue