CompoundInterceptor implements Interceptor { @Override public HttpResponse<?> onFail(Exception e, HttpRequestSummary request, Config config) throws UnirestException { return interceptors.stream() .map(i -> Optional.ofNullable(i.onFail(e, request, config))) .flatMap(Util::stream) .findFirst() .orElseThrow(() -> new UnirestException(e)); } CompoundInterceptor();   CompoundInterceptor(List<Interceptor> interceptors); @Override void onRequest(HttpRequest<?> request, Config config); @Override void onResponse(HttpResponse<?> response, HttpRequestSummary request, Config config); @Override HttpResponse<?> onFail(Exception e, HttpRequestSummary request, Config config);  }
@Test void exceptionsStopsArFirstOne() { CompoundInterceptor compound = new CompoundInterceptor( Arrays.asList(t1, t2) ); RuntimeException e = new RuntimeException(); when(t2.onFail(e, summary, config)).thenReturn(new MockResponse<>()); HttpResponse r = compound.onFail(e, summary, config); assertTrue(r instanceof MockResponse); verify(t1).onFail(e, summary, config); verify(t2).onFail(e, summary, config); verifyNoMoreInteractions(t1); verifyNoMoreInteractions(t2); }
@Test void willThrowIfNothingElse() { CompoundInterceptor compound = new CompoundInterceptor( Arrays.asList() ); RuntimeException e = new RuntimeException(); UnirestException u = assertThrows(UnirestException.class, () -> compound.onFail(e, summary, config)); assertSame(e, u.getCause()); }