use of com.spotify.apollo.test.response.ResponseSource in project apollo by spotify.
the class StubClient method send.
@Override
public CompletionStage<Response<ByteString>> send(Request request) {
final ResponseSource responseSource = responseSource(request);
if (responseSource == null) {
final NoMatchingResponseFoundException notFound = new NoMatchingResponseFoundException(formatRequestNotMatchedExceptionMessage(request));
final CompletableFuture<Response<ByteString>> notFoundFuture = new CompletableFuture<>();
notFoundFuture.completeExceptionally(notFound);
return notFoundFuture;
}
// Create response task
final ResponseWithDelay responseWithDelay = responseSource.create(request);
// Schedule a response in the future
final CompletableFuture<Response<ByteString>> future = new CompletableFuture<>();
final Runnable replyTask = () -> {
Response<ByteString> response = responseWithDelay.getResponse();
requestsAndResponses.add(RequestResponsePair.create(request, response));
future.complete(response);
};
if (responseWithDelay.getDelayMillis() <= 0) {
replyTask.run();
} else {
executor.schedule(replyTask, responseWithDelay.getDelayMillis(), TimeUnit.MILLISECONDS);
}
return future;
}
use of com.spotify.apollo.test.response.ResponseSource in project apollo by spotify.
the class StubClientTest method shouldDisallowInfiniteIterativeReplies.
@Test
public void shouldDisallowInfiniteIterativeReplies() throws Exception {
ResponseWithDelay response1 = ResponseWithDelay.forResponse(Response.forPayload(encodeUtf8("first response")));
ResponseWithDelay response2 = ResponseWithDelay.forResponse(Response.forPayload(encodeUtf8("second response")));
List<ResponseWithDelay> responses = Arrays.asList(response1, response2);
ResponseSource constantResponses = Responses.sequence(responses);
stubClient.respond(constantResponses).to("http://ping");
assertThat(getStringFromPing().toCompletableFuture().get(), is("first response"));
assertThat(getStringFromPing().toCompletableFuture().get(), is("second response"));
try {
getStringFromPing().toCompletableFuture().get();
fail();
} catch (IllegalStateException e) {
assertThat(e.getMessage(), is("No more responses specified!"));
}
}
use of com.spotify.apollo.test.response.ResponseSource in project apollo by spotify.
the class StubClientTest method shouldReturnStatusCodeIntegers.
@Test
public void shouldReturnStatusCodeIntegers() throws Exception {
ResponseWithDelay response = ResponseWithDelay.forResponse(Response.of(Status.createForCode(666), encodeUtf8("constant response")));
ResponseSource responses = Responses.constant(response);
stubClient.respond(responses).to("http://ping");
Response<ByteString> reply = getResponseFromPing().toCompletableFuture().get();
assertThat(reply.status(), withCode(666));
}
use of com.spotify.apollo.test.response.ResponseSource in project apollo by spotify.
the class StubClientTest method shouldReturnIterativeRepliesWithVaryingStatusCodes.
@Test
public void shouldReturnIterativeRepliesWithVaryingStatusCodes() throws Exception {
ResponseWithDelay response1 = ResponseWithDelay.forResponse(Response.of(Status.ACCEPTED, encodeUtf8("first response")));
ResponseWithDelay response2 = ResponseWithDelay.forResponse(Response.of(Status.FORBIDDEN, encodeUtf8("second response")));
List<ResponseWithDelay> responses = Arrays.asList(response1, response2);
ResponseSource responseSequence = Responses.sequence(responses);
stubClient.respond(responseSequence).to("http://ping");
CompletionStage<Response<ByteString>> reply1 = getResponseFromPing();
CompletionStage<Response<ByteString>> reply2 = getResponseFromPing();
Response<ByteString> message1 = reply1.toCompletableFuture().get();
Response<ByteString> message2 = reply2.toCompletableFuture().get();
assertThat(message1.status(), is(Status.ACCEPTED));
assertThat(message2.status(), is(Status.FORBIDDEN));
assertThat(message1.payload().get().utf8(), is("first response"));
assertThat(message2.payload().get().utf8(), is("second response"));
}
use of com.spotify.apollo.test.response.ResponseSource in project apollo by spotify.
the class StubClientTest method shouldReturnConstantReplies.
@Test
public void shouldReturnConstantReplies() throws Exception {
ResponseWithDelay response = ResponseWithDelay.forResponse(Response.forPayload(encodeUtf8("constant response")));
ResponseSource responses = Responses.constant(response);
stubClient.respond(responses).to("http://ping");
assertThat(getStringFromPing().toCompletableFuture().get(), is("constant response"));
assertThat(getStringFromPing().toCompletableFuture().get(), is("constant response"));
}
Aggregations