Search in sources :

Example 1 with StatusType

use of com.spotify.apollo.StatusType in project apollo by spotify.

the class TransformingCallback method transformResponse.

static Response<ByteString> transformResponse(okhttp3.Response response) throws IOException {
    final StatusType status = transformStatus(response.code(), Optional.ofNullable(response.message()));
    Response<ByteString> apolloResponse = Response.forStatus(status);
    for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) {
        apolloResponse = apolloResponse.withHeader(entry.getKey(), HEADER_JOINER.join(entry.getValue()));
    }
    final byte[] bytes = response.body().bytes();
    if (bytes.length > 0) {
        apolloResponse = apolloResponse.withPayload(ByteString.of(bytes));
    }
    return apolloResponse;
}
Also used : StatusType(com.spotify.apollo.StatusType) ByteString(okio.ByteString) List(java.util.List) ByteString(okio.ByteString) Map(java.util.Map)

Example 2 with StatusType

use of com.spotify.apollo.StatusType in project apollo by spotify.

the class AsyncContextOngoingRequest method sendReply.

// handles common functionality for reply() and drop() and is not overridable
private void sendReply(Response<ByteString> response) {
    if (!replied.compareAndSet(false, true)) {
        LOGGER.warn("Already replied to ongoing request {} - spurious response {}", request, response);
    } else {
        final HttpServletResponse httpResponse = (HttpServletResponse) asyncContext.getResponse();
        final StatusType status = response.status();
        httpResponse.setStatus(status.code(), status.reasonPhrase());
        response.headers().asMap().forEach(httpResponse::addHeader);
        response.payload().ifPresent(payload -> {
            try {
                payload.write(httpResponse.getOutputStream());
            } catch (IOException e) {
                LOGGER.warn("Failed to write response", e);
            }
        });
        asyncContext.complete();
        logger.accept(this, Optional.of(response));
    }
}
Also used : StatusType(com.spotify.apollo.StatusType) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 3 with StatusType

use of com.spotify.apollo.StatusType in project apollo by spotify.

the class MinimalAppTest method shouldRespondWithStatusCode.

@Test
public void shouldRespondWithStatusCode() throws Exception {
    stubClient.respond(Response.of(Status.IM_A_TEAPOT, ORDER_REPLY_BYTES)).to(BREWERY_ORDER_URI);
    StatusType status = serviceHelper.request("GET", "/beer").toCompletableFuture().get().status();
    assertThat(status.code(), is(Status.INTERNAL_SERVER_ERROR.code()));
}
Also used : StatusType(com.spotify.apollo.StatusType) Test(org.junit.Test)

Example 4 with StatusType

use of com.spotify.apollo.StatusType in project apollo by spotify.

the class MinimalAppTest method shouldBeOkWithPayload.

@Test
public void shouldBeOkWithPayload() throws Exception {
    stubClient.respond(Response.of(Status.IM_A_TEAPOT, ORDER_REPLY_BYTES)).to(BREWERY_ORDER_URI);
    StatusType status = serviceHelper.request("POST", "/beer", ByteString.encodeUtf8("{\"key\": \"value\"}")).toCompletableFuture().get().status();
    assertThat(status.code(), is(Status.INTERNAL_SERVER_ERROR.code()));
}
Also used : StatusType(com.spotify.apollo.StatusType) Test(org.junit.Test)

Example 5 with StatusType

use of com.spotify.apollo.StatusType in project apollo by spotify.

the class MiddlewaresTest method httpShouldNotSetContentLengthOrAppendPayloadForInvalidStatusCodes.

@Test
public void httpShouldNotSetContentLengthOrAppendPayloadForInvalidStatusCodes() throws Exception {
    List<StatusType> invalid = ImmutableList.of(Status.createForCode(100), NO_CONTENT, NOT_MODIFIED);
    for (StatusType status : invalid) {
        CompletableFuture<Response<ByteString>> future = new CompletableFuture<>();
        delegate = requestContext -> future;
        future.complete(Response.of(status, ByteString.of((byte) 14, (byte) 19)));
        Response<ByteString> result = getResult(Middlewares.httpPayloadSemantics(delegate));
        Optional<String> header = result.headers().get("Content-Length");
        assertThat("no content-length for " + status, header, is(Optional.empty()));
        assertThat("no payload for " + status, result.payload(), is(Optional.<ByteString>empty()));
    }
}
Also used : Response(com.spotify.apollo.Response) CompletableFuture(java.util.concurrent.CompletableFuture) StatusType(com.spotify.apollo.StatusType) ByteString(okio.ByteString) ByteString(okio.ByteString) Test(org.junit.Test)

Aggregations

StatusType (com.spotify.apollo.StatusType)6 ByteString (okio.ByteString)3 Test (org.junit.Test)3 Request (com.spotify.apollo.Request)1 Response (com.spotify.apollo.Response)1 InvalidUriException (com.spotify.apollo.route.InvalidUriException)1 RuleMatch (com.spotify.apollo.route.RuleMatch)1 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1