### Question:
Requests { public static LiveHttpRequest doFinally(LiveHttpRequest request, Consumer<Optional<Throwable>> action) { return request.newBuilder() .body(it -> it.doOnEnd(action)) .build(); } private  Requests(); static LiveHttpRequest doFinally(LiveHttpRequest request, Consumer<Optional<Throwable>> action); static LiveHttpResponse doFinally(LiveHttpResponse response, Consumer<Optional<Throwable>> action); static LiveHttpRequest doOnComplete(LiveHttpRequest request, Runnable action); static LiveHttpResponse doOnComplete(LiveHttpResponse response, Runnable action); static LiveHttpRequest doOnError(LiveHttpRequest request, Consumer<Throwable> action); static LiveHttpResponse doOnError(LiveHttpResponse response, Consumer<Throwable> action); static LiveHttpRequest doOnCancel(LiveHttpRequest request, Runnable action); static LiveHttpResponse doOnCancel(LiveHttpResponse response, Runnable action);  }### Answer:
@Test public void requestDoFinallyActivatesWhenSuccessfullyCompleted() { Requests.doFinally(request, completed::set) .consume(); publisher.next(new Buffer("content", UTF_8)); assertThat(completed.get(), is(nullValue())); publisher.complete(); assertThat(completed.get(), is(Optional.empty())); }
@Test public void responseDoFinallyActivatesWhenSuccessfullyCompleted() { Requests.doFinally(response, completed::set) .consume(); publisher.next(new Buffer("content", UTF_8)); assertThat(completed.get(), is(nullValue())); publisher.complete(); assertThat(completed.get(), is(Optional.empty())); }
@Test public void requestDoFinallyActivatesWhenErrors() { RuntimeException cause = new RuntimeException("help!!"); Requests.doFinally(request, completed::set) .consume(); publisher.next(new Buffer("content", UTF_8)); assertThat(completed.get(), is(nullValue())); publisher.error(cause); assertThat(completed.get(), is(Optional.of(cause))); }
@Test public void responseDoFinallyActivatesWhenErrors() { RuntimeException cause = new RuntimeException("help!!"); Requests.doFinally(response, completed::set) .consume(); publisher.next(new Buffer("content", UTF_8)); assertThat(completed.get(), is(nullValue())); publisher.error(cause); assertThat(completed.get(), is(Optional.of(cause))); }