fixed built-in http client

This commit is contained in:
Adrian Cole 2011-01-25 10:23:58 -08:00
parent 0a769ca6f5
commit 1df15209ec
3 changed files with 190 additions and 182 deletions

View File

@ -47,6 +47,7 @@ public class BaseBlobSignerLiveTest extends BaseBlobStoreIntegrationTest {
String container = getContainerName(); String container = getContainerName();
try { try {
context.getBlobStore().putBlob(container, blob); context.getBlobStore().putBlob(container, blob);
assertConsistencyAwareContainerSize(container, 1);
HttpRequest request = context.getSigner().signRemoveBlob(container, name); HttpRequest request = context.getSigner().signRemoveBlob(container, name);
assertEquals(request.getFilters().size(), 0); assertEquals(request.getFilters().size(), 0);
context.utils().http().invoke(request); context.utils().http().invoke(request);
@ -67,6 +68,7 @@ public class BaseBlobSignerLiveTest extends BaseBlobStoreIntegrationTest {
String container = getContainerName(); String container = getContainerName();
try { try {
context.getBlobStore().putBlob(container, blob); context.getBlobStore().putBlob(container, blob);
assertConsistencyAwareContainerSize(container, 1);
HttpRequest request = context.getSigner().signGetBlob(container, name); HttpRequest request = context.getSigner().signGetBlob(container, name);
assertEquals(request.getFilters().size(), 0); assertEquals(request.getFilters().size(), 0);
assertEquals(Strings2.toStringAndClose(context.utils().http().invoke(request).getPayload().getInput()), text); assertEquals(Strings2.toStringAndClose(context.utils().http().invoke(request).getPayload().getInput()), text);
@ -88,7 +90,7 @@ public class BaseBlobSignerLiveTest extends BaseBlobStoreIntegrationTest {
HttpRequest request = context.getSigner().signPutBlob(container, blob); HttpRequest request = context.getSigner().signPutBlob(container, blob);
assertEquals(request.getFilters().size(), 0); assertEquals(request.getFilters().size(), 0);
Strings2.toStringAndClose(context.utils().http().invoke(request).getPayload().getInput()); Strings2.toStringAndClose(context.utils().http().invoke(request).getPayload().getInput());
assert context.getBlobStore().blobExists(container, name); assertConsistencyAwareContainerSize(container, 1);
} finally { } finally {
returnContainer(container); returnContainer(container);
} }

View File

@ -435,6 +435,8 @@ public class RestAnnotationProcessor<T> {
Multimap<String, String> queryParams = addQueryParams(tokenValues.entries(), method, args); Multimap<String, String> queryParams = addQueryParams(tokenValues.entries(), method, args);
Multimap<String, String> matrixParams = addMatrixParams(tokenValues.entries(), method, args); Multimap<String, String> matrixParams = addMatrixParams(tokenValues.entries(), method, args);
Multimap<String, String> headers = buildHeaders(tokenValues.entries(), method, args); Multimap<String, String> headers = buildHeaders(tokenValues.entries(), method, args);
if (r != null)
headers.putAll(r.getHeaders());
if (shouldAddHostHeader(method)) if (shouldAddHostHeader(method))
headers.put(HOST, endpoint.getHost()); headers.put(HOST, endpoint.getHost());

View File

@ -209,8 +209,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testDelegateAsyncIncludesVersion() throws SecurityException, NoSuchMethodException, InterruptedException, public void testDelegateAsyncIncludesVersion() throws SecurityException, NoSuchMethodException,
ExecutionException { InterruptedException, ExecutionException {
Injector child = injectorForClient(); Injector child = injectorForClient();
TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class); TransformingHttpCommandExecutorService mock = child.getInstance(TransformingHttpCommandExecutorService.class);
@ -224,8 +224,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
AsyncCaller caller = child.getInstance(AsyncCaller.class); AsyncCaller caller = child.getInstance(AsyncCaller.class);
expect(mock.submit(requestLineEquals("GET http://localhost:9999/client/1/foo HTTP/1.1"), eq(function))).andReturn( expect(mock.submit(requestLineEquals("GET http://localhost:9999/client/1/foo HTTP/1.1"), eq(function)))
createNiceMock(ListenableFuture.class)).atLeastOnce(); .andReturn(createNiceMock(ListenableFuture.class)).atLeastOnce();
replay(mock); replay(mock);
caller.getCallee().onePath("foo"); caller.getCallee().onePath("foo");
@ -268,8 +268,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
} }
Caller caller = child.getInstance(Caller.class); Caller caller = child.getInstance(Caller.class);
expect(mock.submit(requestLineEquals("GET http://localhost:1111/client/1/foo HTTP/1.1"), eq(function))).andReturn( expect(mock.submit(requestLineEquals("GET http://localhost:1111/client/1/foo HTTP/1.1"), eq(function)))
Futures.<Void> immediateFuture(null)).atLeastOnce(); .andReturn(Futures.<Void> immediateFuture(null)).atLeastOnce();
replay(mock); replay(mock);
caller.getCallee().onePath("foo"); caller.getCallee().onePath("foo");
@ -281,8 +281,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
private Injector injectorForClient() { private Injector injectorForClient() {
RestContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", RestContextSpec<Caller, AsyncCaller> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo",
null, Caller.class, AsyncCaller.class, null, Caller.class, AsyncCaller.class, ImmutableSet.<Module> of(new MockModule(),
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new CallerCalleeModule())); new NullLoggingModule(), new CallerCalleeModule()));
return createContextBuilder(contextSpec).buildInjector(); return createContextBuilder(contextSpec).buildInjector();
@ -297,7 +297,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
}; };
@Target({ ElementType.METHOD }) @Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@javax.ws.rs.HttpMethod("FOO") @javax.ws.rs.HttpMethod("FOO")
public @interface FOO { public @interface FOO {
@ -764,7 +764,7 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
ListenableFuture<? extends Set<String>> testUnwrap4(); ListenableFuture<? extends Set<String>> testUnwrap4();
@Target({ ElementType.METHOD }) @Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@HttpMethod("ROWDY") @HttpMethod("ROWDY")
public @interface ROWDY { public @interface ROWDY {
@ -836,8 +836,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of(
ImmutableMap.of("foo", "bar")); "foo", "bar"));
} }
@ -852,8 +852,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of(
ImmutableMap.of("foo", "bar")); "foo", "bar"));
} }
@ -868,8 +868,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor Function<HttpResponse, Map<String, String>> parser = (Function<HttpResponse, Map<String, String>>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request); .createResponseParser(parserFactory, injector, method, request);
assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), assertEquals(parser.apply(new HttpResponse(200, "ok", newStringPayload("{ foo:\"bar\"}"))), ImmutableMap.of(
ImmutableMap.of("foo", "bar")); "foo", "bar"));
} }
@ -979,9 +979,12 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testRequestFilterOverrideOnRequest() throws SecurityException, NoSuchMethodException { public void testRequestFilterOverrideOnRequest() throws SecurityException, NoSuchMethodException {
Method method = TestRequestFilter.class.getMethod("getOverride", HttpRequest.class); Method method = TestRequestFilter.class.getMethod("getOverride", HttpRequest.class);
HttpRequest request = factory(TestRequestFilter.class).createRequest(method, HttpRequest request = factory(TestRequestFilter.class).createRequest(
HttpRequest.builder().method("GET").endpoint(URI.create("http://localhost")).build()); method,
HttpRequest.builder().method("GET").endpoint(URI.create("http://localhost")).headers(
ImmutableMultimap.of("foo", "bar")).build());
assertEquals(request.getFilters().size(), 1); assertEquals(request.getFilters().size(), 1);
assertEquals(request.getHeaders().size(), 1);
assertEquals(request.getFilters().get(0).getClass(), TestRequestFilter2.class); assertEquals(request.getFilters().get(0).getClass(), TestRequestFilter2.class);
} }
@ -1224,8 +1227,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@Test @Test
public void testQueryInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testQueryInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneQuery = TestQueryReplace.class.getMethod("queryInOptions", String.class, TestReplaceQueryOptions.class); Method oneQuery = TestQueryReplace.class.getMethod("queryInOptions", String.class, TestReplaceQueryOptions.class);
String query = factory(TestQueryReplace.class) String query = factory(TestQueryReplace.class).createRequest(oneQuery,
.createRequest(oneQuery, new Object[] { "robot", new TestReplaceQueryOptions() }).getEndpoint().getQuery(); new Object[] { "robot", new TestReplaceQueryOptions() }).getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/robot"); assertEquals(query, "x-amz-copy-source=/robot");
} }
@ -1321,8 +1324,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testBuildTwoQuerysOutOfOrder() throws SecurityException, NoSuchMethodException, public void testBuildTwoQuerysOutOfOrder() throws SecurityException, NoSuchMethodException,
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoQuerysOutOfOrder = TestQueryReplace.class.getMethod("twoQuerysOutOfOrder", String.class, String.class); Method twoQuerysOutOfOrder = TestQueryReplace.class.getMethod("twoQuerysOutOfOrder", String.class, String.class);
String query = factory(TestQueryReplace.class) String query = factory(TestQueryReplace.class).createRequest(twoQuerysOutOfOrder,
.createRequest(twoQuerysOutOfOrder, new Object[] { "robot", "eggs" }).getEndpoint().getQuery(); new Object[] { "robot", "eggs" }).getEndpoint().getQuery();
assertEquals(query, "x-amz-copy-source=/eggs/robot"); assertEquals(query, "x-amz-copy-source=/eggs/robot");
} }
@ -1336,8 +1339,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testMatrixInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException { public void testMatrixInOptions() throws SecurityException, NoSuchMethodException, UnsupportedEncodingException {
Method oneMatrix = TestMatrixReplace.class.getMethod("matrixInOptions", String.class, Method oneMatrix = TestMatrixReplace.class.getMethod("matrixInOptions", String.class,
TestReplaceMatrixOptions.class); TestReplaceMatrixOptions.class);
String path = factory(TestMatrixReplace.class) String path = factory(TestMatrixReplace.class).createRequest(oneMatrix,
.createRequest(oneMatrix, new Object[] { "robot", new TestReplaceMatrixOptions() }).getEndpoint().getPath(); new Object[] { "robot", new TestReplaceMatrixOptions() }).getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/robot"); assertEquals(path, "/;x-amz-copy-source=/robot");
} }
@ -1420,8 +1423,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
UnsupportedEncodingException { UnsupportedEncodingException {
Method twoMatrixsOutOfOrder = TestMatrixReplace.class.getMethod("twoMatrixsOutOfOrder", String.class, Method twoMatrixsOutOfOrder = TestMatrixReplace.class.getMethod("twoMatrixsOutOfOrder", String.class,
String.class); String.class);
String path = factory(TestMatrixReplace.class) String path = factory(TestMatrixReplace.class).createRequest(twoMatrixsOutOfOrder,
.createRequest(twoMatrixsOutOfOrder, new Object[] { "robot", "eggs" }).getEndpoint().getPath(); new Object[] { "robot", "eggs" }).getEndpoint().getPath();
assertEquals(path, "/;x-amz-copy-source=/eggs/robot"); assertEquals(path, "/;x-amz-copy-source=/eggs/robot");
} }
@ -1483,8 +1486,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
public void testPutInputStreamPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException, public void testPutInputStreamPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException,
IOException { IOException {
Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class); Method method = TestTransformers.class.getMethod("put", PayloadEnclosing.class);
PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl( PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(newInputStreamPayload(Strings2
newInputStreamPayload(Strings2.toInputStream("whoops"))); .toInputStream("whoops")));
calculateMD5(payloadEnclosing, crypto.md5()); calculateMD5(payloadEnclosing, crypto.md5());
HttpRequest request = factory(TestQuery.class).createRequest(method, payloadEnclosing); HttpRequest request = factory(TestQuery.class).createRequest(method, payloadEnclosing);
@ -1626,8 +1629,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
RestAnnotationProcessor<TestTransformers> processor = factory(TestTransformers.class); RestAnnotationProcessor<TestTransformers> processor = factory(TestTransformers.class);
Method method = TestTransformers.class.getMethod("oneTransformerWithContext"); Method method = TestTransformers.class.getMethod("oneTransformerWithContext");
GeneratedHttpRequest<TestTransformers> request = GeneratedHttpRequest.<TestTransformers> builder().method("GET") GeneratedHttpRequest<TestTransformers> request = GeneratedHttpRequest.<TestTransformers> builder().method("GET")
.endpoint(URI.create("http://localhost")).declaring(TestTransformers.class).javaMethod(method) .endpoint(URI.create("http://localhost")).declaring(TestTransformers.class).javaMethod(method).args(
.args(new Object[] {}).build(); new Object[] {}).build();
Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request); Function<HttpResponse, ?> transformer = processor.createResponseParser(method, request);
assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(transformer.getClass(), ReturnStringIf200Context.class);
assertEquals(((ReturnStringIf200Context) transformer).request, request); assertEquals(((ReturnStringIf200Context) transformer).request, request);
@ -1694,8 +1697,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getMethod(), HttpMethod.GET);
assertEquals(request.getHeaders().size(), 2); assertEquals(request.getHeaders().size(), 2);
assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost")); assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost"));
assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), Collections.singletonList(dateService
Collections.singletonList(dateService.rfc822DateFormat(date))); .rfc822DateFormat(date)));
} }
public void testCreateGetOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException { public void testCreateGetOptionsThatProducesHeaders() throws SecurityException, NoSuchMethodException {
@ -1708,8 +1711,8 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getMethod(), HttpMethod.GET);
assertEquals(request.getHeaders().size(), 2); assertEquals(request.getHeaders().size(), 2);
assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost")); assertEquals(request.getHeaders().get(HttpHeaders.HOST), Collections.singletonList("localhost"));
assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), assertEquals(request.getHeaders().get(HttpHeaders.IF_MODIFIED_SINCE), Collections.singletonList(dateService
Collections.singletonList(dateService.rfc822DateFormat(date))); .rfc822DateFormat(date)));
} }
public class PrefixOptions extends BaseHttpRequestOptions { public class PrefixOptions extends BaseHttpRequestOptions {
@ -2098,12 +2101,13 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
@BeforeClass @BeforeClass
void setupFactory() { void setupFactory() {
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null, RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "userfoo", null,
String.class, Integer.class, String.class, Integer.class, ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(),
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new AbstractModule() { new AbstractModule() {
@Override @Override
protected void configure() { protected void configure() {
bind(URI.class).annotatedWith(Localhost2.class).toInstance(URI.create("http://localhost:1111")); bind(URI.class).annotatedWith(Localhost2.class).toInstance(
URI.create("http://localhost:1111"));
} }
})); }));