use of com.spotify.apollo.Request in project apollo by spotify.
the class HttpClientTest method testSend.
@Test
public void testSend() throws Exception {
mockServerClient.when(request().withMethod("GET").withPath("/foo.php").withQueryStringParameter("bar", "baz").withQueryStringParameter("qur", "quz")).respond(response().withStatusCode(204));
String uri = format("http://localhost:%d/foo.php?bar=baz&qur=quz", mockServerRule.getHttpPort());
Request request = Request.forUri(uri, "GET");
Response<ByteString> response = HttpClient.createUnconfigured().send(request, empty()).toCompletableFuture().get();
assertThat(response.status(), withCode(204));
assertThat(response.payload(), is(empty()));
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class HttpClientTest method testNoDuplicatedAuthContext.
@Test
public void testNoDuplicatedAuthContext() throws Exception {
mockServerClient.when(request().withHeader("Authorization", "Basic dXNlcjpwYXNz")).respond(response().withHeader("x-auth-was-fine", "yes"));
String uri = format("http://localhost:%d/foo.php", mockServerRule.getHttpPort());
Request request = Request.forUri(uri, "GET").withHeader("Authorization", "Basic dXNlcjpwYXNz");
Request originalRequest = Request.forUri("http://original.uri/").withHeader("Authorization", "Basic foobar");
final Response<ByteString> response = HttpClient.createUnconfigured().send(request, Optional.of(originalRequest)).toCompletableFuture().get();
assertThat(response.headers().get("x-auth-was-fine"), equalTo(Optional.of("yes")));
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class HttpClientTest method testSendWithBody.
@Test
public void testSendWithBody() throws Exception {
mockServerClient.when(request().withMethod("POST").withPath("/foo.php").withQueryStringParameter("bar", "baz").withQueryStringParameter("qur", "quz").withHeader("Content-Type", "application/x-spotify-greeting").withBody("hello")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/x-spotify-location").withHeader("Vary", "Content-Type").withHeader("Vary", "Accept").withBody("world"));
String uri = format("http://localhost:%d/foo.php?bar=baz&qur=quz", mockServerRule.getHttpPort());
Request request = Request.forUri(uri, "POST").withHeader("Content-Type", "application/x-spotify-greeting").withPayload(ByteString.encodeUtf8("hello"));
Response<ByteString> response = HttpClient.createUnconfigured().send(request, empty()).toCompletableFuture().get();
assertThat(response.status(), withCode(200));
assertThat(response.headers().asMap(), allOf(hasEntry("content-type", "application/x-spotify-location"), hasEntry("vary", "Content-Type, Accept")));
assertThat(response.payload(), is(Optional.of(ByteString.encodeUtf8("world"))));
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class MinimalApp method beer.
static CompletionStage<Response<String>> beer(RequestContext context) {
Request breweryRequest = Request.forUri("http://brewery/order");
CompletionStage<Response<ByteString>> orderRequest = context.requestScopedClient().send(breweryRequest);
return orderRequest.thenApply(orderResponse -> {
if (orderResponse.status() != Status.OK) {
return Response.forStatus(Status.INTERNAL_SERVER_ERROR);
}
// assume we get an order id as a plaintext payload
final String orderId = orderResponse.payload().get().utf8();
return Response.forPayload("your order is " + orderId);
});
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class AlbumResource method getAlbums.
CompletionStage<Response<ArrayList<Album>>> getAlbums(RequestContext requestContext, String tag) {
// We need to first query the search API, parse the result, then query the album API.
Request searchRequest = Request.forUri(SEARCH_API + "?type=album&q=tag%3A" + tag);
Client client = requestContext.requestScopedClient();
return client.send(searchRequest).thenComposeAsync(response -> {
String ids = parseResponseAlbumIds(response.payload().get().utf8());
return client.send(Request.forUri(ALBUM_API + "?ids=" + ids));
}).thenApplyAsync(response -> Response.ok().withPayload(parseAlbumData(response.payload().get().utf8())));
}
Aggregations