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;
}
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));
}
}
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()));
}
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()));
}
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()));
}
}
Aggregations